COMP 150 MPL Chapter 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given a string variable word, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"

"(" + word + ")"

Given a string variable address, write a string expression consisting of the string "http://" concatenated with the variable 's string value . So, if the value of the variable were "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://" + address

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

#include <cmath>

Write the necessary preprocessor directive to enable the use of the rand function.

#include <cstdlib>

Write the necessary preprocessor directive to enable the use of the time function.

#include <ctime>

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 .

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

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 .

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

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

#include<iomanip>

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.

(1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + 1.0/5.0 + 1.0/6.0 + 1.0/7.0 + 1.0/8.0)

Write an expression that computes the average of the values 12 and 40.

(12+40)/2

Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values ).

(exam1+exam2)/2

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.

(small+large)

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)

Given an integer variable bridgePlayers, write an statement that increases the value of that variable by 4.

bridgePlayers+=4;

Given an int variable datum that has already been declared , write a statement that reads an integer value from standard input into this variable .

cin >> datum;

Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature. has already been declared as an double variable .

cin >> temperature;

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;

Write a statement that reads 5 successive integers into these variables that have already been declared : x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15

cin >> x1 >> x2 >> x3 >> x4 >> x5; cout << setw(5) << right << x1 << "\n"; cout << setw(5) << right << x2 << "\n"; cout << setw(5) << right << x3 << "\n"; cout << setw(5) << right << x4 << "\n"; cout << setw(5) << right << x5 << "\n";

Write an expression that attempts to read a double value from standard input and store it in an double variable , x, that has already been declared .

cin >> x;

Write an expression that attempts to read an integer from standard input and store it in an int variable , x, that has already been declared .

cin >> x;

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

Write a statement using the decrement operator to decrease the value of count (an already declared integer variable ) by 1.

count--;

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 x is a double variable that has been given a value . Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is.

cout << setprecision(3) << fixed << x;

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 << endl;

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;

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.

float (tickets1 + tickets2 + tickets3)/(class1 + class2 + class3)

Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community. Write an expression whose value is the average number of children per family.

float(children)/float(families)

Given two integer variables distance and speed, write an expression that divides distance by speed using floating point arithmetic, i.e. a fractional result should be produced.

float(distance)/float(speed)

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. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.

hn + 1.0/(n+1)

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. Assume that n is an integer variable whose value is some integer N greater than 1. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N-1)th harmonic number.

hn - 1.0 /(n)

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++;

Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized ).

num_rabbits*=4;

Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized ).

pay/=2;

Write an expression that concatenates the string variable suffix onto the end of the string variable prefix .

prefix+suffix

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

profits*=10;

If a right triangle has sides of length A, B and C and if C is the largest, then it is called the "hypotenuse" and its length is the square root of the sum of the squares of the lengths of the shorter sides (A and B). Assume that variables a and b have been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: write an expression for the length of the hypotenuse.

sqrt(a*a+b*b)

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)

The length of a rectangle is stored in a double variable named length, the width in one named width. Write an expression whose value is the length of the diagonal of the rectangle.

sqrt(length*length+width*width)

The area of a square is stored in a double variable named area. Write an expression whose value is length of the diagonal of the square.

sqrt(sqrt(area)*sqrt(area) + sqrt(area)*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);

Write a statement using a compound assignment operator to change val to the remainder when val is divided by 16 (val has already been declared and initialized )

val%=16;

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

val+=5;


Kaugnay na mga set ng pag-aaral

Chapter 38: Family-Centered Care of the Child During Illness and Hospitalization NCLEX

View Set

(Midterms) Chapter 5: Ethical Issues in Clinical Psychology

View Set

Chapter 6.1 and 6.2: Entrepreneurship

View Set

Income Taxation Chapter 1 (Part 1)

View Set

Chapter 18- Advertising PR and Sales Promotions

View Set

Chapter 17- Freedom's boundaries at home and abroad

View Set

Module 9 (Hormonal/Glucose Regulation A)

View Set

Social Psychology Final: Chapter 14 (Altruism)

View Set