Chapter 2: Problem 19
Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows:
Short Answer
Expert verified
The program should accept three integers, calculate sum, average, product, smallest, and largest, and display these values.
Step by step solution
01
Take Input
Prompt the user to input three integers. Store the inputs in three variables, for example, `num1`, `num2`, and `num3`.
02
Calculate Sum
Add the three numbers to calculate the sum: `sum = num1 + num2 + num3`.
03
Calculate Average
Divide the sum by 3 to get the average: `average = sum / 3`.
04
Calculate Product
Multiply the three numbers to get the product: `product = num1 * num2 * num3`.
05
Determine Smallest
Use comparison operators to find the smallest number among `num1`, `num2`, and `num3`.
06
Determine Largest
Use comparison operators to find the largest number among `num1`, `num2`, and `num3`.
07
Display Results
Print the sum, average, product, smallest, and largest values to the screen.
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
C++ Programming
C++ is a high-level programming language created by Bjarne Stroustrup as an extension of the C language. It gives developers the ability to create complex programs with a mix of procedural, object-oriented, and generic programming features. C++ is widely used for developing applications where performance and resource control are critical, such as game development, systems programming, and real-time simulation.
In the context of our exercise, C++ allows for user interaction through input and output operations, performs arithmetic calculations, and facilitates decision-making with comparison operators. This makes it an ideal language for creating a program that processes and compares numerical data, such as the one where you need to calculate the sum, average, product, smallest, and largest of three integers entered by a user.
In the context of our exercise, C++ allows for user interaction through input and output operations, performs arithmetic calculations, and facilitates decision-making with comparison operators. This makes it an ideal language for creating a program that processes and compares numerical data, such as the one where you need to calculate the sum, average, product, smallest, and largest of three integers entered by a user.
Integer Input and Output
C++ provides several methods for integer input and output, typically using streams like
The process of output involves displaying results back to the user. This is accomplished with
cin
and cout
from the iomanip
library. For integer input, you would use the cin
stream to request numbers from the user, often accompanied by a prompt message using cout
. As in the exercise, variables such as num1
, num2
, and num3
are declared to store these integer inputs.The process of output involves displaying results back to the user. This is accomplished with
cout
, enabling you to output text and variables to the screen. For instance, once you calculate the sum, average, product, smallest, and largest values, you can display them using cout
with appropriate messages for clarity. Arithmetic Calculations in C++
C++ supports basic arithmetic operations such as addition, subtraction, multiplication, and division, which are fundamental for processing numerical data.
In our exercise, you first need to implement the addition operation to calculate the sum of three integers using the
In our exercise, you first need to implement the addition operation to calculate the sum of three integers using the
+
operator. Then, you divide this sum by three to get the average, noting that division between integers will result in an integer result. For this, the /
operator is used. Similarly, the product is found by multiplying the three integers with the *
operator.Handling Division for Average
One improvement tip is to be cautious with integer division when calculating the average, as it truncates any decimal part. To obtain a decimal average, one or more of the operands should be cast to a floating-point type before division.Comparison Operators in C++
Comparison operators in C++ allow you to compare values and determine relational conditions. These operators include
In our example, comparison operators are vital to find the smallest and largest numbers from the user inputs. By comparing the variables
==
(equals), !=
(not equals), <
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to).In our example, comparison operators are vital to find the smallest and largest numbers from the user inputs. By comparing the variables
num1
, num2
, and num3
with each other using <
and >
operators, you can determine which is the smallest and largest. Achieving this often involves a series of if
or else if
statements.