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
#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;    
}
Comments