Answer to Question #155854 in C++ for zain ul abdeen

Question #155854

Write a program which takes 10 numbers from the user and store them in an array and find the average, Largest and smallest number in the array


1
Expert's answer
2021-01-15T05:55:41-0500
#include <iostream>

int main()
{
    const int ARRAY_SIZE = 10;
    int buffer[ARRAY_SIZE] = {};
    
    std::cout << "Enter " << ARRAY_SIZE << " numbers: ";

    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        std::cin >> buffer[i];

            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }
    }
    
    int min = buffer[0];
    int max = buffer[0];
    int avg = 0;

    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        if(min > buffer[i])
        {
            min = buffer[i];
        }

        if(max < buffer[i])
        {
            max = buffer[i];
        }
        
        avg += buffer[i];
    }    

    avg /= ARRAY_SIZE;

    std::cout << "Smallest number: " << min << "\n";
    std::cout << "Largest number:  " << max << "\n";
    std::cout << "Average:         " << avg << "\n";
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS