What the FUCK am I doing wrong?#include <stdio.h>
int make_pyramid() {
int n;
for (n = 1; n == 1; n++) {
printf("%d\n", n);
}
for (n = 2; n == 2; n++) {
printf("2%d\n", n);
}
for (n = 3; n == 3; n++) {
printf("33%d\n", n);
}
for (n = 4; n == 4; n++) {
printf("4%d\n", n);
}
printf("%d", n);
return 0;
}
int main(void) {
make_pyramid();
return 0;
}
>>61547228
Nothing. You need to add some more for loops with hardcoded values though
>>61547250
The code looks ugly and I think it could be performed better, I just don't how ~how~.
Here I optimised you're codeint main(void)
{
printf("1\n22\n333\n44\n5");
return 0;
}
What the fuck are you doing right
>>61547228
>>61547228
>>61547228
Son, I am disappoint
>>61547228
kek
>>61547228
C programming
You're doing c programming wrong
>>61547228
>>61547228
>for( initialization; break condition; update)
Your initialization is wrong.
>>61547543
How so? I know how the for loop works but I'm not exactly sure of what I should be doing.
My goal is to use variables along with for loops to display:N
NN
NNN
NN
N
>>61547569
What you should be writing:
>for (n = 0; n == 3; n++)
Meaning:
>Start with n being zero. Do the thing that needs doing. Add 1 to n. Stop if n is 3.
What you wrote:
>for (n = 3; n == 3; n++)
Meaning:
>Start with n being 3. Do the thing that needs doing. Add 1 to n. Stop if n is 3.
If you are starting with n being 3, your program is not going to do much, since you are also telling your program to stop at 3. Your starting and ending points should be at different places, right?
>>61547228
lyke this?
#include <stdio.h>
int make_pyramid() {
int n;
for (n = 1; n < 5; ++n) {
for (int x = 4; x > n; --x)
printf(" ");
for (int i = 0; i < n; ++i) {
printf("%d ",n);
}
printf("\n");
}
return 0;
}
int main(void) {
make_pyramid();
return 0;
}
also how do you post code like op did?
>>61547613
You bring an interesting point, although I was told before that the method I'm using to print "22" "333" and so on is horrible.printf("33%d\n", n);
Any idea of what I could be doing instead?
>>61547691
start the text with (code) and end it with (/code) but replace the parenthesis for brackets
>>61547716
:code:
#include <stdio.h>
int make_pyramid() {
int n;
for (n = 1; n < 5; ++n) {
for (int x = 4; x > n; --x)
printf(" ");
for (int i = 0; i < n; ++i) {
printf("%d ",n);
}
printf("\n");
}
return 0;
}
int main(void) {
make_pyramid();
return 0;
}
:/code:
test
>>61547699
Your program is inefficient, but that is not the main issue. Start by understanding how loops work first! You want to print 5 lines, so you are using 5 loops. Get that program working first, and then move on to fix the next issue.
What if you want a pyramid of 10 lines? Of 50 lines? You cannot write 50 loops, right? Have you studied "nested loops" yet?
>>61547734
FUCK
{code}
test
{/code}
test
>>61547699
I honestly can't believe that a real person could be this fucking stupid. I hope this is a troll thread
>>61547754
oh i see now, thanks.
#include <stdio.h>
int make_pyramid() {
int n;
for (n = 1; n < 5; ++n) {
for (int x = 4; x > n; --x)
printf(" ");
for (int i = 0; i < n; ++i) {
printf("%d ",n);
}
printf("\n");
}
return 0;
}
int main(void) {
make_pyramid();
return 0;
}
>>61547747
Nested loops? Hmm, I was told of that but didn't know what it meant.
What's the point of redundancies like that ?
Anyway, my knowledge of C is limited to the basic syntax, but I'd do it like this .
#include<stdio.h>
int make_pyramid(int n, int s, int count){
while(s<=n){
printf( "%d", s);
count=count+1;
if (count==s){
printf("\n");
return make_pyramid(n, s+1, 0);
}
}
}
int main(){
return make_pyramid(9, 1 ,0);
}
>>61547755
(You) for the dubs
>>61547699
What the fuck are you doing? What fucking tutorial are you following? Someone is trolling you, the way print formating works is like this:printf("%d is an integer, %f is a float. For a string you use %s", 1,2.3,"this")
That will print out:1 is an integer, 2.3 is a float. For a string you use this.
>>61547844
No (You)
>>61547806
Why use a while loop as opposed to a for?
>>61547894
For the most part it's just preference. The rule I go by is this: use for loops when you know how many iterations you'll need, use a while loop when you don't know how any iterations it will take for the stop condition will take. For an example, let's say you want to keep the loop going until the user enters certain input to tell the program to stop.
>>61547944
>anime poster
Hi. Go fuck yourself.
>>61547755
Everyone starts somewhere
>>61547894
I honestly just don't know the syntax for the "for" statement.
>>61547790
A nested loop is just a loop inside of another.for (int x=0; x<3; ++x)
{
for (int y=0; y<3; ++y)
{
printf("(%d, %d)\n", x, y);
}
}
Output:(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
>>61547964
Please hang yourself
Welcome to the NHK is pure kino and acceptable on every board