Professional Documents
Culture Documents
135
1991
7290
1992
11300
1993
16200
12. A cricket team has the following table of batting figures. Write a program to
read the figures in the given format and calculate the batting averages and print the
complete table along with the batting averages.
Players Name
Sachin
Saurav
Rahul
Runs Scored
8530
4200
3350
Innings Played
230
130
105
13. An electricity board charges the following rates to domestic users to discourage
the wastage of electricity. For the first 100 units: 60 P/unit. For the next 200 units:
80 P/unit. Beyond 300 units: 90 P/units. All users are charged a minimum of Rs.50. If
the total amount is more than Rs 300 then additional surcharge of 15% is added.
Write a program to read the names of users and number of units consumed and
print the total charges with names of consumers.
STRUCTURES:
16. A phone number, such as (212) 767- 8900, can be thought of having three
parts: the area code (212), the exchange (767), and the number (8900). Write a
program that uses a structure to store these three parts of a phone number
separately. Call the structure phone. Create two structure variables of type phone.
Initialize one, and have the user input a number for the other one. Then display
both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212
17. A point in the two-dimensional plane can be represented by two numbers: an X
coordinate and a Y coordinate. For example, (4,5) represents a point 4 units to the
right of the origin along the X axis, and 5 units up the Y axis. The sum of two points
can be defined as a new point whose X coordinate is the sum of X coordinates of the
two points, and whose Y coordinate is the sum of their Y coordinates.
WAP that uses a structure called point to model a point. Define three points,
and have the user input values to two of them. Then set the third point equal to the
sum of the other two, and display the value of the new point. Interaction with
program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinate for p1 + p2 are: 8, 11
18. Create a structure called Volume that uses three variables of type Distance to
model the volume of a room. Initialize a variable of type Volume to specific
dimensions, then calculate the volume it represents and printout the result. To
calculate the volume, convert each dimension from a Distance variable to a
variable of type float representing feet and fractions of a foot, and then multiply
the resulting three numbers.
FUNCTIONS:
19. Write a function called circarea() that finds the area of the circle. It should take
an argument of type float and return an argument of same type. Write a main()
function that gets a radius value from the user, calls circarea(), and displays the
result.
20. Raising a number n to a power p is the same as multiplying n by itself p times.
Write a function called power() that takes a double value for n and an int value for
p, and returns the result as double value. Use a default argument of 2 for p, so that
if this argument is omitted, the number will be squared. Write a main() function
that gets values from the user to test this function.
21. Write a function called zeroSmaller () that is passed two int arguments by
reference and then sets the smaller of the two numbers to 0. Write a main()
program to exercise this function.
22. Write a function that takes two Distance values as arguments and returns the
larger one. Include a main() program that accepts two Distance figures from the
user, compares them and displays the larger.
WAP that exercises this class by creating two initialized and one uninitialized Int
values, adding these two initialized values and placing the response in the
uninitialized value, and then displaying this result.
34. Imagine a toolbooth at a bridge. Cars passing by the booth are expected to pay
a fifty-cent toll. Mostly they do, but sometimes a car goes by without paying. The
tollbooth keeps track of the number of cars that have gone by, and of the total
amount of money collected.
Model this tollbooth with a class called tollbooth. The two data items are a type
unsigned int to hold the total numbers of cars, and a type double to hold the total
amount of money collected. A constructor initializes both these to 0. A member
function called payingCar() increments the car total and adds 0.50 to cash total.
Another function called nopayCar(), increments the car total but adds nothing to
the cash total. Finally, a member function called display() displays the two totals.
Include a program to test this class. This program should allow the user to push one
key to count a nonpaying car. Pushing the ESC key should cause the program to
print out the total cars and total cash and then exit.
35. Create a class time that has separate int member data for hours, minutes, and
seconds. One constructor should initialize this data to 0, and another should
initialize it to fixed values. A member function should display it, in 11:59:59 format.
The final member function should add two objects of type time passed as
arguments.
A main() program should create two initialized time objects, and one that is
not initialized. Then it should add the two initialized values together, leaving the
result in the third time variable. Finally it should display the value of this third
variable.
ARRAYS:
36. Write a function called reverseit() that reverse a string (an array of char). Use
a for loop that swaps the first and last characters, then the second and next-to-last
characters, and so on. The string should be passed to reverseit() as an argument.
WAP to exercise reverseit(). The program should get a string from the user,
call reverseit(), and printout the result. Use an input method that allows
embedded blanks. Test the program with Napoleons famous phrase,Able was I ere
I saw elbA.
37. Create a class employee that contains a name (an array of char) and an
employee number (type long). Include a member function called getdata() to get
data from the user for insertion into the object, and another function called
putdata() to display the data. Assume the name has no embedded blanks.
Write a main() program to exercise this class. It should create an array of
type employee, and then invite the user to input data for upto 100 employees.
Finally, it should print out the data for all the employees.
38. Write a program that calculates the average of upto 100 English distances input
by the user. Create an array of objects of the Distance class. This class includes
two member data items feet and inches of type int and float respectively, and
two member functions getdist() and showdist(). To calculate the average, you can
use the dist_add() member function. Youll also need a member function that
divides a Distance value by an integer. Here is one possibility:
void Distance:: div_dist(Distance d2, int divisor)
{
float fltfeet = d2.feet + d2.inches/12.0;
fltfeet /= divisor;
feet = int (fltfeet);
inches = (fltfeet-feet);
}
OPERATOR OVERLOADING:
39. To the Distance class in the previous question, add an overloaded operator
for the Distance class that subtracts two distances. It should allow statements like
dist3 = dist2 dist1;. Assume the operator will never be used to subtract a larger
number from a smaller one (that is, negative distances are not allowed).
40. WAP that uses an overloaded += operator for string concatenation. This
operator should allow statements like
s1 += s2;
where s2 is added (concatenated) to s1 and the result left in s1. The operator
should also permit the results of the operation to be used in other calculations, as in
s3 =s1+=s2;.
41. Modify the time class from problem # 35 so that instead of a function
add_time() it uses the overloaded + operator to add two times. WAP to test this
class.
42. Create a class Int based on problem #33. Overload all five integer arithmetic
operators (+, -, *, /, %) so that they operate on objects of type Int. If the result of
any such arithmetic operation exceeds the normal range of ints from -32,768 to
32,767, have the operator print a warning and terminate the program. WAP to test
this class.
INHERITANCE:
43. Imagine a publishing company that markets both book and audio-cassette
versions of its works. Create a class publication that stores the title (a string) and
price (type float) of a publication. From this class derive two classes: book, which
adds a page count (type int); and tape, which adds a playing time in minutes (type
float). Each of these three classes should have a getdata() function to get its data
from the user at the keyboard, and a putdata() function to display its data.
Write a main() program to test the book and tape classes by creating
instances of them, asking the user to fill in their data with getdata(), and then
displaying the data with putdata().
44. Start with the problem #43, add a base class sales that holds an array of three
floats so that it can record the dollar sales of a particular publication for the last
three months. Include a getdata() function to get three sales amounts from the
user, and a putdata() function to display the sales figures. Alter the book and tape
classes so they are derived from both publication and sales. An object of class book
or tape should input and output sales data along with its other data. Write a main()
function to create a book object and a tape object and exercise their input/output
capabilities.
POINTERS:
45. Write a program that reads a group of numbers from the user and places them
in an array of type float. Once the numbers are stored in the array, the program
should average them and print the result. Use pointer notation wherever possible.
46. Make a class String. Include a display() function to display the string. Add a
member function called upit() that converts the string to all upper case. You can
use the toupper() library function, which takes a single character as an argument
and returns a character that has been converted (if necessary) to uppercase. This
function uses the CTYPE.H header file. Write some code in main() to test this
function.
47. Use an array of pointers to strings to display the days of the week. Provide
functions to sort the strings into alphabetical order. Make functions for sorting and
ordering. Sort the pointers to the string, not the actual string.