 // This program will calculate the highest and the lowest,
// variable out of a set of numbers. Also, it is going to give
// the sum and the average out of this set.
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int number, average, amount, sum;
int variables = 0;

cout << " What is the amount of numbers that you want to sum ? ";
cin >> amount;

do
{
cout << "Enter the following number until you move into the next step: ";
cin >> number;

variables++;
sum += (number + number) / 2;
average = ( sum / amount);

} while (amount > variables);

if ()
cout << " The largest number that you entered was " << number << endl;

cout << " The amount of numbers that you decide to add was " << amount << endl;

cout << " The total sum of all the numbers that you entered is " << sum << endl;

cout << " The average of the numbers that you entered is " << average << endl;

return 0;
}

Write a C++ program that will first ask the user how many numbers they want to enter. Then, the program should allow the user to enter those numbers. Once all numbers are entered, the program should report the sum of all of the numbers, the average of all of the numbers, and the highest and lowest of all of the numbers that were entered.

I'm having a trouble with the last part, finding the lowest and highest value and I would like to get some help.

Why not? I provided code for everything except highest/lowest and explained the algorithm you could use to accomplish that.

Why isn’t it working?

All 4 Replies

Firstly, I'm confused how you're calculating sum on line 23.

Let's say the first run through the loop, I enter the number 5.

Then the sum will be (5 + 5) / 2 ... 5

Why not just have sum += number ?

Why multiple by 2 and then divide by 2? If you take any number, add it to itself, and then divide by 2, you're going to end up with the same number you started with.

Anyways ... that's a moot point :)

In terms of calculating the largest and smallest numbers ...

For the first run through the loop, set the value of the variables representing the largest and smallest to whatever was entered. Then, for each additional run through the loop, if the number entered is smaller than the current smallest, overwrite it, and if the number entered is larger than the current largest, overwrite it. If not, leave it alone. At the end, you'll end up with the latest values for the smallest and largest out of all the numbers that were entered.

Per our chat conversation, here's an example:

// This program will calculate the highest and the lowest,
// variable out of a set of numbers. Also, it is going to give
// the sum and the average out of this set.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int total_numbers, avg, highest, lowest;
int sum, numbers_entered = 0;

cout << " What is the amount of numbers that you want to sum ? ";
cin >> total_numbers;

do
{
// Prompt for a number
cout << "Enter the following number until you move into the next step: ";
cin >> number;

// Increment the number of numbers entered
numbers_entered++;

// Increment the sum of all numbers by the value of this latest number
sum += number;

// Calculate an average based on the numbers entered so far
avg = sum / numbers_entered;

// Calculate highest ...
highest = ...

// Calculate lowest ...
lowest = ...

} while (numbers_entered < total_numbers);

cout << " The largest number that you entered was " << highest << endl;
cout << " The smallest number that you entered was " << lowest << endl;
cout << " The amount of numbers that you decide to add was " << total_numbers << endl;
cout << " The total sum of all the numbers that you entered is " << sum << endl;
cout << " The average of the numbers that you entered is " << average << endl;
return 0;
} thanks for your help, however i haven't been able to fix the program

Why not? I provided code for everything except highest/lowest and explained the algorithm you could use to accomplish that.

Why isn’t it working?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.