Chapter 3 Expressions and Interactivity – Flashcards

Unlock all answers in this set

Unlock answers
question
Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable .
answer
cin >> val;
question
Write the declaration of a char array named smallWord suitable for storing 4-letter words such as "love", "hope" and "care".
answer
char smallWord[5];
question
Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE." where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70.".
answer
cin >> name >> age; cout << "The age of " << name << " is " << age << ".";
question
Declare k, d, and s so that they can store an integer , a real number, and a small word (under 10 characters ). Use these variables to first read in an integer , a real number, and a small word and print them out in reverse order (i.e., the word, the real, and then the integer ) all on the same line, separated by EXACTLY one space from each other. Then, on a second line, print them out in the original order (the integer , the real, and the word), separated again by EXACTLY one space from each other.
answer
int k; double d; char s[10]; cin >> k >> d >> s; cout << s << " " << d << " " << k << "n" << k << " " << d << " " << s;
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 . Instructor Notes: Remember 1)include statement for iostream 2) namespace line 3) main function 4) declare and integer k 5)get k from the console (cin) 6) cout statement
answer
#include using namespace std; int main() { int random; cin >> random ; cout << random << " " ; cout << random * 2 << " " ; cout << random * random ; return 0; }
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
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
(small+large) * (small + large)
question
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number.
answer
(small+large) * (small + large)1.0 / 7.0 + 1.0 / 6.0 + 1.0 / 5.0 + 1.0 / 4.0 + 1.0 / 3.0 + 1.0 / 2.0 + 1 + 1.0/8.0
question
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the number of single dollars that would have to be paid.
answer
price/100
question
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents". So, if the value of price was 4321, your code would print "43 dollars and 21 cents". If the value was 501 it would print "5 dollars and 1 cents". If the value was 99 your code would print "0 dollars and 99 cents".
answer
cout << price / 100 << " dollars and " << price % 100 << " cents";
question
Declare an int constant , MonthsInYear, whose value is 12.
answer
const int MonthsInYear=12;
question
Declare an int constant MonthsInDecade whose value is the value of the constant MonthsInYear (already declared ) multiplied by 10.
answer
const int MonthsInDecade = MonthsInYear * 10;
question
Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.
answer
profits= profits* 10;
question
Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized ).
answer
val= val + 5;
question
Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized ).
answer
minutes_left -= 10;
question
Write a statement using the increment operator to increase the value of num_items (an already declared integer variable ) by 1.
answer
num_items++;
question
Assume that m is an int variable that has been given a value . Write a statement that prints it out in a print field of 10 positions.
answer
cout << setw (10) << m;
question
Assume that x is a double variable that has been initialized . Write a statement that prints it out, guaranteed to have a decimal point, but without forcing scientific (also known as exponential or e-notation).
answer
cout << showpoint << x;
question
A variable c of type char has been declared . Write the necessary code to read in the next character from standard input and store it in c, regardless of whether is a whitespace character .
answer
cin.get(c);
question
Declare a string named line and write a statement that reads in the next line of standard input into this variable .
answer
string line; getline (cin,line);
question
Assume that c is a char variable has been declared . Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long.
answer
cin.ignore(20,'n'); cin.get(c);
question
Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.
answer
#include
question
Write the necessary preprocessor directive to enable the use of functions like sqrt and sin.
answer
#include
question
The area of a square is stored in a double variable named area. Write an expression whose value is length of one side of the square.
answer
sqrt(area)
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New