exercise 3 - william paterson universitycs.wpunj.edu/~ndjatou/cs2300-chapter3-solutions.pdfexercise...
TRANSCRIPT
Exercise 3.1
1.a
Variables: len (double) to hold the length of the rectangle
width (double) to hold its width
Pseudo code
1. Read the length of the rectangle into the variable len.
2. Read the width of the rectangle into the variable width.
3. Compute and print the perimeter of the rectangle.
4. Compute and print the area of the rectangle.
Flowchart
1.b. C++ Program
/*************************************************************************
Program to read the length and the width of a rectangle and
to compute its perimeter and area.
*************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout.setf(ios :: fixed);
cout.setf(ios :: showpoint);
double len, // to hold the length of the rectangle
width; // to hold its width
/*---------- read the width and the length of the rectangle ----------*/
cout << endl << “Enter the width of the rectangle (in inches):\t”;
cin >> width;
cout << endl << “Enter its length (in inches):\t”;
cin >> len;
Start
Read
len, width
Write
2 * (len + width)
Stop
Write
len * width
/*------------ compute and print its perimeter -----------------------*/
cout << setprecision(2);
Cout << endl << “Its perimeter is:\t”
<< 2 * ( len + width ) << “ inches”;
/*------------compute and print its area ----------------------------*/
cout << endl << “Its area is:\t” << len * width
<< “ square inches”;
return( 0 );
}
2.a
Variables: num ( int ) to hold the positive integer value
Pseudo code
1. Read the positive integer value into the variable num.
2. Compute and print its right-most digit: num % 10.
3. Compute its quotient in the division by 10 and store it into variable num: num = num / 10.
4. Compute and print the middle digit: num % 10.
5. Compute and print the first digit: num / 10.
Flowchart
Start
Read
num
num = num / 10
Write
num / 10
Stop
Write
num % 10
Write
num % 10
2.b. C++ Program
/*************************************************************************
Program to read a positive integer value and to print its digits in reverse order
*************************************************************************/
#include <iostream>
using namespace std;
int main()
{
int num; // to hold the positive integer value
/*---------- read the positive integer value ----------*/
cout << endl << “Enter a 3-digit integer value:\t”;
cin >> num;
cout << endl << The digits of this number are:\t”;
/*------- compute and print its right-most digit -----*/
cout << num % 10;
/*----- compute its quotient in the division by 10 ---*/
num = num / 10;
/*------- compute and print the middle digit ---------*/
cout << num % 10;
/*-------compute and print the first digit -----------*/
cout << num / 10;
return( 0 );
}
Exercise 3.2
a. num1 <= 5 c. 3 * num 2 > fnum + 2 e. num1 + 3 != num1 * 4 f. ch – ‘M’ = = 5
9 <= 5 3 * 5 > 12.50 + 2 9 + 3 != 9 * 4 3 = = 5
False 15 > 14.50 12 != 36 False
True True
b. 3 * num1 + 4 < num3 * 2 d. 2 * num1 + 12 = = 3 * num3
3 * 9 + 4 < 10 * 2 2 * 9 + 12 = = 3 * 10
27 + 4 < 20 18 + 12 = = 30
31 < 20 30 = = 30
False True
Exercise 3.3
A.
a. 2 * num1 - 5 >= 9 || fnum / 2 + 10 <= 6.5 b. num1 + num2 == num3 + 5 && 2 * num3 <= 4 * num2
2 * 9 – 5 >= 9 || 9 + 5 == 10 + 5 &&
18 – 5 >= 9 || 14 == 15 &&
13 >= 9 || False &&
True || False
True
c. ! (num1 + 5 <= 13) d. 3 * num1 > num1 + num2 && num1+ 3 >= 12
! ( 9 + 5 <= 13) 3 * 9 > 9 + 5 && 9 + 3 >= 12
! ( 14 <= 13) 27 > 14 && 12 >= 12
! False True && True
True True
e. ! ( num3 % 4 < 3) f. num1 - 5 >= num3 || num2 < 15 && num1 >= 9
!( 10 % 4 < 3) || 5 < 15 && 9 >= 9
!( 2 < 3) || True && True
! True || True
False True
B. Which of the following conditions are equivalent (that means have the same true value)?
a. num1 != 0, c. num1, and e. !(num1 == 0) are equivalent.
b. num1 == 0 and d. !num1 are equivalent.
C. a. age = = 45 b. height > 5.7 c. salary > 35000 && salary < 50000
d. ! (NumChild = = 3) e. (height = = 6.0) || (NumChild < 4)
f. !(age > 35) && ((NumChild = = 2) || (NumChild = = 3))
Exercise 3.4
1.
Variables num (int) to hold the integer value
a. Algorithm using Pseudo-Code
1. read the integer value into variable num.
2. If num > 0 then do the following:
2.a. print it with the message “POSITIVE”
3. Otherwise, do the following:
3.a. print it with the message “NEGATIVE”
Flowchart
False True
C++ Language Code Segment:
int num;
cin >> num;
if ( num > 0 )
cout << endl << num << “POSITIVE”;
else
cout << endl << num << “NEGATIVE”;
2.
Variables symb (char) to hold the character value
a. Algorithm using Pseudo-Code
1. read the character value into variable symb.
2. If (symb >= ‘0’ AND symb <= ‘9’) then do the following:
2.a. print it with the message “DIGIT”
3. Otherwise, do the following:
3.a. print it with the message “NOT A DIGIT”
Start
Read
num
num > 0
Write
num,
“POSITIVE”
Write
num,
“NEGATIVE”
Stop
Flowchart
False True
b. C++ Language Code Segment:
char symb;
cin >> symb;
if (symb >= ‘0’ && symb <= ‘9’)
cout << endl << symb << “\t is a digit”;
else
cout << endl << symb << “\t is not a digit”;
Exercise 3.5
Variables num (int) to hold the integer value
a. Algorithm using Pseudo-Code
1. read the integer value into variable num.
2. If num % 5 = 0 then do the following:
2.a. print it with the message “MULTIPLE OF 5”
3. Otherwise, do the following:
3.a. print it with the message “NOT MULTIPLE OF 5”
Start
Read
symb
(symb >= ‘0’ && symb <= ‘9’)
Write
symb,
“DIGIT”
Write
symb, “NOT A
DIGIT”
Stop
Flowchart
False True
b. C++ Language Code Segment:
int num;
cin >> num;
if ( num % 5 == 0 )
cout << endl << num << “MULTIPLE OF 5”;
else
cout << endl << num << “NOT MULTIPLE OF 5”;
Exercise 3.6
1. Assuming that all variables are properly defined and initialized, what are the error(s) in each of the following
program segments:
a) cin >> num;
if (num = 5) ---------------------> num == 5
sum = num + 4;
else
sum = num + 10;
Start
Read
num
num % 5 = 0
Write
num,
“MULTIPLE OF 5”
Write
num,
“NOT MULTIPLE of 5”
Stop
b) cin >> num;
if (num > 5 && <= 10) ---------------> num > 5 && num <= 10
cout << num + 15;
else
cout << num - 3;
c) cin >> num; cin >> num;
if (num < 15) if (num < 15)
result1 = 2 * num; ----------------> {
result2 = num + 20; result = 2 * num;
else ---------------- > result2 = num + 20;
result1 = 5 * num; }
result = result1 + result2; else
cout << “\nThe result is:\t” << result; reult1 = 5 * num;
result = result1 + result2;
cout << “\nThe result is:\t” << result;
2. Program trace
Input: 4 input: 20
Line # num Line # num
1 4 1 20
2 4 2 20
3 10 5 20
4 10 6 20
6 10
Output: num = 10 Output: num / 4 = 5
result = 20 result = 40
3.a. Variables:
num (int) to hold the value
Algorithm (in Pseudo-Code)
1. read the value into variable num
2. if the value is greater than 10 do the following:
2.a. subtract 2 from it.
3. else do the following:
3.a. add 3 to it
4. print the new value of variable num
5. multiply the new value of variable num by 5 and print the result.
Flowchart
False True
b. C++ Language Code Segment
int num;
cin >> num;
if (num > 10)
num = num - 2;
else
num = num + 3;
cout << num;
cout << num * 5;
Exercise 3.7 1.
a. b. c.
count = 0; count = 0;
while( count < 10 ) while(count < 10) count = 0;
{ { while(count < 10 )
cin >> num; count = 0; {
cout << 2 * num; cin >> num; cin >> num;
count = count + 1; cout << 2 * num; cout << 2 * num;
} count = count + 1; count = count + 1;
}
}
Start
Read
num
num > 10
Write
num
Write
num * 5
Stop
num = num + 3 num = num - 2
2.a Variables:
tempf (double) to hold a temperature in Fahrenheit.
TempCount (int) to count the temperatures
Pseudo-Code
1. Set the loop counter TemCount to 0.
2. As long as the loop counter TempCount is less than 50 do the following:
2.a. Read a temperature in Fahrenheit into variable tempf.
2.b. Convert it to Celsius and print the result.
2.c. Increment the loop counter TempCount by 1.
Flowchart
False True
Start
TempCount = 0
TempCount = TempCount + 1
TempCount < 50
Read
temf
Write
5.0 / 9 * (tempf – 32)
Stop
b. C++ Language Code Segment
double tempf; int TempCount = 0;
while (TempCount < 50)
{
cin >> tempf;
cout << endl << (5.0 / 9 * (tempf – 32));
TempCount = TempCount + 1;
}
Exercise 3.8
1.
a. b.
sum = 0; sum = 0;
count = 0; count = 0;
while (count < 10 ) while (count < 10 )
{ {
sum = 0; cin>>num;
cin >> num; sum = sum + num;
sum = sum + num; count = count + 1;
count = count + 1; }
}
2.a. Variables
testCount (int) to count the test scores
score (double) to hold a test score
totalScore (double) to hold the current total of the test scores
Pseudo-Code
1. Set the test score counter testCount to 0: testCount = 0.
2. set the current total of the test scores to 0: totalScore = 0.
3. As long testCount is less than 20, do the following:
3.a. Read a test score into variable score.
3.b. add the test score read to the current total of the test scores: totalScore = totalScore + score.
3.c. increment the test score counter by 1: testCount = testCount + 1.
4. Compute and print the average score, totalScore / 20.
Flowchart
False True
2.b. C++ Language Code Segment
int testCount = 0;
double score,
totalScore = 0;
while ( testCount < 20 )
{
cout << endl << “enter a test score:\t” ;
cin >> score;
totalScore = totalScore + score;
testCount = testCount + 1;
}
cout << endl << “the average test score is:\t” << totalScore / 20;
Start
totalScore = totalScore + score
testCount < 20
Stop
testCount = 0
Read
score
totalScore = 0
testCount = testCount + 1
Write
totalScore / 20
Exercise 3.9
Output: the sum of the first 10 positive multiples of 5
Input: no input
Variables:
sum (int) to hold the current sum of the first multiples of 5 greater than 0.
count (int) to count the first 10 multiples of 5 greater than 0
Pseudo-Code
1. Set the variable sum to 0.
2. Set the loop counter count to 1.
3. As long as count < = 10 do the following:
3.a. compute 5 * count and add the result to sum: sum = sum + 5 * count.
3.b. Increment the loop counter count by 1: count = count + 1.
4. Print the sum.
Flowchart
False True
Start
count = 1
count = count + 1
count < = 10
Stop
sum = 0
sum = sum + 5 * count
Write
sum
b. C++ Language Code Segment
int sum = 0,
count = 1;
while ( count <= 10)
{
sum = sum + 5 * count;
count = count + 1;
}
cout << sum;
Exercise 3.10
1.a Variables:
value (int) to hold a value
product (int) to hold the product.
count (int) to count the values.
Pseudo-Code
1. Set the current product to 1: product = 1.
2. Set the loop counter count to 0.
3. As long count < 10 do the following:
3.a. read a value into the variable value.
3.b. compute: product = product * value
3.c. Increment the loop counter count by 1.
4. Print the product.
Flowchart
False True
Start
count = 0
count = count + 1
count < 10
Stop
product = 1
product = product * value
Write
product
Read
value
1.b. C++ Language Code Segment
int product = 1, value,
count = 0;
while ( count < 10)
{
cin >> value;
product = product * value;
count = count + 1;
}
cout << product;
2.a
Variables
value (int) to hold the current even number
Pseudo-Code
1. Set the current even number to 20: value = 20.
2. As long value > 0, do the following:
2.a. compute value * value and print the result.
2.b. Decrement the value of variable value by 2.
Flowchart
False True
Start
value = value - 2
value > 0
Stop
value = 20
Write
value * value
b. C++ Language Code Segment
int value = 20;
while ( value > 0)
{
cout << endl << value * value;
value = value - 2;
}
Exercise 3.11
Variables
TotalSale (double) to hold the total sale in the store
DailySale (double) to hold a daily sale in the store
Pseudo-Code
1. set the total sale TotalSale to 0
2. As long as the total sale TotalSale is less than 10000 do the following:
2.a. Read a daily sale into the variable DailySale.
2.b. Add the daily sale to the total sale.
3. print the total sale and the message “it is time to make a deposit”.
Flowchart
False True
Start
TotalSale = TotalSale + DailySale
TotalSale < 10000
Stop
TotalSale = 0
Read
DailySale
Write
TotalSale - 10000
“time to make deposit”
b. C++ Language Code Segment
int totalSale = 0,
dailySale;
while (totalSale < 10000)
{
cin >> dailySale;
totalSale = totalSale + dailySale;
}
cout << endl << totalSale - 10000 << “\tIt is time to make a deposit”;
Exercise 3.12
Variables
num (int) to hold the integer value
divisor (int) the hold the divisor
Pseudo-Code
1 read an integer value greater than 1 into variable num.
2 set the variable divisor to 2
3 As long as ( num % divisor != 0 and divisor < num / divisor) do the following:
3.a. increment the current value of variable divisor by 1.
4 If num % divisor = 0, do the following:
4.a. print the message “the answer is:” and compute and print num /divisor.
5. Otherwise do the following:
5.a. print the message “the answer is:” and print 1.
Flowchart
False True
False True
b. C++ Language Code Segment
cin >> num;
divisor = 2;
while ( num % divisor != 0 && divisor * divisor < num )
{
divisor = divisor + 1;
}
if ( num % divisor = = 0)
cout << endl << “\t The answer is:” << num / divisor;
else
cout << endl << “\t The answer is:” << 1;
Start
num % divisor != 0
&&
divisor * divisor< num
Stop
divisor = 2
Read
num
Write
“The answer is:”, num / divisor
divisor = divisor + 1
num % divisor = 0
Write
“The answer is:”, 1
Exercise 3.13
1. #define DUMMYVALUE -99
int main ( )
{
int total = 0, value;
cout << “\nEnter all values to add followed by”
<< DUMMYVALUE << “to stop\n”;
cin >> value; // add this statement here
while ( value != DUMMYVALUE )
{
cin >> value; // remove this statement
total = total + value;
cin >> value; // add this statement here
}
cout << endl << “The total of these values is:\t” << total;
return ( 0 );
}
2.a Variables
ftemp (double) to hold a temperature in Fahrenheit
Pseudo-Code
1. Read a temperature (in Fahrenheit) into variable Ftemp.
2. As long as the temperature in the variable Ftemp is not –99.00 do the following:
2.a. Convert it to Celsius and print the result..
2.b. Read the next temperature (in Fahrenheit) into variable Ftemp.
Flowchart
False True
Start
ftemp != -99.00
Stop
Read
ftemp
Write
5.0 / 9 * (ftemp – 32)
Read
ftemp
b. C++ Language Code Segment
int ftemp;
cin >> ftemp;
while ( ftemp != -99.00 )
{
cout << endl << ftemp << “\t” << 5.0 / 9 * (Ftemp – 32);
cin >> ftemp;
}
Exercise 3.14
1. a. Input value: 4 b. Input Value: 20
Output Output
Result= 3 pnum= 27
Result= 19
2.
Variables
uprice (double) to hold the unit price
qty (int) to hold the quantity
price (double) to hold the price of the product
Pseudo-Code
1. Read the unit price of the product into variable uprice, and the quantity purchased into variable qty.
2. Compute the price of the product (before the rebate if applicable): price = uprice * qty
3. if the price is greater than or equal to 20.00 do the following:
3.a. Compute the price with the rebate as follows: price = price – 0.1 * price
4. Print the price of the product (with the rebate if applicable).
Flowchart
False True
Start
Read
uprice, qty
price = uprice * qty
price >= 20.00
Price = price – 0.1 * price
Write
price
Stop
C++ Language Code Segment
int qty;
double uprice,
price;
cin >> uprice >> qty;
price = uprice * qty;
if ( price >= 20.00)
price = price – 0.10 * price;
cout << endl << “price =” << price;
Exercise 3.15
b. C++ Language Code Segment
int num,
qty;
double uprice;
cin >> num >> qty;
if (num = = 101)
uprice = 5.25;
else if (num = = 202)
uprice = 3.10;
else if (num = = 303)
uprice = 9.75;
else if (num = = 404)
uprice = 6.50;
else
uprice = 0.0;
cout << endl << “The price is:\t” << (uprice * qty);
if (uprice = = 0)
cout << endl << “invalid bouquet number”;
a.
Variables
num (int) to hold a bouquet type code.
unprice (double) to hold its unit price.
qty (int) to hold its quantity.
Flowchart
True False
True False
True False
True False
True False
Start
Read
num, qty
num = 101
uprice = 5.25
uprice = 3.10
num = 202
num = 303
uprice = 9.75
uprice = 0.0 uprice = 6.50
num = 404
Write
uprice * qty
Stop
uprice = 0
Write
“invalid bouquet number”
Exercise 3.16
1.a. a. Input value: 3 b. Input value: 7 c. Input value: 12
Output Output Output
Red Yellow Green
blue blue blue
1.b. a. the output is Red Blue for num < 5
b. the output is Yellow Blue for 5 <= num < 10
c. the output is Green Blue for num >= 10
2. Variables
speed (int) to hold the speed of the car.
fine (double) to hold the fine
Flowchart
Start
Read
speed
A
True False
True False
True False
b. C++ Language Code Segment
int speed;
double fine;
cin >> speed;
if (speed <= 50)
fine = 0.00;
else if (speed <= 70)
fine = 15.00;
else if (speed <= 80)
fine = 30.00;
else
fine = 60.00;
cout << endl << “the fine imposed is:\t” << fine;
speed <= 50.00
fine = 0.00
fine = 15.00
speed <= 70.00
speed <= 80.00
fine = 30.00
fine = 60.00
Write
fine
Stop
A
Exercise 3.17
Variables
num (int) to hold the integer value.
remain (int) the remainder in the division by 5.
bonus (int) to hold the bonus
Flowchart
True False
True False
True False
True False
Start
Read
num
remain = num % 5
remain = 0
bonus = 200
bonus = 350
remain = 1
remain = 2
bonus = 400
bonus = 750 bonus = 500
remain = 3
Write
bonus
Stop
b. C++ Language Code Segment
cin >> num;
remain = num % 5;
if (remain = = 0)
bonus = 200;
else if (remain = = 1)
bonus = 350;
else if (remain = = 2)
bonus = 400;
else if (remain = = 3)
bonus = 500;
else
bonus = 750;
cout << endl << “The bonus is:\t” << bonus;
Exercise 3.18
1. a. Input value: 2
Line Numbers Variables OUTPUT
num1 num2 result 18
- 5 ? 0
1 5 2 0
2 5 2 0
3 5 2 9
4 5 2 9
5 7 2 9
8 7 2 18
9 7 2 18
1. b. Input value: 6
Line Numbers Variables OUTPUT
num1 num2 result 17
- 5 ? 0
1 5 6 0
2 5 6 0
4 5 6 0
5 11 6 0
8 11 6 17
9 11 6 17
Exercise 3.19
Line Numbers Variables OUTPUT
ndx pnum pnum = 20
- ? ? ndx = 5
1 1 ?
2 1 60
3 1 60
4 1 60
5 3 60
3 3 60
4 3 20
5 5 20
3 5 20
6 5 20
Hands-On Exercises
1.a.
Variables num (int) to hold the integer value
False True
c. C++ Language Code Segment:
int num;
cin >> num;
if ( num % 7 == 0 )
cout << endl << num << “MULTIPLE OF 7”;
else
cout << endl << num << “NOT MULTIPLE OF 7”;
Start
Read
num
num % 7 = 0
Write
num,
“MULTIPLE OF 7”
Write
num,
“NOT MULTIPLE of 7”
Stop
2. Variables
uprice (double) to hold the unit price of the product.
price (double) to hold the price of the product
qty (int) to hold the number of items purchased.
bonus (double) to hold the bonus
Flowchart
True False
C++ Language Code Segment
double uprice, price, bonus;
int qty;
cin >> uprice >> qty;
price = uprice * qty;
if (price >= 100.00)
bonus = .10 * price;
else
bonus = .05 * price;
cout << endl << “The price is:\t” << price << “\nThe bonus is:\t” << bonus;
price >= 100.00
bonus = .1 * price bonus = .05 * price
Write
Price, bonus
Stop
Start
Read
uprice, qty
price = uprice * qty
3. Variables:
num1 (int) to hold the first value
num2 (int) to hold the second value
Flowchart
False True
b. C++ Language Code Segment
int num1, num2;
cin >> num1 >> num2;
if (num1 > num2)
{
num1 = num1 + 3;
cout << (num1 + num2);
cout << (num1 – num2);
}
else
{
num1 = num1 – 2;
cout << (num1 * num2);
cout << (num2 / 2);
}
cout << 2 * num1 – num2;
Start
Read
num1, num2
num1 > num2
Write
num1 + num2, num1 – num2 Write
num1 * num2, num2 / 2
Write
2 * num1 – num2
Stop
num1 = num1 - 2 num1 = num1 + 3
4. Variables symb (char) to hold the character value
Flowchart
False True
c. C++ Language Code Segment:
char symb;
cin >> symb;
if ( (symb >= ‘A’ && symb <= ‘Z’) || (symb >= ‘a’ && symb <= ‘z’) )
cout << endl << symb << “LETTER”;
else
cout << endl << symb << “NOT A LETTER”;
5. Variables
prodCount (int) to count the products
uprice (double) to hold the unit price of a product
qty (double) to hold the quantity of a product
price (double) to hold the price of the product
totalPrice (double) to hold the current total price of the products
Start
Read
symb
(symb >= ‘A’ && symb <= ‘Z’)
|| (symb >= ‘a’ && symb <= ‘z’)
Write
symb,
“LETTER”
Write
symb, “NOT A
LETTER”
Stop
Flowchart
False True
b. C++ Language Code Segment
int pcount = 0;
double uprice, price, qty, totalPrice = 0;
while ( pcount < 20)
{
cout << endl << “enter product unit price and quantity:\t” ;
cin >> uprice >> qty;
price = uprice * qty;
cout << price
totalPrice = totalPrice + price;
pcount = pcount + 1;
}
cout << endl << “the total price is:\t” << totalPrice;
Start
totalPrice = totalPrice + price
prodCount < 20
Stop
prodCount = 0
Read
uprice, qty
totalPrice = 0
price = uprice * qty
Write
totalPrice
prodCount = prodCount + 1
Write
price
6. Variables:
value (int) to hold the values
total (int) to hold their sum
Flowchart
False True
b. C++ Language Code Segment
int value = 1,
total = 0;
while ( value < = 20)
{
total = total + value;
value = value + 1;
}
cout << total;
Start
value = 1
value = value + 1
value < = 20
Stop
total = 0
total = total + value
Write
total
7. Variables:
value (double) to hold the value.
power (double) to hold its powers.
count (int) to count the powers.
Flowchart
False True
b. C++ Language Code Segment
double value, power = 1, int count = 0;
cin >> value
while ( count < 6)
{
power = power * value;
count = count + 1;
}
cout << power;
Start
count = 0
count = count + 1
count < 6
Stop
power = 1
power = power * value
Write
power
Read
value
8. Variables
num (int) to hold an integer value
count (int) to count the integer values read
pcount (int) to count the number of positive integer values
ncount (int) to count the number of negative integer values
Flowchart
False True
False True
Start
count = 0
count = count + 1
count < 20
Stop
pcount = 0
Write
pcount, ncount
sum
ncount = 0
Read
value
value > 0
ncount = ncount + 1 pcount = pcount + 1
b. C++ Language Code Segment
int value,
pcount = 0,
ncount = 0,
count = 0;
while ( count < 20)
{
cin >> value;
if (value > 0)
pcount = pcount + 1;
else
ncount = ncount + 1;
count = count + 1;
}
cout << endl << “number of positive values:\t” << pcount;
cout << endl << “number of negative values:\t” << ncount;
9. Variables
num (int) to hold an integer value
count (int) to count the integer values read
pcount (int) to count the number of positive integer values
ptotal (int) to hold the sum of the positive integer values
ncount (int) to count the number of negative integer values
ntotal (int) to hold the sum of the negative integer values
Flowchart
Start
pcount = 0
ncount = 0
ptotal = 0
ntotal = 0
A
False True
False True
b. C++ Language Code Segment
int value,
pcount = 0,
ptotal = 0
ncount = 0,
ntotal = 0,
count = 0;
while ( count < 20)
{
cin >> value;
if (value > 0)
{
ptotal = ptotal + value;
pcount = pcount + 1;
}
count = 0
count = count + 1
count < 20
Stop
Write
ptotal/pcount
ntotal/ ncount
Read
value
value > 0
ncount = ncount + 1 pcount = pcount + 1
ntotal = ntotal + value
A
ptotal = ptotal + value
else
{
ntotal = ntotal + value;
ncount = ncount + 1;
}
count = count + 1;
}
cout << endl << “average of positive values:\t” << ptotal / pcount;
cout << endl << “average of negative values:\t” << ntotal / ncount;
10.a
Variables
stock (int) to hold the number of units of the product in the store
dailySale (int) to hold a daily sale of the product
Flowchart
False True
10.b. C++ Language Code Segment
int stock = 120,
dailySale;
while (stock > 30)
{
cin >> dailySale;
stock = stock - dailySale;
}
cout << endl << 120 - dailySale << “\nPlace a new order”;
Start
stock = stock - dailySale
stock > 30
Stop
stock = 120
Read
dailySale
Write
120 - stock
“place a new order”
11. Variables
price (double) to hold the price of a book
totalPrice (double) to hold the total price of the books
count (int) to count the books
Flowchart
False True
Start
totalPrice = totalPrice + price
price != -99.00
Stop
totalPrice = 0
Read
price
Write
totalPrice / count
count = 0
Read
price
count = count + 1
b. C++ Language Code Segment
double price,
totalPrice = 0;
int count = 0;
cin >> price;
while (price != -99.00)
{
totalPrice = totalPrice + price;
count = count + 1;
cin >> price;
}
cout << endl << “The average price is :\t” << totalPrice / count;
12. Variables
num (int) to hold the integer value
product (int) to hold their product
Flowchart
False True
b. C++ Language Code Segment
int num,
product = 1;
cin >> num;
while (num != 0)
{
Product = product * num;
cin >> num ;
}
cout << endl << “Their product is :\t” << product;
Start
product = product * num
num != 0
Stop
product = 1
Read
num
Write
product
Read
num
13. Variables
score (double) to hold a test score
totalScore (double) to hold the sum of all test scores
count (int) to count the test scores
Flowchart
False True
b. C++ Language Code Segment
double score,
totalscore = 0;
int count = 0;
cin >> score;
while (score != -99.00)
{
totalScore = totalScore + score;
count = count + 1;
cin >> score;
}
cout << endl << “The average score is :\t” << totalScore / count;
Start
totalScore = totalScore + score
score != -99.00
Stop
totalScore = 0
Read
score
Write
totalScore / count
count = 0
Read
score
count = count + 1
14.
Variables
num (int) to hold the integer value
Flowchart
False True
False True
b. C++ Language Code Segment
cin >> num;
if (num < 10)
smallt = smalt + 1;
if (num > 15)
greatt = greatt + 2;
else
smallt = smallt – 3;
somet = somet + 7;
15
Variables
num (int) to hold the integer value
Start
Read
num
num < 5
num > 20
greatt = greatt+ 2
somet = somet + 7
Stop
smallt = smallt + 1
smallt = smallt - 3
Flowchart
False True
False True
b. C++ Language Code Segment
cin >> num;
if (num > 100)
large_ct = large_ct + 1;
if (num < 50)
small_ct = small_ct + 2;
else
middle_ct = middle_ct + 3;
sum = sum + num;
16.
Variables
count (int) to count the number of values read
ecount (int) to count the number of even values
num (int) to hold an integer value
Start
Read
num
num > 100
num < 50
small_ct = small_ct + 2
sum = sum + num
Stop
large_ct = large_ct + 1
middle_ct = middle_ct +3
Flowchart
False True
False True
C++ Language Code Segment
int num,
count = 0,
ecount = 0;
while (count < 50)
{
cin >> num;
if (num % 2 == 0)
ecount = ecount + 1;
count = count + 1;
}
cout << endl << “the number of even values is:\t” << ecount;
Start
Read
num
count = 0
ecount = 0
count < 50
num % 2 = 0
ecount = ecount + 1
count = count + 1
Write
ecount
Stop
17.
Variables
ptotal (int ) to hold the sum of the positive integer values
pcount (int) to count the number of positive values
num (int) to hold an integer value
Flowchart
False True
False True
False True
Start
Read
num
pcount = 0
ptotal = 0
num != -99
num > 0
ptotal = ptotal + num
Write
ptotal / pcount
Stop
Read
num
pcount = pcount + 1
pcount = 0
Write
“No positive values “
C++ Language Code Segment
int num,
ptotal = 0,
pcount = 0;
cin >> num
while ( num != -99 )
{
if ( num > 0 )
{
Ptotal = ptotal + value;
Pcount = pcount + 1;
}
cin >> num;
}
if( pcount = = 0 )
cout << endl << “there is no positive values ”;
else
cout << endl << The average of positive values is:\t” << ptotal / pcount;
18.
Variables
grade (char) to hold a student’s letter grade.
Flowchart
True False
True False
True False
True False
True False
Start
Read
num1, op, num2
op = ‘+’
op = ‘-’
op = ‘*’
op = ‘/’ &&
num2 != 0
Write
op or num2 is
“invalid“
Stop
Write
num1+ num2
Write
num1 – num2
Write
num1 * num2
Write
num1 / num2
Write
num1 % num2
op = ‘\%’ &&
num2 != 0
b. C++ Language Code Segment
int num1, num2;
char op;
cin >> num1 >> op >> num2;
if ( op = = ‘+’)
cout << endl << num1 + num2;
else if ( op = = ‘-’)
cout << endl << num1 - num2;
else if ( op = = ‘*’)
cout << endl << num1 * num2;
else if (op = = ‘/’ && num2 != 0 )
cout << endl << num1 / num2;
else if ( op = = ‘\% ’ && num2 != 0 )
cout << endl << num1 % num2;
else
cout << endl << op << “or” << num2 << “ is invalid”;
19. Variables
uprice (double) to hold the unit price of the product.
price (double) to hold the price of the product
qty (int) to hold the number of items purchased.
rebate (double) to hold the rebate
Flowchart
True False
True False
True False
price >= 100.00
rebate = 10.00
rebate = 5.00
price >= 50.00
price > = 20.00
rebate = 2.00 rebate = 0.00
Start
Read
uprice, qty
price = uprice * qty
A
C++ Language Code Segment
double uprice, price, rebate;
int qty;
cin >> uprice >> qty;
price = uprice * qty;
if (price >= 100.00)
rebate = 20.00;
else if (price >= 50)
rebate = 5.00;
else if (price >= 20.00)
rebate = 2.00;
else
rebate = 0.00;
price = price – rebate;
cout << endl << “amount due is:\t” << (price + 0.0825 * price);
20.
Variables
symbol (char) to hold a character
cin >> symbol;
if ((symbol >= ‘a’ && symbol <= ‘z’) || (symbol >= ‘A’ && symbol <= ‘Z’)
cout << endl << “letter”;
else if (symbol >= ‘0’ && symbol <= ‘9’)
cout << endl << “digit”;
else
cout << endl << “special character”;
Write
price + 0.0825 * price
Stop
price = price - rebate
A