# Thread: strange numbers, while loops questionable?

1. ## strange numbers, while loops questionable?

This program is supposed to give me the average, largest, smallest, sum, and number of entries from a group of integers.
The results I'm getting are impossibly wrong. I suspect that the "while (cin >> x) " has something to do with it, but I may be wrong. What I'm trying to do there is say "if you're still getting input, keep at it".
The other thought I had is that it is taking end of file (ctrl+Z) as a value. Anyway, this is the code (I know it's not pretty, I'm new at this!):

Code:
```// entering a list of integers and finding the number of entries, the smallest/largest entry,
// the average of the entries, and their sum. (that's the theory, anyway : ) )

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int get_data (int);
int count_integers (int,int);
int sum_int (int , int);
int large_num (int, int);
int small_num (int, int);
int avg_total (int ,int , int, int);
int results (int ,int, int, int, int);

int main ()
{
int x;
int count;
int sum;
int largest;
int smallest;
int avg;

get_data (x);
count_integers (x,count);
sum_int (sum, x);
large_num (x, largest);
small_num (x, smallest);
avg_total (x, count, sum, avg);
results (count, sum, avg, largest, smallest);

system ("pause");
return 0;
}

int get_data (int x)                 //getting input from keyboard
{
cout<<"Please enter a list of integers,([Ctrl]+Z to end):"<<endl;
cin>>x;
return x;
}
int count_integers (int x, int count)  // counting the # of entries
{
while (cin>>x)
{
count++;
}
return count;
}
int sum_int (int sum, int x)        // getting the sum of the entries
{
while (cin>>x)
{
sum = sum + x;
}
return sum;
}

int large_num (int x, int largest)  //finding the largest entry
{
while (cin>>x)
{
if (x >= largest)
{
largest = x;
}
}
return largest;
}
int small_num (int x, int smallest)  //finding the smallest entry
{
while (cin>>x)
{
if (x<=smallest)
{
smallest = x;
}
}
return smallest;
}
int avg_total (int x,int count, int sum,int avg) // finding the average of the ehtries
{
while (cin>>x)
{
avg = count/sum;
}
return avg;
}
int results (int count, int sum, int avg,int smallest, int largest) //print results
{
cout<<"You entered "<<count<<" integers."<<endl;
cout<<"the sum of the integers is "<<sum<<"."<<endl;
cout<<"The average of the integers is "<<avg<<"."<<endl;
cout<<"The largest integer entered was "<<largest<<"."<<endl;
cout<<"The smallest integer entered was "<<smallest<<"."<<endl;

}```
And this is what I get for results, irregardless of the entries:
Please enter a list of integers,([Ctrl]+Z to end):
12
2
22
55
^Z
You entered 4370432 integers.
the sum of the integers is 4198592.
The average of the integers is 2293600.
The largest integer entered was 2009196833.
The smallest integer entered was 2293664.
Press any key to continue . . .
Any ideas? I know, it's something glaringly obvious. I think I just need that one hint that'll make me say "Oooohh, ok.."

If there is a good tutorial or page that might help to clarify things, please clue me in. I am still woefully lacking in decent reference texts, so for now I'm using the web.

2. I may be wrong so wait for someone else to respond, but should you not declare some of the integers as 0, so there is nothing already in that memory space?

3. Problem #1: You didn't initialize your variables. The garbage that got output was the values of the variables (count, sum, largest, etc.) when you didn't initialize them. You must set them to zero or some other meaningful number. An example is count. If you don't set it to 0, then what does count++ mean? How can you add 1 to some random number? Obviously, if you start it at zero, that would help. The smallest and largest might take more thinking.

Problem #2: You are not getting the results of the functions. There are two ways to do this. One is to save the return value. All of your functions are returning a value, but when you call them in main you don't save the return value anywhere. You could do something like count = count_integers (x,count);. Another option is to make the parameter a reference, which means that the variable you pass in will be updated when it is changed inside the function. Otherwise, you are passing a copy of the data to the function, but the variable in main still has the original value after the function exits. You can solve this problem either way. The reference might be better if you understand references, but either way is fine.

Problem #3: You need to get input in only one place. Then, return the number that was input and pass that number to your other functions to update the count, sum, smallest, etc. If you do the getting of the number in a function, you might have to use a reference because you will want to return the number and true or false depending on whether the user hit Ctrl-Z or not. Regardless, the point is you shouldn't be doing input in each function, you should only use the passed in value for x once.

4. Ok, I've got myself on the right track now, I think. Thanks.
I see what you mean about "largest/smallest requiring more thought"..I combined a few things to fix part of my problem, as well as putting "avg=sum/count" in the right order (D'oh!!), but the largest/smallest is still throwing me a bit. I've tried making x equal another variable "x = lrg, or x = sml" since it just seems to make the largest whatever the last entry was, and the smallest equal to zero.
After several attempted permutations the only thing I'm absolutely sure of is that the "too many errors, bailing" message in dev-c++ is only funny the first few times...
Anyway, here's where I'm at:

Code:
```#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int get_data (int&, int&, int&);
int avg_total (int& ,int& , int&,int&);
int large_num (int&, int&);
int small_num (int&, int&);
int results (int ,int, int,int, int);

int main ()
{
int x;
int sum =0 ;
int count=0;
int avg;
int largest=0;
int smallest=0;

get_data (x,sum,count);
large_num (x, largest);
small_num (x, smallest);
avg_total (x,avg,sum,count);
results (count, sum, avg,largest,smallest);

system ("pause");
return 0;
}

int get_data (int& x,int& sum, int& count)                 //getting input from keyboard, sum of
// integers and # of integers
{
cout<<"Please enter a list of integers,([Ctrl]+Z to end):"<<endl;
cin>>x;

do
{
sum = sum + x;
count++;
} while (cin>>x);

return (count / sum);
}

int large_num (int& x, int& largest)  //finding the largest entry
{
do
{
if ((x >= largest)&& (x != 0))
{
largest = x;
}
} while (cin>>x);
return largest;
}
int small_num (int& x, int& smallest)  //finding the smallest entry
{
do
{
if ((x <= smallest) && (x !=0))
{
smallest = x;
}
}while (cin>>x);
return smallest;
}
int avg_total (int& x,int& avg, int& sum, int& count)   // average of the numbers entered

{
do
{
avg = sum/count;
} while (cin>>x);
return avg;
}

int results (int count, int sum, int avg, int largest,int smallest) //print results
{
cout<<"You entered "<<count<<" integers."<<endl;
cout<<"the sum of the integers is "<<sum<<"."<<endl;
cout<<"The average of the integers is "<<avg<<"."<<endl;
cout<<"The largest integer entered was "<<largest<<"."<<endl;
cout<<"The smallest integer entered was "<<smallest<<"."<<endl;
}```
and the output is this
Please enter a list of integers,([Ctrl]+Z to end):
10
20
15
^Z
You entered 3 integers.
the sum of the integers is 45.
The average of the integers is 15.
The largest integer entered was 15.
The smallest integer entered was 0.
Press any key to continue . . .
Closer, but still no cigar for the largest and smallest. I have a feeble grasp of why, I just haven't quite hit on the solution. If I'm following jlou's previous reply, I need to have smallest and largest holding a value somehow. Anyway, a push in the right direction (Thanks for the last push jlou, at least I got it started!) would be appreciated.

Oh, and one more thing. My dev-c++ sometimes has the annoying habit of erasing the rest of the line rather than letting me insert something. Probably fumbling fingers, but does anyone know why it does that?

5. As for the smallest being 0, you need to set smallest to a very big number, because 0 will always be smaller than everything else, except negative numbers.
dont know about the largest yet

And you are using return, but no assignment operator (=) in the main function.. I don't see the point...

6. Originally Posted by Rouss
As for the smallest being 0, you need to set smallest to a very big number, because 0 will always be smaller than everything else, except negative numbers.
dont know about the largest yet

And you are using return, but no assignment operator (=) in the main function.. I don't see the point...
Well, it doesn't really seem to make a difference if it's not there. Force of habit, I suppose. If it's a bad habit, tell me!
If I just set smallest to a large number, it just considers largest and smallest the last number entered it's not holding onto what it had before....Somehow I need to make them both compare the first input, discard it if it's smaller (or larger) and check the next one.
if it were a finite number of integers coming in, I could get by with an if statement ( if x>a,etc..) but I can't do that here.

7. What I would do is set each intiger to a variable, and then compare variables.

This way, you can easily control each number.

I think this can be done with a for loop and an array.

Code:
```int control[100];

for(int var=0, var>amountofnumbers-1; var++){
control[var] = x;
}```
You would have to incorporate this into your code, but i'm just trying to give you an idea.

Then you can do another for loop to compare them with if statements.

8. You must solve problem #3 that I mentioned above. When you write do { ... } while (cin >> x) in each function, that code is not actually getting anything into x except for in the get_data function. The reason is that you do cin >> x inside get_data until the user hits Ctrl-Z, meaning it evaluates to false and breaks the loop. Any time you do cin >> x after that it will evaluate to false, so the do-while loops in your other functions are only running once.

For your other functions (largest and smallest) to work properly, the do-while loop should be removed from all of the functions and you must make a loop in your main() function that calls get_data until it returns false. It should return false when cin >> x evaluates to false. Your main function loop would then pass the data you just got to largest() and smallest() each time through the loop.

If you fix Problem #3 you'll be a lot closer.

9. Is the program layout set in stone, like an assignment for class to practice writing functions and passing values back and forth? Or do you have the option of combining all those loops and functions into fewer loops and functions (say maybe 1)?

//all in the same loop
obtain current value
if current value less than smallest value, change smallest value
if current value larger than largest value, change largest value
increment count
increment sum
repeat until all values evaluated

//when loop is done
calculate average.

Popular pages Recent additions