473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

I need help with a program

14
"Wrire 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)

All I have is this so far, i'm stuck.

#include <iostream.h>
#include <string.h>
#include <iomanip.h>

main()
{
int number;
int number2;
int number3;
int total;
int average;
int counter;
int largest;
int smallest = 130000;
int difference;
int addition;

do
{
cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;

if (number != 0)
{
Oct 28 '06 #1
17 2622
jessy
106 100+
Sure You must be stuck since your first line in the program is wrong !!
... cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;

how can you request a series of integers from a user and then expects only one !!

you can't say cin>>number ; coz this will accepts only the first number that will be entered and ignores the rest ??

instead you should say cout<<"how many numbers ?"<<endl;
cin>>n;
cout<<"plz enter them "<<endl;
For (int i=0;i<n;i++)
cin>>a[i] ;

where n is their number ( 12 number 4 example) and a[i] is the array at which you will store these numbers ...
i hope you get that small part ...if u need more help .......post !
Oct 28 '06 #2
JDK721
14
Are you sure it will only accept one number? I saw someone else do it like that, and they finished the program.. and it worked!

By the way, im writing this program using a do while loop.
Oct 28 '06 #3
jessy
106 100+
Of course Using a do while is a different matter ...

using any loop will do the job ... for ,while , do..while ,...
so what is your problem now ??
Oct 28 '06 #4
Ganon11
3,652 Expert 2GB
"Wrire 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)

All I have is this so far, i'm stuck.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <iomanip.h>
  4.  
  5. main()
  6. {
  7.  int number;
  8.  //int number2;          I see no reason for these two variables from your problem specification. 
  9.  //int number3;
  10.  int total;
  11.  int average; // This should probably be a double, so you can get an accurate average
  12.  int counter;
  13.  int largest;
  14.  int smallest = 130000;          // Why do you initialize smallest, but not largest?  And why 130000?
  15.  int difference;
  16.  //int addition;         Again, I don't see a use for this.
  17.  
  18. do
  19.  {
  20.   cout << "Enter a series of integers, enter 0 when you're done." << '\n';
  21.   cin >> number;
  22.  
  23.   if (number != 0)
  24.   {
Actually, your code looks somewhat OK from what I can see. The best way to solve this problem is with a while loop - the condition will be (number != 0). In order for this to work, you will have to prompt the user for input once, before the loop begins, and at the very end of your loop.

Before the loop, it might be a good idea to set largest AND smallest to the value of number. This way, while inside your loop, it will be easy to set new values to smallest and largest with a pair of if statements:

Expand|Select|Wrap|Line Numbers
  1. if (number > largest) largest = number;
  2. if (number < smallest) smallest = number;
Finally, make sure you initialize all remaining variables, so that you won't get garbage values from them.

Good luck!
Oct 28 '06 #5
JDK721
14
Actually, your code looks somewhat OK from what I can see. The best way to solve this problem is with a while loop - the condition will be (number != 0). In order for this to work, you will have to prompt the user for input once, before the loop begins, and at the very end of your loop.

Before the loop, it might be a good idea to set largest AND smallest to the value of number. This way, while inside your loop, it will be easy to set new values to smallest and largest with a pair of if statements:

Expand|Select|Wrap|Line Numbers
  1. if (number > largest) largest = number;
  2. if (number < smallest) smallest = number;
Finally, make sure you initialize all remaining variables, so that you won't get garbage values from them.

Good luck!
I initiliazed the smallest to 130,000 because its unlikely someone will enter 130,000.

I'm just having problems with figuring out how to do all this, how to figure out how to make it know the largest number, the average, etc.
Oct 28 '06 #6
JDK721
14
I got this so far, I don't know what else to do!

#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);

if (number > largest) largest = number;
if (number < smallest) smallest = number;

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';
} while (number != 0);

return 0;
}
Oct 28 '06 #7
jessy
106 100+
You want to know how to make it know the largest and the average ??!!

this can't be done unless you have all the numbers the user entered in an array so as to compare each number with the previous one and get the smallest .... that's the idea snd try to figure it out yourself ...i know its not easy bs you have to work a bit hard to reach

hint : use the array and assume that the largest number is the first one for example and then compare it with all other numbers , if there exist ine larger then that is the largest up till now

ex. Max=a[0] where a[0] is the first number
if (a[i] >max ) then max=a[i];

try it and post if u face troubles
Oct 28 '06 #8
JDK721
14
You want to know how to make it know the largest and the average ??!!

this can't be done unless you have all the numbers the user entered in an array so as to compare each number with the previous one and get the smallest .... that's the idea snd try to figure it out yourself ...i know its not easy bs you have to work a bit hard to reach

hint : use the array and assume that the largest number is the first one for example and then compare it with all other numbers , if there exist ine larger then that is the largest up till now

ex. Max=a[0] where a[0] is the first number
if (a[i] >max ) then max=a[i];

try it and post if u face troubles
I'm not supposed to use arrays at all. This is how im supposed to do it.

I gotta figure out what to initialize all the int's to.
Oct 28 '06 #9
jessy
106 100+
what a very very strange assignment !!!

what's your point of this strange program .......

since you assigned the smallest to 130000 or whatever ...you can assign the largest to one miliion then or 3*smallest ( 3 times the smallest) and the average (smallest+largest /2) but what the hell is that ????

plz if you got the answer from anywhere plz tell me coz im very curious to know what is going on
Oct 29 '06 #10
JDK721
14
what a very very strange assignment !!!

what's your point of this strange program .......

since you assigned the smallest to 130000 or whatever ...you can assign the largest to one miliion then or 3*smallest ( 3 times the smallest) and the average (smallest+largest /2) but what the hell is that ????

plz if you got the answer from anywhere plz tell me coz im very curious to know what is going on
It's in a book, introduction to computer science using C++
Oct 29 '06 #11
Ganon11
3,652 Expert 2GB
"Wrire 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)
From your problem specification, here's the pseudocode I imagine:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.    int num;
  6.    double ave;
  7.    int count = 0, sum = 0, smallest, largest;
  8.  
  9.    cout << "Please enter a number, or 0 to quit: ";
  10.    cin >> num;
  11.  
  12.    if (num != 0) {
  13.       largest = num;
  14.       smallest = num;
  15.    }
  16.  
  17.    while (num != 0) {
  18.       count++;
  19.       if (num is larger than largest) largest = num;
  20.       if (num is smaller than smallest) smallest = num;
  21.       add num to sum;
  22.       cout << "Please enter a number, or 0 to quit: ";
  23.       cin >> num;
  24.    }
  25.  
  26.    //Display relevant information here
  27.    //Calculate average by dividing sum by count
  28.    //You will have to cast either sum or count to a double type in order to get
  29.    // a decimal average.
  30.    //Range can be calculated by subtracting smallest from largest
  31.  
  32.    return 0;
  33. }
Oct 29 '06 #12
JDK721
14
Okay, I got this. But this is a while loop, I need to write the program in a do while. And my problem with this is, it only lets you enter ONE number, and after you enter a number nothing happens!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string.h>
  3. #include <iomanip.h>
  4.  
  5. int main() {
  6.    int number;
  7.    double average;
  8.    double counter = 0; 
  9.    int total = 0;
  10.    int smallest = 130000;
  11.    int largest;
  12.    int range;
  13.  
  14.    cout << "Please enter a series of numbers, enter 0 to display the information " << '\n';
  15.    cin >> number;
  16.  
  17.    if (number != 0) {
  18.       largest = number;
  19.       smallest = number;
  20.    }
  21.  
  22.    while (number != 0) {
  23.       counter++;
  24.       average = total/counter;
  25.       if (number > largest) largest = number;
  26.       if (number < smallest) smallest = number;
  27.       range = largest - smallest;
  28.       total += number;
  29.      }
  30.  
  31.      cout << "The number of integers you entered was" << counter << '\n';
  32.      cout << "The average of the integers is" << average << '\n';
  33.      cout << "The largest integer in the series is" << largest << '\n';
  34.      cout << "The smallest integer in the series is" << smallest << '\n';
  35.      cout << "The difference between the largest integer and smallest integer is" << range << '\n';
  36.      return 0;
  37. }
Oct 29 '06 #13
jessy
106 100+
TRY THIS

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string.h>
  3. #include <iomanip.h>
  4.  
  5. int main() {
  6.    int number;
  7.    double average;
  8.    double counter = 0; 
  9.    int total = 0;
  10.    int smallest = 130000;
  11.    int largest;
  12.    int range;
  13.   DO{
  14.    cout << "Please enter a series of numbers, enter 0 to display the information " << '\n';
  15.    cin >> number;
  16.  
  17.  
  18.    if (number != 0) {
  19.       largest = number;
  20.       smallest = number;
  21.    }
  22.                 }
  23.    while (number != 0) {
  24.       counter++;
  25.       average = total/counter;
  26.       if (number > largest) largest = number;
  27.       if (number < smallest) smallest = number;
  28.       range = largest - smallest;
  29.       total += number;
  30.      }
  31.  
  32.      cout << "The number of integers you entered was" << counter << '\n';
  33.      cout << "The average of the integers is" << average << '\n';
  34.      cout << "The largest integer in the series is" << largest << '\n';
  35.      cout << "The smallest integer in the series is" << smallest << '\n';
  36.      cout << "The difference between the largest integer and smallest integer is" << range << '\n';
  37.      return 0;
  38. }
[/quote]
Oct 29 '06 #14
JDK721
14
Well, it didn't work.

1. It repeats the statement everytime you enter a number
2. When you enter 0 the program closes
Oct 29 '06 #15
Ganon11
3,652 Expert 2GB
Well, the reason nothing happens is because you only ask for user input once. You have to ask for it again at the end of your while loop - otherwise, the end condition will never be satisfied!

Now, in order to use a do...while loop...that'll be a little weird. But apply the following changes:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string.h>
  3. #include <iomanip.h>
  4.  
  5. int main() {
  6.    int number;
  7.    double average;
  8.    double counter = 0; 
  9.    int total = 0;
  10.    int smallest;    // Does not need to be initialized.
  11.    int largest;
  12.    int range;
  13.  
  14.    cout << "Please enter a number, or 0 to display the information: ";
  15.    cin >> number;
  16.  
  17.    if (number != 0) {
  18.       largest = number;
  19.       smallest = number;
  20.       do {
  21.          counter++;
  22.          if (number > largest) largest = number;
  23.          if (number < smallest) smallest = number;
  24.          total += number;
  25.          cout << "Please enter a number, or 0 to display the information: ";
  26.          cin >> number;
  27.       } while (number != 0);
  28.    }
  29.  
  30.    average = static_cast<double>(total) / count;
  31.    range = largest - smallest;  
  32.    cout << "The number of integers you entered was" << counter << '\n';
  33.    cout << "The average of the integers is" << average << '\n';
  34.    cout << "The largest integer in the series is" << largest << '\n';
  35.    cout << "The smallest integer in the series is" << smallest << '\n';
  36.    cout << "The difference between the largest integer and smallest integer is" << range << '\n';
  37.      return 0;
  38. }
Oct 29 '06 #16
JDK721
14
Still the same problems, it repeats the same statement over and over everytime you enter a number.. and when you enter 0 the program closes!
Oct 29 '06 #17
Ganon11
3,652 Expert 2GB
Well, the repetition is expected and good - it means the program is taking these values and actually calculating them. The reason it is exiting...well...I didn't think you needed it, since you weren't including it with all your other samples, but put this statement in the line right before return 0;

Expand|Select|Wrap|Line Numbers
  1. system("PAUSE");
This will pause the program so you can view the output, displaying a message like, "Press any key to continue . . ."
Oct 30 '06 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Spamtrap | last post by:
I only do occasional Perl programming and most things I write are short processes. I have something I'm working on that is scanning a text file with about 15 million lines and trying to extract...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
1
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
1
by: raghavshastri | last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity. Here is a sample PGM image with 10...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.