In: Computer Science
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers
PS use file I/o and input error checking methods
#include<iostream>
using namespace std;
//Error Check Method to check if the data entered is an integer or not
bool isNumeric(string str) {
for (int i = 0; i < str.length(); i++)
if (isdigit(str[i]) == false)
return false; //when one non numeric value is found, return false
return true;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int array[10];
for (int i = 0; i < 10; ++i) {
string temp;
cin >> temp;
bool negative = false;
if (temp[0] == '-') {
temp.erase(temp.begin());
negative = true;
}
if (isNumeric(temp)) {
array[i] = stoi(temp);
if (negative)
array[i] = -1 * array[i];
} else {
cout << "Entered value is not a number. Try Again.\n";
i--;
}
}
int Max = array[0], Min = array[0], sum = 0;
for (int i = 0; i < 10; ++i) {
if (array[i] < Min) {
Min = array[i];
}
if (array[i] > Max) {
Max = array[i];
}
sum = sum + array[i];
}
cout << "The sum of maximum and minimum numbers is " << Max + Min << endl;
cout << "The average of the numbers is " << double(sum) / 10 << endl;
return 0;
}
INPUT.TXT
1
2
3
4
5
6
7
8
9
10
OUTPUT.TXT
The sum of maximum and minimum numbers is 11
The average of the numbers is 5.5
NOTE: Name the input file as input.txt and output file as output.txt