Two similar for-loops give different results [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 3 years ago.
Improve this question
just playing around with for loops, and wanted to see what the results are.
I'm assuming both produce the same results, but I'm wrong.
int sum ,sum2 , i , j;
for( sum = 0, i = 1 ; i <= 5 ; sum+=i , i++ )
printf("%d\t",sum);
printf("\n");
for( sum2 = 0, j = 1 ; j <= 5 ; j++ ) {
sum2 +=j;
printf("%d\t",sum2); }
0 1 3 6 10
1 3 6 10 15

In the first loop sum is incremented at the end of the iteration, so after the call to printf, while in the second loop sum2 is incremented before the call to printf.

If you follow the execution pointer carefully, in the first loop sum += i occurs before i is incremented.
In the second loop, sum2 += j occurs after j has been incremented.

Related

Initializing a matrix with 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 1 year ago.
Improve this question
I can't imagine a simpler case of matrix initialization, and yet it goes horribly wrong, with the program getting into an endless loop. WHY????
#include <stdio.h>
int main ( void )
{
int M [ 5 ] [5] = {{}} ;
int i , j ;
for ( i = 1 ; i <= 5 ; i ++ )
{
for ( j = 1 ; j <= 5 ; j ++ ) M [ i ] [ j ] = 1 ;
}
return 0 ;
}
Indexes in arrays start from zero.
So the code should be
#include <stdio.h>
int main(void)
{
int M[5][5] = {{}} ;
int i, j ;
for (i = 0; i < 5; i ++ )
{
for (j = 0 ; j < 5 ;j ++ ) M[i][j] = 1 ;
}
return 0 ;
}
Not only, at stated above, indexes start with 0, not 1, which would lead you to write
for (i=0; i<5; i++)
… instead of :
for (i=1; i<=5; i++)
… (note the < sign instead of <=) but if you do it anyway, nothing will prevent you from doing. Since the machine code produced by your C compiler sticks to adding the index to the base pointer, you'll exceed the end of your array, thus leading to a buffer overflow.
And what lies after the array you declared are your index variables i and j. Since you fill your array with ones only, you'll write 1 into your index variables each time you run out of your array, hence resetting it to this particular value, and that's why your loop will never end.

for loops in C with incrementing a counter and short-hand counter++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
for i := 0 to 5 do
begin
i := i + 1;
counter++
end;
I am trying to write the code for a loop in C that will increment a counter from 0 to 5, writing out the counter value with each iteration. Use the C short-hand: counter++ to increment counter by 1 in each iteration of the loop. Am I doing it right?
No, this is not a correct syntax in C.
You must write it like :
for ( int i = 0; i < 5 ; i++ )
{
printf("Value of i: %d\n", i);
}
For more information:
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm

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)

Replacing even numbers with zero - Infinite loop issue 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
I've did a program for replacing even numbers with 0 and putting them at the end of the array.
Input: 1 2 3 4 5 6
Output: 1 3 5 0 0 0
The constrains are to use only one loop and two variables apart from the array. I've did it with a while loop but for some unknown reason it loops infinitely. Could someone please clarify?
#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = {1,2,3,4,5,6};
int i=0,j=5;
while(i<6){
//Loops till the zero is swapped to the end of the array
if(j!=5){
arr[j] = arr[j] + arr[j+1];
arr[j+1] = arr[j] - arr[j+1];
arr[j] = arr[j] - arr[j+1];
j++; continue;
}
//Checks for even number
if(arr[i]%2==0 && arr[i]!=0){
arr[i]==0;
j=i;
continue;
}
printf("%d ",arr[i]);
i++;
}
return 0;
}
What is causing the infinite loop is this line:
arr[i]==0;
This doesn't update the value of arr[i] when i = 1, so arr[1] is always 2, and this produces that i is stuck always with the value 1. Should be an assignment no a comparison expression:
arr[i]=0;
Note: If you enable the warnings when compiling, the line in question should produce one.

Function that's calculating a log - Unused variable error [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 8 years ago.
Improve this question
I'm writing a function that tries to get the square of a root.
I guess it's easier with an example:
I want to give a number to that function, say 1024 and the function should tell me 12. So always looking for the x here: 1024 = 2^x.
If I gave 255 to the function it should return 7.
Now I guess my maths is pretty okay, but I get an Error saying I didn't use a Variable in Line 6. Could you have a look?
int log_base2(int num)
{
int x = 2;
int count = 0;
for(; x <= num; x * 2 )
{
count++;
}
return count;
}
Error is in line 6 ( for(....))
for(; x <= num; x * 2 )
Here x * 2 calculates its value, and then throws the result out. What you want is probably:
for(; x <= num; x *= 2 )
The error message is perhaps because the compiler optimizes the variable x away as it's useless.
You are not modifying x anywhere. If you want x to become 2 * x in the next iteration you have to change this
for(; x <= num; x * 2 )
to
for(; x <= num; x = 2 * x )

Resources