Answer to Question #261816 in C++ for Neda

Question #261816

Use the "for" loop with "if" to write a program that asks the user to type 15 integers and displays :

1. The smallest of these integers.

2. The largest of these integers


1
Expert's answer
2021-11-05T18:00:21-0400
#include <iostream>

int main()
{
    int min, max;

    std::cout << "Input 15 integers: ";

    for(int i = 0; i < 15; ++i)
    {
        int tmp;
        std::cin >> tmp;

        if(!std::cin)
        {
            std::cout << "Error: invalid data\n";
            return 1;
        }

        if(i == 0)
        {
            min = max = tmp;
        }
        else
        {
            if(tmp < min) min = tmp;
            if(tmp > max) max = tmp;
        }
    }

    std::cout << "The smallest of integers is " << min << "\n";
    std::cout << "The largest of integers is  " << max << "\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