Question

In: Computer Science

write a c++ program that prompts a user to enter 10 numbers. this program should read...

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

Solutions

Expert Solution

#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


Related Solutions

Write a C program that prompts the user to enter three sets of five double numbers...
Write a C program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT