Loop program output [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I got curious and experimented a little on looping and I kind of confused myself here.
Question 1:
Why does this code output 012345678910 instead of 00000000000111111111112222222222233333333333444444444445555555555566666666666777
7777777788888888888999999999991010101010101010101010 ?
for(a=0;a<=10;a++){
for(a=0;a<=10;a++){
printf("%d", a);
}
}
Question 2: For the code shown below, does taking out the first statement mean that it would start on 0? If yes, then why does it output 12345678910 instead of 01234567891001234567891001234567891001234567891001234567891001234567891001234567
8910012345678910012345678910012345678910012345678910 ?
for(a=0;a<=10;a++){
for(;b<=10;b++){
printf("%d", b);
}
}

Because when a reaches 10 in the inner loop, the outer loop also exits, so you only see
012345678910
Because after the 1st time the outer loop is executed, b is already 11 and the inner loop is no longer executed.
For your desired output, you should reset b to zero every time the outer loop is executed
for (...) {
for (b = 0; ... ; ...) {...}
// ^~~~~ This is what you should do
}

For the first one, you are not getting the answer because you use same counter variable in inner loop:
Modify it to use different variable:
for(int a=0;a<=10;a++){
for(int b=0;b<=10;b++){
printf("%d", a);
}
}

You have same counter for outer and inner loop which is in this case incorrect. Use counter a and b then loop should work.
In inner loop b stays 11 after first iteration, if you want to achieve your expected result add b = 0 in your inner loop

Related

Explain this for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
int i; for ( i = 0; i = 3; i++ ) printf("Hello");
In this C code, the loop condition is "i = 3". What does this condition mean? And the loop runs infintely. Does that mean the condition is true?
Your test part in your for loop is i = 3 which is an assignment to the variable. After assignment it evaluates as the which has been assigned: 3 which is not 0 hence true in C.
Then the condition of your for loop is always hence you have an infinite loop.
If the condition was i == 3, it would be false at the first iteration of the loop. hence the code will never execute the body of the for block.
If the condition was i < 3, it would be true until the i variable is incremented up to the 3 value. Hence the body of the for loop would execute 3 times

My C code for 2d array creation and printing doesn't return any value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have written a piece of c code which I intend to just print out the value of a 2d array
I'm very new to c so this answer might be really basic sorry.
my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.
heres my code
#include <stdio.h>
int main(void) {
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i2 = 0;
printf("hello");
for(int i = 0; i > 4; i++){
i2 = i;
printf("hi %d", array1[i][i2]);
}
}
returned value
hello
i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.
Thanks.
There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example).
The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.

Stumped: for loop to build an array not working, initial condition ignored? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This one has me stumped. Here is my code to build an array, b[i] of doubles, from 0 to N where N = 126.
int N = 126;
double b[N];
int i;
for(i = 0; i < N; i++);
{
b[i] = (double)i;
printf("b[%lf] = %d\n",b[i], i);
}
For some reason, this is what I get:
b[126.000000] = 126
and nothing else. The initial condition of i being at 0 is ignored, and for some reason it sets i to be the value of N. Strange!
I'm a bit of a c novice so I must be missing something obvious. Any help greatly appreciated!
Andy.
The mistake is at you using the ; at the end of the for loop statement. That is why the program is simply executing the remaining statements as if they are in no loop, and at that time i has become 126.
Remove the ; on the end of the for loop, it is running through the loop without doing anything then executing the body for the last value of i(which is N = 126)

Loop iteration in c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i,x=10;
for(i=0;i<7;i++);
{
x++;
}
printf("%d",x);
}
Output : 11
No matter how many times the for loop iterates, the value of x stays 11. Why is that ?
Remove the semicolon from here:
for(i=0;i<7;i++);
The semicolon makes the for loop have an empty body. It makes it equivalent to
for(i=0;i<7;i++){}
Including warning flags in your compiler(-Wextra in GCC) emits a warning about these kind of issues.
Semicolon (; ) punctuation mark in C means that block of code is finished. That means if you use
for(i=0;i<7;i++);
{
x++;
}
For loop ends before it reaches brackets. Then code between your brackets runs like normal lines out of loop. If you want your loop to include brackets, get rid of the semicolon, like:
for(i=0;i<7;i++)
{
x++;
}

C - Nested For loop never stops [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I wrote a simple nested For loop, but for some reason it never stops.
#include <stdio.h>
int main (void) {
int x,y;
for (x=10;x<100;x+=10) {
for (y=10;y<100;x+=10)
printf("x is %d \n",x);
printf("y is %d \n",y);
}
return 0;
}
I'm new at c, but from examples I read, it should stop when x and y gets to 100. But for some reason it keeps on going for ever.
You need to increment y, not x
for (y=10;y<100;y+=10)
Also, it really looks like you meant to put braces around the inner loop
for (y=10;y<100;y+=10)
{ // <-- Did you mean to leave this out?
printf("x is %d \n",x);
printf("y is %d \n",y);
} // <-- and this?
Because y is not updated in the loop, which will make the second for loop always true (i.e. run forever).
You need to change
for (y=10;y<100;x+=10)
^
should be y here
to
for (y=10;y<100;y+=10)

Resources