Chapter 3 Expressions and Interactivity

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized ).

val= val + 5;

Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision.

#include <iomanip>

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

#include <iostream> using namespace std; int main() { int random; cin >> random ; cout << random << " " ; cout << random * 2 << " " ; cout << random * random ; return 0; }

Write the necessary preprocessor directive to enable the use of functions like sqrt and sin.

#include<cmath>

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.

(small+large) * (small + large)

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.

(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

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.

(width1* length1) + (width2* length2)

Write the declaration of a char array named smallWord suitable for storing 4-letter words such as "love", "hope" and "care".

char smallWord[5];

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.".

cin >> name >> age; cout << "The age of " << name << " is " << age << ".";

Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable .

cin >> val;

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 .

cin.get(c);

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.

cin.ignore(20,'\n'); cin.get(c);

Declare an int constant MonthsInDecade whose value is the value of the constant MonthsInYear (already declared ) multiplied by 10.

const int MonthsInDecade = MonthsInYear * 10;

Declare an int constant , MonthsInYear, whose value is 12.

const int MonthsInYear=12;

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".

cout << price / 100 << " dollars and " << price % 100 << " cents";

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.

cout << setw (10) << m;

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).

cout << showpoint << x;

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.

int k; double d; char s[10]; cin >> k >> d >> s; cout << s << " " << d << " " << k << "\n" << k << " " << d << " " << s;

Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized ).

minutes_left -= 10;

Write a statement using the increment operator to increase the value of num_items (an already declared integer variable ) by 1.

num_items++;

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.

price/100

Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.

profits= profits* 10;

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.

sqrt(area)

Declare a string named line and write a statement that reads in the next line of standard input into this variable .

string line; getline (cin,line);


Ensembles d'études connexes

Life and Health Insurance (Florida) ExamFX Simulation Questions

View Set

Prep U Fundamentals of Nursing CH 9

View Set

AP Chemistry Semester 1 Test Questions

View Set

"The Children's Hour" - Karen Wright (ACT THREE)

View Set

"Primera Generación 1963-73": La celebración, según Litto Nebbia

View Set