Chapter 3.2 – Mathematical Expressions C++ – Flashcards
Unlock all answers in this set
Unlock answersquestion
Write an expression that computes the average of the values 12 and 40.
answer
(12 + 40) / 2
question
Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values ).
answer
(exam1 + exam2) / 2.0
question
Write a complete program that
declares an integer variable ,
reads a value from the keyboard into that variable , and
writes to standard output the square of the variable 's value .
Besides the number, nothing else should be written to standard output .
answer
#include
#include
using namespace std;
int main()
{
int variable;
cin >> variable;
cout << pow ( variable, 2.0);
}
question
Write a complete program that
declares an integer variable ,
reads a value from the keyboard into that variable , and
writes to standard output the variable 's value , twice the value , and the square of the value , separated by spaces.
Besides the numbers, nothing else should be written to standard output except for spaces separating the values .
answer
#include
#include
using namespace std;
int main()
{
int variable;
cin >> variable;
cout << variable << " " << (2 * variable) << " " << pow(variable,2) << endl;
}
question
The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms.
answer
(width1 * length1) + (width2 * length2)
question
A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.
answer
large + small
question
Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable small. Similarly, the length of all the larger ones is the same and is stored in the variable large. Write a single expression whose value is the total area of this room. DO NOT use the pow function.
answer
(large + small) * (large + small)
question
Three classes of school children are selling tickets to the school play. The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables :tickets1, tickets2, tickets3 and class1, class2, class3. Write an expression for the average number of tickets sold per school child.
answer
float(tickets1 + tickets2 + tickets3) / ( class1 + class2 + class3)