Infinite Loop

Pages: 12
I cannot pinpoint why the infinite loop occurs. What I have makes sense in my head but not to the compiler

1
2
3
4
5
6
7
sum = 0
incr = 1/num
for (val = 20.0; val <= 21.0; val += incr) 
   {
      cout << val << " ";
      sum += incr;
   }


Assume all variables are defined as types and let's say num is 4
but my output is
20 20 20 20 20 20 ...

with 4 as num , the output is to be
20 20.25 20.5 20.75 21

Last edited on
What is the type of incr and num?
int num
float incr
How are you using num? What is it equal to?
num is equal to some positive even number; for the sake, I'm testing 4
oh yeah, this is an assignment so don't just give me the code but please point me in the right direction
When you are doing 1 / num, num is an integer and thus you are doing integer division resulting in an integer value of 0 (assuming num = 4). If you want your program to calculate the actual value, num needs to be a float as well.
but when I change num to a float, it messes up this loop:
1
2
3
4
5
  if(num % 2 == 0 && num > 0 && num != 0)
      cout << "The number " << num << " is even!" << endl << endl;
   else 
      cout << "The number is not a positive even number." << endl;
   } while (num % 2 !=0 || num <=0);
Well we didn't see that earlier did we? :D

May I suggest doing something like int num1 and float num2. Then have something like num1 = num2.

Also, in your if statement, having it say num != 0 right after num > 0 is redundant.
Last edited on
No you may not, cuz it didn't work :p
and it is redundant, but when I tested 0, it went right through the loop
Can you post the entire code please so we can see what your program is trying to do?
it actually did work, I just ~sigh~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/*
Randall Hall
CSCE 1020.302
Lab 09
randallhall2@my.unt.edu

*/
#include <iostream>
int main() {
   float val, sum, incr, num2;
   int num1;
   num1 = num2;

   cout << "CSCE 1020 Lab 9\tRandall Hall\trandallhall2@my.unt.edu" << endl;
   cout << "\n";
   do {
   cout << "Enter an even number: ";
   cin >> num1;
   if(num1 % 2 == 0 && num1 > 0 && num1 != 0)
      cout << "The number " << num1 << " is even!" << endl << endl;
   else 
      cout << "The number is not a positive even number." << endl;
   } while (num1 % 2 !=0 || num1 <=0);
   sum = 0;
   incr = 1/num2;
   for (val = 20.0; val >= 21.0; val += incr) 
   {
      cout << val << " ";
      sum += incr;
   }
   cout << "The sum is " << sum << endl;
   return 0;
}


Let's say you input 4
output supposed to be
20 20.25 20.5 20.75 21
The sum is: [sum of those numbers]
Last edited on
Your for loop to calculate sum does not even run, try changing val >= 21.0 to val <= 21.0. :D
Last edited on
I changed the for loop to a do while:

1
2
3
4
5
 do {
   cout << val << " ";
   sum += val;
   val += incr;
   } while (num2 < 21.0 && num2 > 20.0);

Here we go! The reasons why the program wasn't working are as follows. First, when we declare that num1 = num2, what ever random value num2 had is stored in num1. This is not good! We need to declare num2 = num1 after we have defined what num1 is. Secondly, we need to do something with sum += incr; Since incr = .25, sum is going to be .25 + .25 + .25 + .25 = 1. Change sum += incr; to something more meaningful.
CSCE 1020 Lab 9 Randall Hall    randallhall2@my.unt.edu

Enter an even number: 4
The number 4 is even!

20 20.25 20.5 20.75 21 The sum is 102.5

Process returned 0 (0x0)   execution time : 1.314 s
Press any key to continue.
Last edited on
the original issue was that 1/num is an integer value because 1 is an int, you need to do 1.0 so the compiler knows thats supposed to be a float

dont know if this input shed any light as i didnt read the whole thread
what do you mean by change sum += incr; to something more meaningful?
Last edited on
GFreak45, my friend suggested that also but it didn't do anything, well it did nut nothing that I could immediately see
1
2
3
4
5
6
7
sum = 0;
incr = 1/(float)num; // or incr = 1./num
for (val = 20.0; val <= 21.0; val += incr) 
   {
      cout << val << " ";
      sum += incr;
   }
And by the way, haven't you guys ever heard about something called Debugging? Like step over, step into, step out, call stack, watches, breakpoints? None at all?
Pages: 12