I have an assignment due tomorrow that deals with a previous assignment. The previous assignment was a program that asked the user how many numbers they wished to enter, proceeded to ask them for the numbers, then calculated the sum, average, largest and smallest of the inputted numbers. Here is my code for that assignment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int UserInput, number, i;
int sum = 0;
foat average = 0;
int largest = 0;
int smallest = 0;
cout << "How many numbers do you wish to enter? ";
cin >> UserInput;
for (i = 0; i < UserInput; i++)
{
cout << "Enter a number: ";
cin >> number;
sum = sum + number;
average = float(sum)/UserInput;
if (number > largest)
largest = number;
if (!i || smallest > number)
smallest = number;
}
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;
getch();
return 0;
}
|
The assignment that's due tomorrow deals with this same program. These are the directions:
Ask the user for a series of numbers between 0 and 100. This is similar to the program for today, but instead of asking the user how many numbers he/she wishes to enter, after each number ask if they'd like to enter another.
Validate the input. Make sure each number is in the range 0 to 100 before accepting it, and make sure the answer is 'y' or 'n' for yes or no.
Print out the sum, average, and largest and smallest of the numbers. Here is a sample of how your program should run:
Enter a number: 80
Would you like to enter another number (y or n)? y
Enter a number: 100
Would you like to enter another number (y or n)? y
Enter a number: 200
The number must be in the range 0 to 100.
Enter a number: -5
The number must be in the range 0 to 100.
Enter a number: 50
Would you like to enter another number (y or n)? y
Enter a number: 70
Would you like to enter another number (y or n)? n
You entered 4 numbers
The sum is 300
The average is 75.0
The largest is 100
The smallest is 50
I'm having a bit of trouble with this assignment. My teacher said to not change much from the original program. This is what I have for code, if anyone could help, I'd really appreciate it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int number = 0;
int count;
char key;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
cout << "Enter a number between 0 and 100: ";
cin >> number;
for (count = 0; count < number; count++)
{
do
{
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
key = tolower(key);
} while ( ! ( key == 'y' || key == 'n'));
cout << "Enter a number: ";
cin >> number;
if (number < 0 || number > 100)
{
cout << "Please enter a number between 0 and 100. ";
cin >> number;
}
sum = sum + number;
average = float(sum)/number;
if (number > largest)
largest = number;
if (!count || smallest > number)
smallest = number;
}
cout << "You've entered " << count << " numbers. ";
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;
getch();
return 0;
}
|