0

The first part of the question is to write a program that:

prompts user to enter integer values and keep a running total.

#include<iostream>
using namespace std;

int main()
{
    int n, sum = 0;
    while(n != 0)
    {
        cout << "Enter number :";
        cin >> n;

        if(n <= 0)
            break;

        sum += n;
    }

and the second part is:

Modify the previous program to print out the largest and smallest number read in as well as the average. Also change the prompt to show the number of numbers still to be entered.

#include<iostream>
using namespace std;

int main()
{
    float n, sum=0.0, minimum=1.0, maximum=0.0, average;
    int i = 0, x;

    while(n != 0)
    {
        cout << "Enter number" << (i+1) << " :";
        cin >> n;

        if(n <= 0)
            break;

        sum += n;
        i++;

        if(n > maximum)
        {
            maximum = n;
        }

        if(n <= minimum)
        {
            minimum = n;
        }

        x = x + (i + 1);
    }

cout << "Total=" << sum << endl;

average = sum / x;

cout << "Average=" << average << endl;
cout << "Maximum=" << maximum << endl;
cout << "Minimum=" << minimum << endl;
return 0;
}

My problem is that I'm not able to compute the average and show the number of numbers still to be entered. Can anyone help please?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Umme
  • 23
  • 3
  • 9
  • 1
    Have you heard of indentation? Might help to make the code readable – Ed Heal Oct 17 '15 at 13:31
  • Seem to be working on a similar problem as http://stackoverflow.com/questions/33186635/determining-maximum-and-mean-of-an-user-provided-array – Ed Heal Oct 17 '15 at 13:32
  • What do you mean with "how to show the number of numbers still to be entered". EDIT: sry, I understand it now. But what is you problem with `cout<<"Enter number"<<(i+1)<<" :";` – JojOatXGME Oct 17 '15 at 13:35
  • (1)Write a program to read in a set of numbers and print out their average. The program will start by prompting the user for the number of numbers to be read in and will then prompt for the individual numbers with a prompt such as Enter Number 23 to indicate to the user which data item is currently being entered. Do something special when prompting for the last number. Note that there is no need to store all the individual numbers, it is sufficient to maintain a running total. – Umme Oct 17 '15 at 13:48
  • 2) Modify the previous program to print out the largest and smallest number read in as well as the average. Also change the prompt to show the number of numbers still to be entered. Here are the two parts of the question maybe you would now understand it better . – Umme Oct 17 '15 at 13:50
  • What is the value of `x` the first time `x = x + (i + 1);` is encountered? – David C. Rankin Aug 21 '22 at 05:53

2 Answers2

2

The primary issue is in your use of x while it is uninitialized leading to undefined behavior. Specifically, you declare x as a value with automatic storage duration:

    int i = 0, x;

Then on your first iteration through the loop, you encounter:

    x = x + (i + 1);

Where the value of x is accessed while it is uninitialized invoking undefined behavior. Lesson, always initialize your variables.

Next, you fail to validate the input of n. You must validate all input to ensure a valid value is received, and especially where a numeric conversion is involved. You can use the failure to enter a numeric value as an indication the user is done entering values in this case, but if(n <= 0) is insufficient in that case.

There is no real reason for x to begin with. Your loop variable i is declared prior to the loop and will survive the loop termination. You can simply use that to contain the number of values entered. You don't use average anywhere except in output, so there is no need for it as a separate variable.

Taking that into consideration and addressing the other issues discussed above, and after checking Why is “using namespace std;” considered bad practice?, you could do something similar to:

#include <iostream>

int main()
{
  float n = 0., sum = 0., minimum = 1., maximum = 0.;
  int i = 0, init = 0;
  
  std::cout << "enter numbers, any non-number when done:\n\n";
  
  for (;; i++)                  /* loop continually incrementing i */
  {
    std::cout << "Enter number " << i+1 << " : ";
    if (!(std::cin >> n)) {
      break;
    }
    if (!init) {                /* initialize min & max with 1st entry */
      minimum = maximum = n;
      init = 1;
    }

    sum += n;                   /* add n to sum */

    if (n > maximum) {          /* check/set new max */
        maximum = n;
    }

    if (n < minimum) {          /* check/set new min */
        minimum = n;
    }
  }

  if (init) {   /* check if valid value received */
    std::cout << "\nNo. values : " << i << "\n\n"
              << "Total   = " << sum << '\n'
              << "Average = " << sum / i << '\n'
              << "Maximum = " << maximum << '\n'
              << "Minimum = " << minimum << '\n';
  }
  else {
    std::cerr << "error: no valid input received.\n";
    return 1;
  }
}

(note: the use of the init variable as a flag to indicate whether valid input has been received and to initialize maximum and minimum to the first value entered. There is only a single std::cout needed for any one continual block of output -- regardless of how many lines are involved. See also C++: “std::endl” vs “\n” for subtle distinctions between the use of the two)

Example Use/Output

$ ./bin/sumavgmaxmin
enter numbers, any non-number when done:

Enter number 1 : 1.1
Enter number 2 : 2.2
Enter number 3 : 3.3
Enter number 4 : 4.4
Enter number 5 : done

No. values : 4

Total   = 11
Average = 2.75
Maximum = 4.4
Minimum = 1.1

There a many ways you can approach this problem, but following from your start, this was a logical way to implement it.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

Problem is that you divide sum to x. Why you need this x? i is your count of inputed object, just divide sum to i;
One more bug, you define variable n and dont set value, and you want to compare it with 0. It can work incorrect.

    #include<iostream>
    using namespace std;
    int main()
    {
        float n = 1,sum=0.0,minimum=1.0,maximum=0.0,average;
        int i=0;

        while(n!=0)
        {
            cout<<"Enter number"<<(i+1)<<" :";
            cin>>n;
            if(n<=0)
                break;
            sum+=n;
            i++;
            if(n>maximum)
            {
                maximum=n;
            }

            if(n<=minimum)
            {
                minimum=n;
            }
        }

        cout<<"Total="<<sum<<endl;

        average=sum/i;

        cout<<"Average="<<average<<endl;
        cout<<"Maximum="<<maximum<<endl;
        cout<<"Minimum="<<minimum<<endl;
        return 0;
    }
Gor
  • 2,808
  • 6
  • 25
  • 46