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.