Thread: Need help with a program

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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;

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    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
    nextus, the samurai warrior

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    main returns int... look in the FAQ for why.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You are also using old headers.

    Code:
    iostream.h
    should be

    Code:
    iostream
    and there is no need for this particular program to have the string and iomanip librarys included either

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    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;
    }

  10. #10
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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]
    Last edited by Decrypt; 10-29-2006 at 03:24 PM.
    There is a difference between tedious and difficult.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    I have to use a DO WHILE loop.

  12. #12
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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?
    Last edited by Decrypt; 10-29-2006 at 06:09 PM.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM