-
Need help with a program
"Write a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information.
1) the number of integers in the series (not including zero)
2) the average of the integers
3) the largest integer in the series
4) the smallest integer in the series
5) the range (difference between largest and smallest integer)
My problem is, I don't know what to initialize these int's to to get the above results.
Code:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
main()
{
int number;
int total;
double average;
int counter;
int largest;
int smallest = 130000;
int range;
do
{
cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;
if (number != 0);
cout << "The number of integers you entered was" << number << '\n';
cout << "The average of the integers is" << average << '\n';
cout << "The largest integer in the series is" << largest << '\n';
cout << "The smallest integer in the series is" << smallest << '\n';
cout << "The difference between the largest integer and smallest integer is" << range << '\n';
} while (number != 0);
return 0;
}
-
>> I don't know what to initialize these int's to to get the above results.
Do you know any of them? What you initialize counter to? How do you calculate the average?
-
I believe I know the largest and smallest. But i'm not sure, the others one i'm clueless. And I forgot what the counters purpose is, the teacher said you needed to have one.
Code:
if (number > largest) largest = number;
if (number < smallest) smallest = number;
Edit, and I believe the range would be :
Code:
range = largest - smallest;
-
In the code you posted the if(number != 0); line is useless.
Also, the requirements you posted said you were to display the statistical information regarding the integer series only once. In the first code you posted the statistical information will be displayed everytime a new integer is added by the user.
Intializing all the variables to zero to make things easy and is mandatory for counter and total.
You can calculate the statistics on the fly with each entry or collect all entrys in a container and loop through the container to do the statistics just once. Either way, you would not do anything if number equals zero and you would increment counter and either do the statistical evaluations or store the entry in the container if the number is not zero.
If doing the calculations on the fly, then there would be a special case if counter equals 1. If so, then largest and smallest would be assigned the first integer entered. Else you compare number to largest and smallest as in your second post. As each number is entered you increment total by number and you get the average by dividing total by counter. Your calculation for range is appropriate based on your definition of range in the instructions.
The algorhithm for use with a container depends somewhat on the type of container you use, so you would have to tell us that before we could offer advice.
-
1) the number of integers in the series (not including zero)
2) the average of the integers
3) the largest integer in the series
4) the smallest integer in the series
5) the range (difference between largest and smallest integer)
1)everytime a number that is not 0 is inputed
counter++;
2)keep a running total
total += number;
then when the user inputs 0
average = total/counter;
3-5...you got it right
-
main returns int... look in the FAQ for why.
-
You are also using old headers.
should be
and there is no need for this particular program to have the string and iomanip librarys included either
-
On a side note, you could call a function(s) to calculate and return the largest / smallest of three entered numbers. Finding the avergage of them is just like nextus told you above.
-
I got this, but theres two problems. It repeats the same statement everytime you enter a number, and when you enter 0 the program closes.
Code:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
int main() {
int number;
double average;
double counter = 0;
int total = 0;
int smallest = 130000;
int largest;
int range;
do{
cout << "Please enter a series of numbers, enter 0 to display the information " << '\n';
cin >> number;
if (number != 0) {
largest = number;
smallest = number;
}
}
while (number != 0); {
counter++;
average = total/counter;
if (number > largest) largest = number;
if (number < smallest) smallest = number;
range = largest - smallest;
total += number;
}
cout << "The number of integers you entered was" << counter << '\n';
cout << "The average of the integers is" << average << '\n';
cout << "The largest integer in the series is" << largest << '\n';
cout << "The smallest integer in the series is" << smallest << '\n';
cout << "The difference between the largest integer and smallest integer is" << range << '\n';
return 0;
}
-
To find out why the program is closing when it's done, read the FAQ.
It repeats the same statement because that's what you're telling it to do. You have a do...while loop, but I think you're confusing that with the while loop you only think you have below. What you really have is a do...while loop, then a block of code that is always executed. Do you see why?
[edit]Why is it kind of hard to see? Because your indentation format sucks. :) [/edit]
-
I have to use a DO WHILE loop.
-
That's fine. What I said was you're confusing (I think) what needs to go in the do...while loop and what should go after it.
Run through the code on paper; when does the do...while loop terminate? What happens once it does? Is that what you want your program to do?