Computer Programming I - Chapter 3

Ace your homework & exams now with Quizwiz!

Assume that the following variables are defined: int age; double pay; char section; Write a single cin statement that will read input into each of these variables.

cin >> age >> pay >> section;

The __________ file must be included in a program that uses the mathematical functions.

cmath

The ______ library function returns the natural logarithm of a number.

log

The ______ library function returns the base-10 logarithm of a number.

log10

The ______ library function returns the sine of an angle.

sin

The ______ library function returns the square root of a number.

sqrt

Assume that qty and salesReps are both intergers. Use a type cast expression to rewrite the following statement so it will no longer perform integer division. unitsEach = qty / salesReps;

unitsEach = static_cast<double>(qty) / salesRep;

The ______ library function returns the exponential function of a number.

exp

Complete the following table by writing statements with combined assignment operators in the right-hand column. The statements should be equivalent to the statements in the left-hand column. STATEMENTS WITH ASSIGNMENT OPERATOR: x = x + 5; total + total + subtotal; dist = dist / rep; ppl = ppl * period; inv = inv - shrinkage; num = num % 2;

STATEMENTS WITH COMBINED ASSIGNMENT OPERATOR x += 5; total += subtotal; dist /= rep; ppl *= period; inv -= shrinkage; num %= 2;

The ______ library function returns the remainder of floating point division.

fmod

What header files must be included in the following program? int main ( ) { double amount = 89.7; cout << showpoint << fixed; cout << setw(8) << amount << endl; return 0; }

iostream and iomanip

Each of the following programs has some errors. Locate as many as you can. using namespace std; int main ( ) { double number1, number2, sum; Cout << "Enter a number: "; Cin << number1; Cout << "Enter another number : "; Cin << number2; number1 + number2 = sum; Cout "The sume of the two numbers is " << sum return 0; }

#include <iostream> is missing. Each cin and cout statement starts with capital C. The << operator is mistakenly used with cin. The assignment statement should read: sum = number1 + number2; The last statement should have << after cout. The last statement is missing a semicolon.

What will each of the following programs display? (Some should be hand traced and require a calculator.) #include <iostream> using namespace std; int main ( ) { long x, y, z; x = y = z = 4 x += 2; y-= 1; z *= 3; cout << x << " " << y << " " << z << endl; return 0; }

6 3 12

Assume a string object has been defined as follows: string description; A) Write a cin statement that reads in a one-word string. B) Write a statement that reads in a string that can contain multiple words separated by blanks.

A) cin >> description; B) getline(cin, description);

Assume a program has the following variable definitions: int a, b = 2; float c = 4.2; and the following statement: a = b * c; What value will be stored in a? A) 8.4 B) 8 C) 0 D) None of the above.

B) 8

Assume a program has the following variable definitions: int units; float mass; double weight; and the following statement: weight = mass * units; Which automatic data type conversion will take place? A) mass is demoted to an int, units remain an int, and the result of mass * units is an int. B) units is promoted to a float, mass remains a float, and the result of mass * units is a float. C) units is promoted to a float, mass remains a float, and the result of mass * units is a double.

C) units is promoted to a float, mass remains a float, and the result of mass * units is a double.

Write a pseudocode algorithm for a program that calculates the total of a retail sale. The program should ask for the amount of the sale and the sales tax rate. The sales tax rate should be entered as a floating-point number. For example, if the sales tax rate is 6 percent, the user should enter 0.06. The program should display the amount of sales tax and the total of the sale. After you write the pseudocode algorithm, convert it in to a complete C++ program.

Display "Enter the amount of the sale: ". Read saleAmount. Display "Enter the sales tax rate : ". Read taxRate. salesTax = saleAmount * taxRate. saleTotal = saleAmount + salesTax. Display "The sales tax is $". Display salesTax. Display "The sale total is $". Display saleTotal. ********************************************** #include <iostream> using namespace std; int main() { double saleAmount, taxRate, salesTax, totalSale; cout << "Enter the amount of the sale: "; cin >> saleAmount; cout << "Enter the sales tax rate: "; cin >> taxRate; salesTax = saleAmount * taxRate; totalSale = saleAmount + salesTax; cout << "The sales tax is $" << salesTax << endl; cout << "The sale total is $" << totalSale << endl; return 0; }

Write a pseudocode algorithm for a program that asks the user to enter a golfer's score for three games of golf, and then displays the average of the three scores. After you write the pseudocode algorithm, convert it in to a complete C++ program.

Display "Enter the bowler's score for the 1st game: ". Read score1. Display "Enter the bowler's score for the 2nd game: ". Read score2. Display "Enter the bowler's score for the 3rd game: ". Read score3. averageScore = (score1 + score2 + score3) / 3. Display "The bowler's average score is : ". Display averageScore. ******************************************** #include <iostream> using namespace std; int main() { int score1, score2, score3, averageScore; cout << "Enter the bowler's score for the 1st game: "; cin >> score1; cout << "Enter the bowler's score for the 2nd game: "; cin >> score2; cout << "Enter the bowler's score for the 3rd game: "; cin >> score3; averageScore = (score1 + score2 + score3) / 3; cout << "The bowler's average score is :"; cout << averageScore << endl; return 0; }

A retail store grants its customers a maximum amount of cred. Each customer's available credit is his/her maximum amount of credit minus the amount of credit used. Write a pseudocode algorithm for a program that asks for a customer's maximum amount of credit and amount of credit used. The program should then display the customer's available credit. After you write the pseudocode algorithm, convert it in to a complete C++ program.

Display "Enter the customer's maximum amount of credit: ". Read maxCredit. Display "Enter the amount of credit the customer has used: ". Read creditUsed. availableCredit = maxCredit - creditUsed. Display "The customer's available credit is $". Display availableCredit. #include <iostream> using namespace std; int main() { double maxCredit, creditUsed, availableCredit; cout << "Enter the customer's maximum amount of credit: "; cin >> maxCredit; cout << "Enter the amount of credit used by the customer: "; cin >> creditUsed; availableCredit = maxCredit - creditUsed; cout << "The customer's available credit is $"; cout << availableCredit << endl; return 0; }

What will each of the following programs display? (Assume the user enters George Washington.) #include <iostream> #include <iomanip> #include <string> using namespace std; int main ( ) { string userInput; cout << "What is your name? "; getline(cin, userInput); cout << "Hello " << userInput << endl; return 0; }

Hello George Washington

What will each of the following programs display? (Some should be hand traced and require a calculator.) (Assume the user enters 36720152. Use a calculator.) #include <iostream> #include <iomanip> using namespace std; int main ( ) { long seconds; double minutes, hours, days, months, years; cout << "Enter the number of seconds that have\n"; cout << "elapsed since some time in the past and\n"; cout << "I will tell you how many minutes, hours,\n"; cout << "days, months, and years have passed: "; cin >> seconds; minutes = seconds / 60; hours = minutes / 60; days = hours / 24; years = days / 365; months = years * 12; cout << setprecision(4) << fixed << showpoint << right; cout << "Minutes: " << setw(6) << minutes << endl; cout << "Hours: " << setw (6) << hours << endl; cout << "Days: " << setw (6) << days << endl;

Minutes: 612002.0000 Hours: 10200.0332 Days: 425.0014 Months: 13.9726 Years: 1.1644

Each of the following programs has some errors. Locate as many as you can. #include <iostream> using namespace std; int main ( ) { int number1, number2; float quotient; cout << "Enter two numbers and I will divide\n"; cout << "the first by the second for you.\n"; cin >> number 1, number2; quotient = float<static_cast>(number1) / number2; cout << quotient return 0; }

The first cin statement should read: cin >> number1 >> number2; The assignment statement should read: quotient = static_cast<float>(number1) / number2; The last statement is missing a semicolon.

Each of the following programs has some errors. Locate as many as you can. #include <iostream>; using namespace std; int main ( ) { const int number1, number2, product; cout << "Enter two numbers and I will multiply\n"; cout << "them for you.\n"; cin >> number1 >> number 2; product = number1 * number2; cout << product return 0; }

The variables should not be declared const. The last statement is missing a semicolon.

Each of the following programs has some errors. Locate as many as you can. #include <iostream>; using namespace std; main { double number, half; cout << "Enter a number and I will divide it\n" cout << "in half for you.\n" cin >> number1; half =/ 2; cout << fixedpoint << showpoint << half << endl; return 0; }

There shouldn't be a semicolon after the #include directive. The function header for main should read: int main() The first two cout statements should end with a semicolon. The variable number1 is used, but never defined. The combined assignment operator is improperly used. The statement should read: half /= 2; There is also a logical error in the program. The value divided by 2 should be number1, not half. The following statement: cout << fixedpoint << showpoint << half << endl; should read: cout << fixed << showpoint << half << endl;

Each of the following programs has some errors. Locate as many as you can. #include <iostream>; using namespace std; main { int number1, number2; cout << "Enter two numbers and I will multiply\n" cout << them by 50 for you.|n" cin >> number1 >> number 2; number1 = 50; number2 =*50; cout << number1 << " " << number2; return 0; }

There shouldn't be a semicolon after the #include directive. The function header for main should read: int main() The combined assignment operators improperly used. Those statements should be: number1 *= 50; number2 *= 50;

Each of the following programs has some errors. Locate as many as you can. #include <iostream>; using namespace std; int main ( ) { char name, go; cout << "Enter your name: "; getline >> name; cout << "Hi " << name << endl; return 0; }

There shouldn't be a semicolon after the #include directive. name should be declared as an array. The following statement: cin.getline >> name; should read: cin >> name;

Complete the following table by writing the value of each expression in the Value Column EXPRESSION: 28 / 4-2 6 + 12 * 2 - 8 4 + 8 * 2 6 + 17 % 3 - 2 2 + 22 * (9 - 7 ) (8 + 7) * 2 (16 + 7) % 2 - 1 12 / (10 - 6) (19 - 3) * (2 + 2) / 4

VALUE: 5 22 20 6 46 30 0 3 16

What will each of the following programs display? (Some should be hand traced and require a calculator.) (Assume the user enters 38700. Use a calculator.) #include <iostream> using namespace std; int main ( ) { double salary, monthly; cout << "What is your annual salary? " ; cin >> salary; monthly = static_cast<int>(salary) / 12; cout << "Your monthly wages are " << monthly << endl; return 0; }

Your monthly wages are 3225.000000

Write C++ expressions for the following algebraic expressions: a = 12x z = 5x + 14y + 6k y+ x cubed g= h+ 12 over 4k c = a to the third over bsquared kcubed

a = 12 * x; z = 5 * x + 14 * y + 6 * k; y = pow(x, 4); g = (h + 12) / (4 * k); c = pow(a, 3) / (pow(b, 2) * pow(k, 4));

Rewrite the following variable definition so the variable is named constant. int rate;

const int RATE = 12;

The ______ library function returns the cosine of an angle.

cos

Write a cout statement so the variable totalAge is displayed in a field of 12 spaces, in a fixed point notation, with a precision of 4 decimal places.

cout << setw(12) << fixed << setprecision(4) << totalAge;

Write a cout statement so the variable population is displayed in a field of 12 spaces, left-justified, with a precision of 8 decimal places. The decimal point should always be displayed.

cout << setw(12) << left << showpoint << setprecision(8) << population;

Write a cout statement so the variable divSales is displayed in a field of 8 spaces, in fixed point notation, with a precision of 2 decimal places. The decimal point should always be displayed.

cout << setw(8) << fixed << showpoint << setprecision(2) << divSales;

Write a multiple assignment statement that can be used instead of the following group of assignment statements: east = 1; west = 1; north = 1; south = 1;

east = west = north = south = 1;

associativity

either left or right, or right to left

The ______ library function returns the value of a number raised to a power.

pow

expression

program statement that has a value

The ______ library function returns the tangent of an angle.

tan


Related study sets

PSYCH180 Chapter 4 (Employee Selection: Recruiting and Interviewing)

View Set

P17 Everday English - Social expressions (Headway Pre-Intermediate 5th edition- Unit 1)

View Set

Management of Patients with Upper Respiratory Tract Disorders

View Set

Traditions & Encounters Chapter 35

View Set

Unit 2 CH 15 Utilitarianism, John Stuart Mill

View Set