COSC 1436 C++

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

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;

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 the stream manipulators like setw and setprecision.

#include <iomanip>

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

#include<cmath>

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.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 floor-space area of the two rooms.

(width1*length1)+(width2*length2)

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.

1. #include<iostream> 2. #include<cmath> 3. using namespace std; 4. 5. int main() 6.{ 7. 8. int variable; 9. 10. cin>>variable; 11. cout<<pow(variable, 2.0); 12. }

Assume that a,b and c are char variables have been declared. Write some code that reads in the first character of the next line into a, the first character of the line after that into b and the first character of the line after that into c. Assume that the lines of input are under 100 characters long.

1. cin.ignore(100,'\n')>>a; 2. cin.ignore(100,'\n')>>b; 3. cin.ignore(100,'\n')>>c;

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

1. string line; 2. 3. getline(cin,line);

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

bridgePlayers+=4;

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

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;

Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa") Write some code that reads a value into name then prints the message "Greetings, NAME!!!" where NAME is replaced the value that was read into name. For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!".

cin>>name; cout<<"Greetings, "; cout<<name<<"!!!";

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 "; cout<<name<<" is "<<age<<".";

Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature. has already been declared as a 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 a statement using the decrement operator to decrease the value of count (an already declared integer variable) by 1.

count--;

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)/(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)/(speed)

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)

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; string s; cin>>k; cin>>d; cin>>s; cout<<s<<" "<<d<<" "<<k<<endl; cout<<k<<" "<<d<<" "<<s<<endl;

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

prefix+suffix

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;

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*=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((pow(a,2))+(pow(b,2)))

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)

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;


संबंधित स्टडी सेट्स

Quiz 1 - Cardiovascular Training

View Set

leadership and management chapter 1

View Set

Lesson 3: Ratifying the Constitution

View Set

integrated math unit 7 geometric movement

View Set

TEST 3: Macroeconomics Chap. 12: AD-AS

View Set