simple program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnoobie
    New Member
    • Oct 2006
    • 4

    simple program help

    Problem 1 - Min, Mean, Max

    Write a program that reads in successive integer values from the user. The user will indicate termination of these values with the sentinel value 0 (zero). After the program has read in the values, the program will output the minimum value entered, the maximum value entered, and the mean (average) of the values entered (to two decimal places). If the user does not enter any numbers before entering 0, the program will display the message "No numbers were read".


    Sample outputs:
    Please enter some integers, and a 0 (zero) to terminate:
    1 2 3 4 5 0
    The minimum number entered was 1
    The maximum number entered was 5
    The mean of the numbers was 3.00
    Press any key to continue . . .




    Please enter some integers, and a 0 (zero) to terminate:
    1 2 3 4 5 6 7 8 9 10 0
    The minimum number entered was 1
    The maximum number entered was 10
    The mean of the numbers was 5.50
    Press any key to continue . . .




    Please enter some integers, and a 0 (zero) to terminate:
    0
    No numbers were read.
    Press any key to continue . . .


    You may assume that the user enters a valid integer.




    this is what i have so far although i dont really have a clue what im doing help most appreciated:)


    #include <iostream>
    #include <cmath>
    using namespace std;

    int main ()

    {
    cout << "Please enter some integers, and a 0 (zero) to terminate:" <<endl;

    int num;

    do
    {
    int num;
    cin >> num;

    if (num =0)
    {
    cout << "No numbers were read." << endl;

    }

    else;
    }

    while (num !=0)
    {
    int minimum;
    int count = 0;

    count++; //Add one to count
    }

    cout << count << endl;

    return 0;
    }
  • cnoobie
    New Member
    • Oct 2006
    • 4

    #2
    okay ive done it up a little better now. hopefully this helps a bit

    #include <iostream>
    #include <iomanip>
    using namespace std;


    int main ()

    {

    cout << "Please enter some integers, and a 0 (zero) to terminate:" <<endl;
    cout << endl;

    int number;
    cin >> number;

    if (number == 0)
    {
    cout << endl;
    cout << "No numbers were read." << endl;
    cout << endl;
    }

    int min = 0;
    int max = 0;
    int mean = 0;
    int count = 0;

    while (number != 0);
    {

    int number;
    count ++;
    cin >> number;

    if (number == 0)
    {
    cout << count << endl;
    cout << endl;
    cout << "The minimum number entered was " << min << endl;
    cout << endl;
    cout << "The maximum number entered was " << endl;
    cout << endl;
    cout << "The mean of the numbers was " << mean / count << endl;
    cout << endl;
    }
    }
    return 0;
    }


    i am however stil not getting it to work if someone enters a zero after, and the min, max, and mean are hard to do to

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Don't declare

      int number;

      twice

      intialise min to INT_MAX and max to INT_MIN defined in limits.h then

      Code:
      if (number < min)
         min = number;
      gets the minimum and something similar for the maximum.

      To calculate the mean you will need to total all the numbers entered.

      count should only be incremented if number is non-zero

      put your calculations before getting a new number in the loop.

      Comment

      • cnoobie
        New Member
        • Oct 2006
        • 4

        #4
        okay i changed it to a for statement now and got the count to work and only have one cin >>

        now btw we cant use min, max functions. we have to create our own using if statements and stuff. sorry im really new to this.

        #include <iostream>
        #include <iomanip>
        using namespace std;


        int main ()

        {

        int number = 0;
        int count = 0;
        int min = 0;
        int mean = 0;
        int somenum = 0;
        int x;

        cout << "Please enter some integers, and a 0 (zero) to terminate:" <<endl;
        cout << endl;

        for (mean = 0; somenum = 1; somenum++)
        {

        cin >> number;

        if (number == 0)
        {
        cout << count - 1 << endl;
        cout << endl;
        cout << "The minimum number entered was " << min << endl;
        cout << endl;
        cout << "The maximum number entered was " << somenum - 1 << endl;
        cout << endl;
        cout << "The mean of the numbers was " << mean / (count - 1) << endl;
        cout << endl;
        }

        }

        return 0;
        }

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by cnoobie
          now btw we cant use min, max functions. we have to create our own using if statements and stuff. sorry im really new to this.
          If you read what I posted you will find I haven't used min() or max().

          You previous code had less errors than you latest code.

          Start with tring to input a set of numbers until 0 is input and printing out the value of the smallest number entered (i.e. forget about the mean and the max to start with and use the code snipit I posted).

          Comment

          Working...