Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
#include<math.h>
#include<stdio.h>
int main(void)
{
int i = 0;
int f = 10000;
int div1 = (powl(10,i));
int temp1 = f/div1;
for(i = 2; temp1 > 1; i++)
{
printf("%i\n",temp1);
}
}
As far as I know, the value of div1 should be 100,1000,10000... With corresponding increments in I. Then temp1 should be 100,10, then loop stops (?). But I get an endless loop of 10000 10000 10000 10000......
Can someone explain what am I doing wrong?
The for loop checks for temp1, but temp1 is not modified in the loop's body. Try putting the desired modification inside the loop's body or as the last expression in the for loop; the variable i is perhaps not necessary at all.
Your for statement should have like this. You missed to call those to statement inside your for loop
for(i = 2; temp1 > 1; i++)
{
div1 = (powl(10,i));
temp1 = f/div1;
printf("%i\n",temp1);
}
Related
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 months ago.
Improve this question
#include<studio.h>
int main()
{
int a=5,i;
i!=a>10;
printf("i=%d",i);
return 0;
}
This code should print i=1 but it is printing i=0 Why is it so?
That's because you don't do anything to i.
Your "i!=a>10" evaluates to false, but the result is not stored into a variable.
As it is mentioned in the comments, you need something like this:
int a = 5;
int i = !(a > 10);
The != is mostly used in if-clauses, like
if (a != 0) {...}
I hope this helps. ;)
It's not printing because:
the variable i is not initialized
the second statement should be changed and be a variable int i = !(a > 10)
Returning a value and printing it are different things, but that's not the point of the question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I would appreciate if anyone can help me figuring out why is this fuction (fn) does not print the int array elements?
#include <stdio.h>
void fn (int arr[], int s);
int main(void)
{
int arr[] = {5, 2, 0, 9, 7}, s=5;
fn (arr, s);
}
fn(int arr[], int s)
{
int i;
for (i = 0; i < 5; i++);
{
arr[i]++;
printf("%d ", arr[i] );
}
}
for (i = 0; i < 5; i++);
The semicolon at the end of the expression terminates the for-loop before even entering the body of the loop. Hence the body is seen as a stand-alone block of code by the compiler, unrelated to the for-loop.
Since the body of the loop is detached from the for-loop, variable i (the counter of the for-loop) is out of scope since i is local to the for-loop, and can only be called inside it as it was declared inside the conditional statement of the for-loop.
To fix this problem, remove the semicolon at the end of the for-loop condition.
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 6 years ago.
Improve this question
I believe it is because of the strcmp(). I tried it multiple ways so far and this is just the latest. My objective is to get the index of the array so I can go on to a switch statement to execute code. Any help would be appreciated, although I'm only expecting a "You can't do that."
Big picture is to incorporate this snip of code into a "utility" file that has multiple "functions" and would call it like util("Ping") to execute a Ping and so on...
int main(){
char *cmd = "Ping";
char *names[3]={"Ping","Stop","Go"};
int index = 3;
int i;
char *test;
for (i = 0; i < 44; i++)
{
test = names[i];
if (!strcmp(cmd,test))
{
index = i;
}
}
printf("%s is index of %d\n",cmd,index);}
Why do loop it for 44 times I don't get it. But when I changed 44 to 3 in the loop it works.
for (i = 0; i < 3; i++)
Depends on your compiler though but in case of 44 iteration windows gives me message application not responding.
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)
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
First of all sorry for this, I really think it is a silly question but I've been stuck on this for a while. So maybe you can help me. The problem is I don't truly understand what's wrong in the code. So let's look up.
void enter()
{
int init= 1, end= 2;
float jump= 0.2;
create(init, end, jump);
}
void create(int Init, int End, float Jump)
{
float i;
int total = 0;
for(i = Init; i < End; i + Jump)
total += 1;
}
It does not exit the loop and I don't understand why.
The problem is here:
i + Jump
That does not change i, it simply evaluates. Change it to this:
i += Jump
and it should work fine.
The loop is infinit because variable i is not being changed within the loop. Change this statement
for(i = Init; i < End; i + Jump)
to
for(i = Init; i < End; i += Jump)
Also maybe there is a sense to define the function as having return type int is not there? For example
int create(int Init, int End, float Jump)
{
//...
return total;
}
i + Jump this does not modify i and hence i < End is always evaluated as TRUE, as per the initial condition (Init being less than End).
You need to change to i += End to keep incrementing the value of i.