Greetings, I am trying to write a program that takes in numbers and outputs only specific ones; highest number, lowest number, the total numbers entered, the sum of all numbers, and lastly the average of all numbers. This is program is to be done with arrays, so used a mixture of if-else & do-while statements. Unfortunately I have run into a halt as only three numbers will be processed.
#include<iostream>
usingnamespace std;
int main()
{
float large, small, average, total, total2, sum, a, b, c;
//the next entered third number
char thirdnum, y, n;
///do {
cout << "Please enter two numbers:";
cin >> a >> b;
//comparing for high & low
if( a > b)
{
large = a;
small = b;
}
else
{
large = b;
small = a;
}
//calculate sum
sum=a+b;
//calculate total numbers entered
total = 2;
//calculate average
average = sum / total;
cout << "Do you wish to enter more numbers? Enter Y or N to continue:";
cin >> thirdnum;
if (thirdnum = y);
{
cout << "Please enter you number:";
cin>>c;
sum = sum + c;
total = total + 1;
average = sum / total;
if( c > large)
{
large = c;
}
else
{
if (c < small);
small = c;
}
}
cout << "Do you wish to enter another numbers? Enter Y or N to continue:";
cin >> thirdnum;
if (thirdnum = n);
{
cout << "The largest number is:" << large << "\n";
cout << "The smallest number is: " << small << "\n";
cout << "The average is:" << average << "\n";
cout << "The total number of numbers entered is:" << total << "\n";
cout << "The sum of all numbers is:" << sum << "\n";
}
return 0;
}
Suggestions? Ideas? Assistance?
Thanks for anything provided.
#include<iostream>
int main()
{
// initialize
float value {};
size_t count {};
// handle input
while ( std::cin >> value ) {
// do something with value
++count;
}
if ( 0 < count ) {
// show results
}
return 0;
}
The sample program that I did post has only one value at a time, but you don't know how many times.
Besides, finding the largest and finding the smallest are independent operations, are they not? A single new value might be larger than anything before or smaller than anything before, but it could be both too (it is on the first value), and most of the time it is probably neither.
What would you add to my sample program in order to get the smallest value?
Would you need to initialize something before the loop?
What should you do in the loop with the value?
What should you show on line 16?
Couldn't you use a std::list as a container (which is not an array but a doubly linked list). Then you can iterate over the list and determine the high, low, average and total count using list iterators. It is not using an array but a list container so you can get the min val, max val, average (sum/count) and count with one full iteration through the list.