General Review Problems C++
General Review Problems C++
General Review Problems C++
Hasan Kamal
University of Bahrain
College of Information Technology
Department of Computer Science
CSC100/ITCS101 Introduction to Computers and IT
Introduction Problems
1. Write a program that converts a distance measure from inches to cents. Note that one inch is equal to 2.54
centimeters?
2. Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius.
celsius = 5/9 (f ahrenheit 32)
3. Students are awarded points toward their grades based upon the average of three quizzes (Q1 , Q2 , Q3 ), the
midterm exam (M T ), and half the final exam (F inal). Quizzes are on scale from 0 to 100, the midterm is on
scale from 0 to 50, and the final is on scale from 0 to 100. Compute the total points using a C++ program and
print the result out of 100.
4. The reliability of two components connected in parallel is
P1
P2
P1
P2
R=
+
100.0
100.0 100.0 100.0 100.0
where P 1 is the reliability in percentage of the first component, P 2 is the reliability in percentage of the second
component, and R is the reliability of the two components when connected in parallel.
Write a program that reads P 1 and P 2 and displays R. If R1 or R2 is not a percentage value (i.e. not in the
range 0..100) display an invalid message and quit the program.)
5. Write a program to read three resistance values R1 , R2 , and R3 in Ohms and compute their combined resistance
Rc when they are arranged in parallel. The value of Rc is computed using the following formula.
Rc =
1
1
1
1
+
+
R1 R2 R3
Selection Problems
1. Write a program to find the ideal weight for a given height. The height must be given in centimeters between
140 cm and 240 cm. The ideal weight is the height minus 100. Before calculating the height check that the input
is valid?
2. ACE balloon shop sells balloons to customers at 10 fils each. If the customer buys 500 1000 balloons, the price
is 8 fils each. If the customer buys over 1000 balloons, the price is 6 fils each. Write a program that reads the
number of balloons to purchase and prints the amount of the bill?
3. A part time employee is paid BD. 2/000 per hour for the first 20 hours in a week. Additional hours are paid
BD. 2/500 per hour. In addition the company deducts 5% of the total weekly income for health insurance. Find
how much (s)he will be paid for any given number of hours?
4. Write a C++ program that reads a month number as an integer (form 1 to 12) and a day number as an integer
(from 1 to 31.)
If the month entered is not between 1 and 12 inclusive, print a message informing the user that an invalid month
has been entered. If the day entered is not between 1 and 31 inclusive, print a message informing the user that
an invalid day has been entered.
If no error messages are displayed, print Winter if the month is in the range 13, Spring if the month is in
the range 46, Summer if the month is in the range 7-9, and Autumn otherwise.
5. Write a C++ program for a Cable Power Company (CPC) to produce the monthly bills for their customers.
The customer will pay:
BD 1.000 per kWh for less than or equal to 100 kWh.
BD 1.500 per kWh for greater than 100 kWh.
Your program will input the customer number CNUM and quantity QUANT in kWh consumed by a customer.
It will then output the customer number and amount in BD (i.e. Bahraini Dinars) to be paid.
6. Write a program that determines the gross pay for employees in a company according to the following rules:
The company pays straight- time for the first 50 hours worked by each employee and pays time-and-aquarter for hours worked in between 51 and 60 hours (inclusive) and pays time-and-a-half for all hours
worked in excess of 60 hours.
In addition, the company pays $30 as social allowance for single employees and $50 for married employees.
(Hint: use S or s to indicate single employees and M or m to indicate married employees).
Furthermore, the company deducts from the total gross salary of each employee 5% for the insurance
company.
Here is a sample Input/Output of your program:
Enter No. of hours worked: 40
Enter hourly rate: 4.5
Enter Marital Status: M
Gross Pay is $218.50
7. Write a C++ program that accepts a number followed by one space and then a letter. If the letter following the
number is f or F, the program is to treat the number entered as a temperature in degrees Fahrenheit, convert
the number to the equivalent degrees Celsius, and print the result. If the letter following the number is c or
C, the program is to treat the number entered as a temperature in degrees Celsius, convert the number to the
equivalent degrees Fahrenheit, and print the result. If the letter is neither f, F, c, nor C, the program is to
print a message that the input is invalid and terminate.
8. Write a program that reads two points on a line and another two points on another line. Your program should
determine whether these two lines are parallel or they intersect. If they intersect, you need further to state
whether they are perpendicular or not.
Two lines are parallel if their slopes are equal, otherwise they intersect. Two intersected lines are perpendicular
if the product of their slopes is -1. Use the formula below to compute the slopes of the two lines.
Slope of (x1 , y1 ) and (x2 , y2 ) is
y2 y1
.
x2 x1
9. Write a C++ program that asks the user to enter two float numbers and a character which represents a code
for an arithmetic operation. The program performs arithmetic operations (addition, subtraction, multiplication,
and division) on those two numbers depending on the value of code as follows:
3
Code
A or a
S or s
M or m
D or d
Arithmetic Operation
+
*
/
You must use the switch statement to determine the arithmetic operation. If the code character that is entered
by the user is not one of those specified above, print a message saying invalid code.
Loops problems
1. Blood sugar is considered normal if its rate is less than 140 and greater than 70, it is considered high if its rate
is greater than or equal to 140, and it is considered low if its rate is less than or equal to 70.
Write a C++ program that asks the user to input the rate of blood sugar of each patient in a hospital (-1 to
indicate the end of the data.) The program should calculate and print the number of patients with normal blood
sugar, number of patients with high blood sugar, number of patients with low blood sugar, and the total number
of patients entered by the user.
2. Write a program that reads a positive integer number of any digit size and do the following:
(a) Print the sum of its digits.
(b) Print the average of its digits.
(c) Print the leftmost digit.
(d) Print the maximum digit.
If the input number is negative, convert it to positive. Here is a sample Input/Output of your program:
Enter a positive integer: 1246
The Sum is 13
The average is 3.25
The leftmost digit is 1
The maximum is 6
3. Write a C++ program that reads two integers num1 and num2. If num1 is greater than num2, then swap
(exchange) them. Next, your program must display all numbers from num1 to num2 on a single line such that
every line contains the number followed by all its divisors (see sample input/output below.)
Sample Input/Output
Enter two numbers: 20 25
number
20
21
22
23
24
25
Divisors
2
4
5
3
7
21
2
11
22
23
2
3
4
5
25
10
20
12 24
4. Write a program that reads ages of students in a community and counts the number of students in each class
according to the following table:
1
6
13
16
Age
5
12
15
18
Class
Preschool
Primary
Intermediate
Secondary
Your program must continuously read ages in the range 1 18 until -1 is entered. It should ignore all ages that
are outside this range. Here is a sample Input/Output of your program.
5. The grade level of undergraduate college students is typically determined according to the following schedule:
Number of Credits Completed
Grade Level
Less than 32
Freshman
32 to 63
Sophomore
64 to 95
Junior
96 or more
Senior
Number of Credits is valid if it is from 0 to 133, and any other value is considered invalid.
Using this information, write a C++ program that accepts the number of credits of each student in a college
(negative number to stop) and determines the percentage of Freshmen students, Sophomore students, Junior
students, and Senior students.
6. Write a program that reads student GPAs on a 4.0 scale. The number of students is not known in advance.
Therefore, you must read the GPAs until a negative number is entered.
Your program should display the number of students with GPA of 4.0 and their corresponding percentage.
Sample Input/Output
Enter student GPAs, negative number to end:
3.11
2.25
4.0
1.95
3.05
4.0
2.35
-1
There are 2 students with a GPA 4.0.
They represent 28.57% of the students.
7. A sport club wants to update the information about its members. Write a complete C++ program that asks
the user to input members ID (integer) and status S or M to indicate single or married (char). Use -1 for the
ID number to stop entering the data. The program should then output the following:
The total number of members in the club.
The total number if single members.
The total number of married members.
8. Write a complete program that accepts two integer numbers num1 and num2, where num1 < num2. Make sure
that num1 < num2. If not, read again for new integers until num1 < num2. Your program should count and
print the number of integers divisible by 5 between num1 and num2 (inclusive.)
Sample Input/Output
Enter first number: 10
Enter second number: 1
Invalid Input. First should be less than second.
Enter first number: 1
Enter second number: 10
The numbers divisible by 5 are:
5
10
There are 2 numbers divisible by 5.
9. Write a program that reads from the keyboard a positive integer M and generates a triangle of numbers (as
shown below). You must make sure the number is entered is positive before making the triangle.
For example, if the input for M is 6, the output triangle should be
6
5
4
3
2
1
5
4
3
2
1
4 3 2 1
3 2 1
2 1
1
10. Write a program that reads two integers num1 and num2. If any of these integers is negative convert it to
positive and if num1 > num2, swap num1 and num2. The output of your program is as shown below:
Enter first number: 3
Enter second number: 7
3
4
5
6
7
6
8
10
12
14
9
12
15
18
21
...
...
...
...
...
30
40
50
60
70
11. Write a program that reads a positive integer N in the range 1 to 15. If N is not in this range display an error
message and quit the program. The program should display N lines of characters in a form of twin pyramids
using nested f or loops. For example, if N = 5, the Output is:
A
A
BBB
BBB
CCCCC
CCCCC
DDDDDDD
DDDDDDD
EEEEEEEEE EEEEEEEEE
Functions
1. Use the Math Library Functions to write a C++ program to display the following table for values of X, Y , and
Z:
X
2
5
8
.
.
.
Y
29.00
143.00
347.00
Z
2.84
3.33
3.77
53
56
14207.00
15851.00
7.68
7.87
where
Y = 5X 2 + 3X + 3
1
Z = X +6+ 2
|X 100|
2. The distance between points with coordinates (XA , YA ) and (XB , YB ) is given by
p
DIST AN CE = (XA XB )2 + (YA YB )2
You are given the coordinates of three points: point1 (X1 , Y1 ), point2 (X2 , Y2 ), and point3 (X3 , Y3 ). Write a
program to read the coordinates. Next write a function that calculates the distance between any two points.
Use this function to find and print the distance DIST12 between points 1 and 2, distance DIST1 between points
1 and 3, and distance DIST23 between points 2 and 3.
4. Write a C++ program to compute the surface area of a cylinder, given the radius (R) and height (H). The
program must ask the user to enter R and H. It then prints the surface-area with a proper message in the main
program.
Define a function to compute the surface-area and let main() call it. Clearly show the function prototype. This
function takes the radius R and height H as arguments and returns the surface area. The formula for computing
the surface-area of a cylinder is 2R(H + R), where = 3.141592654.
5. Write a function called isValidCode() that takes an integer number and returns 1 if the number represents
valid code and 0 otherwise. Valid code is determined according to the following rules:
The number length is at least 3 digits and at most 5 digits.
All the digits must be between 1 and 5 (inclusive).
The sum of the digits must be odd number.
For example, the number 324 is valid because its 3 digits long, all digits are between 1 and 5, and the sum of
the digits is 9, which is odd. However, the number 3256 is not valid because it consists of the digit 6 which is
not in the range 1 to 5. Also the the number 114 is not valid because the sum is 6 which is not odd.
Arrays
1. What is the output of the following program.
#include <iostream>
int main()
{
int x[3] = {630, 1, 0},
y[3] = {196, 0, 1},
z[3], i, quot;
while( x[0] % y[0] != 0 )
{
quot = x[0] / y[0];
for ( i = 0; i < 3; i++ )
{
z[i] = x[i] - quot * y[i];
x[i] = y[i];
y[i] = z[i];
}
cout << z[0] << "\t" << z[1] << "\t" << z[2] << endl;
}
return 0;
}
2. Write a C++ program to do the following:
(a) Declare an array of size 200 that will hold marks of the midterm exam of all students taking CSC100/ITCS101
course this semester. Name this array midterm.
(b) Read the midterm marks for all students and store them in midterm until a negative number is entered. If
the mark is not in the range 0 to 100 ignore it and do not store it in the array.
(c) Sort the array midterm (either ascending or descending).
(d) Display the top 5 students.
Here is a sample of your program:
78.0
66.5
66.2
50.8
3. Write a program that generates 500 random numbers in the range 2 to 10000. Among these random numbers,
your program should display only the numbers that are prime in increasing order (ascending.)
4. Write a complete program that generates and displays n distinct (no duplicate) random numbers in the range
-1000 to 1000, where n is a positive integer input by the user.
5. Write a C++ program that prompts a user to input a string of text. The program should capitalize the first
letter of every word in the string and change the remaining letters to small letters. The words are separated by
only one space and non-alphabet characters are skipped. Here is a sample input/output of your program:
Enter a string:
6. Hundred students are asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 with
1 being awful (bad) and 10 being excellent. Suppose that the student answers are randomly generated in your
program (i.e. for each of the 100 students, a random number in the range 1 to 10 is generated as the answer),
write a C++ program that stores these 100 answers in an integer array and summarizes the frequency for each
rating.
Sample output of your program.
Rating
1
2
3
4
5
6
7
8
9
10
Frequency
12
5
17
4
15
11
5
7
11
13
In the above sample output, the number of students rated the quality of the food to 1 (awful) is 12, the number
of students rated the quality of the food to 2 is 5, the number of students rated the quality of the food to 3 is
17, and so on.
7. Write a C++ function called CheckArray that determines if a one-dimensional array of integers is sorted (ordered)
or not. If sorted, then is it sorted in increasing order or decreasing order.
Your function should return -1 if the array is unsorted, 0 if sorted in ascending order, and 1 if sorted in descending
order. The main function that calls CheckArray is given below. You should write the prototype of the function
first and then the function CheckArray itself.
8. Write a program that reads the temperatures for one week in different cities. Define a 2-dimensional array of
size 5 rows and 7 columns that will store the temperature readings. Ask the user to input the required data,
then print all those days in which the temperatures are the same (i.e. it should find all identical columns in the
2-dimensional array).
For example, if the numbers in the array are as follows:
9
Days
city0
city1
city2
city3
city4
0
23.0
22.4
25.0
23.6
21.1
1
27.1
28.0
31.7
30.6
28.0
2
23.0
22.4
25.0
23.6
21.1
3
26.9
30.5
26.4
32.3
30.2
4
28.0
24.0
30.0
26.0
34.0
5
29.0
24.0
31.2
26.0
29.5
6
26.9
30.5
26.4
32.3
30.2
Files
1. Write a C++ program to copy a text file called ORIGINAL.TXT to another file called COPY.TXT so that
finally you end up with two files having exactly the same content.
2. Write a program that reads a set of numbers from a file called marks.dat. The numbers in the file represent
marks for all sections of CSC100. Due to some hardware failure some of the marks in the file have been corrupted
(i.e. changed) and became out of range. The correct range for marks is from 0 to 100. Write a program that
reads the numbers from the file, remove the out-of-range marks, sort them in ascending order, and store them
into a new file called sorted.dat. Assume that the number of students in all CSC100 sections cannot exceed
200 students.
3. For research purposes and to better help students, the admission office of the university wants to know how well
female and male students perform in certain courses. You received a data file named STUDENTS.TXT that
contains female and male student GPAs for certain courses. The letter code F is used for female and the letter
code M is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has
one entry. The number of entries in the file in unknown. Write a C++ program that computes and outputs the
average GPA separately for female and male students.
SAMPLE DATA FILE
F
M
M
F
M
F
F
...
2.230
3.340
1.043
3.530
2.530
1.899
2.234
SAMPLE OUTPUT
Average GPA for female students is 2.657
Average GPA for male students is 2.534
4. An instructor of CSC100 has all students grades in a data file called "grades.dat". Each line in the file has
two numbers: student ID (integer) and the exam mark (double ) see sample data file below. The number of
lines in the data file is unknown. Write a C++ program that reads the ID and the exam mark for each student
in the file. The program must then display the ID of the student with the maximum mark. Assume that there
is no more than one maximum mark in the file.
SAMPLE DATA FILE
20034411
20037762
20031213
20045423
...
65
90
84
73
10
5. Consider a data file named STORES.DAT, which contains inventory information for four shops. Each line in the
file contains the inventory of five types of merchandize in each store. Write a program to perform the following:
(a) Read the data into a two-dimensional array invent(4,5).
(b) Produce the output shown below, which includes:
- finding the total inventory of all items in each store
- finding the maximum quantity of each item
THE OUTPUT:
STORE#1
STORE#2
STORE#3
STORE#4
Max Quant
ITEMS
#1
#2
#3
#4
#5 TOTAL
- - - - - - - - - - - - - - - - 14
0
5
2
16
37
15
20
0
0
5
40
25
0
10
30
20
85
5
3
3
6
0
17
- - - - - - - - - - - - - - - - 25
20
10
30
20