Why is this an endless loop? [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 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.

Related

How to fix "1 value required as left operand of assignment"? [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 2 years ago.
Improve this question
I want to build a project, where a counter counts up, every time a measurement is under 800 and resets if a measurement is over 800. I tried lots of things, but many didn't work, and some were way to complicated. Please help me... This down there is my original code, which doesn't work.
const int sensor = A0;
int x;
void setup(){
x = 0;
}
void loop(){
int Val = analogRead(sensor);
if(Val =< 800){
x + 1;
}
else{
x = 0;
}
delay(250);
}
Inside your if condition, you are not setting the value of variable. You are just giving an expression.
if(Val =< 800){
x + 1;
}
The correct code would be assigning the new value to the variable again, and also your relational operator is wrong.. it should be <=
if(Val <= 800){
x = x + 1;
}

How to calculate long long / int [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 4 years ago.
Improve this question
I am trying to learn how to code by myself and is experiencing some difficulty in calculations. Can someone please explain why the pf always return 0 in the below?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}
pf will always be zero, because num is set to zero at the end of your while loop.
Therefore num/anything will always equal zero.
A good method of debugging, is to step through the code line by line, and look at the values of your variables at each point in time.
This can help you narrow down problems like this.
The problem is where you get the length of your number:
while (num>0)
num/=10;
num will always be 0 after this and thus your final expression will result in 0 because 0/x = 0 (x != 0).

I can't understand what's going wrong in this program [closed]

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);
}

C telling me to initialize variable even though it's already initialized [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 6 years ago.
Improve this question
I am working on CS50 PSET1. I have the following code so far:
#include <stdio.h>
#include <cs50.h>
int main(void) {
float change;
do {
printf("Change: ");
change = get_float();
} while(change < 0);
int coins;
for(int q = change; q < 25; q++) {
q = 25 / q;
coins += 1;
}
printf("%i", coins);
}
I am having an issue. When I try to compile my code with the make command I get an error saying this
greedy.c:17:9: error: variable 'coins' is uninitialized when used here [-> Werror,-Wuninitialized]
coins += 1;
The compiler is correct. You never assign anything to coins in the first place. All you do is increment its (uninitialized) value.
To assign an initial value, write
int coins = 0; /* or whatever the correct initial value is */
As an aside, I'm not quite sure what the intent is, but the following is highly unlikely to be what you want:
for(int q = change; q < 25; q++) {
q = 25 / q;
Note how the assignment modifies the loop variable. While this is permissible, in this context it looks unlikely to be intentional.

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)

Resources