Answer to Question #149458 in C++ for Jochelle Buenaventura

Question #149458
Create a program using two-dimensional arrays that determines the highest and lowest of the 12 input values.
Sample input/output dialogue:
Enter twelve numbers:
13 15 20 13 35 40 16 18 20 18 20 14
highest: 40
lowest: 13
1
Expert's answer
2020-12-07T17:04:45-0500
#include <iostream>

int main()
{
    const int ARRAY_DIMENSION_1 = 2;
    const int ARRAY_DIMENSION_2 = 6;
    int buffer[ARRAY_DIMENSION_1][ARRAY_DIMENSION_2] = {};
    
    std::cout << "Enter twelve numbers:\n";

    int min;
    int max;
    
    for(int i=0; i<ARRAY_DIMENSION_1; ++i)
    {
        for(int j=0; j<ARRAY_DIMENSION_2; ++j)
        {
            std::cin >> buffer[i][j];

            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }
            
            if(i == 0 && j == 0)
            {
                min = buffer[0][0];
                max = buffer[0][0];
            }
            else
            {
                if(min > buffer[i][j])
                {
                    min = buffer[i][j];
                }

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

    std::cout << "highest: " << max << "\nlowest: " << min << "\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