CS 111 - SUMMER 2002: Homework 1

Due Thursday, May 30

Reading: Chapters 1,2 and 3 (first part), pages 1-182


Reminder: Homework is due by 5:00 PM.

1. Write a C++ program which takes as input some number of hours, minutes and seconds and converts this input to the total number of seconds.

So for example if the input is 1 hour, 3 minutes and 20 seconds the output would be 3800 seconds.

You can use the program on page 35 of the book as a model.

Test your program on the inputs (i) 5 hours, 23 minutes, 6 seconds, (ii) 12 hours and 55 seconds and (iii) 4 minutes.

2. Write a program which takes as input a year and which tells if that year is a leap year or not. So for example, the output might be,

1860 is a leap year, or

1999 is not a leap year.

The rule is that a year that is not divisible by 4 is not a leap year. If a year is divisible by 4 then it is a leap year unless it is divisible by 100 in which it is not a leap year except when it is also divisible by 400. So 1900 is not a leap year but 2000 is a leap year.

3. Write a C program to read four numbers from a file, the first two are integers and the last two are real numbers. The program should then print out the following information. It should be clearly labeled and formatted.

1. The four numbers in the order they were read.

2. The four numbers in the opposite order from which they were read.

3. The average of the four numbers.

4. The largest number in the list.

5. All the numbers in the list which are smaller than 10.

Your program should use if-else statements but not use any loop statements. (No while or do-while) It should not use arrays.

Part 2:

Here are a few "self-check" problems. You should be able to do them. You need not turn them in.

1. Write C++ expressions for the following. You can assume all of the variables are integer variables.

a. 5x

b. 1/(x + y)^ 2

c. a x^2 + bx + c

d. a/x <= (b+c)d

2. What is the value of x in the following assignment statements ?

a. x = 4+3*6/2-5;

b. x = 4%2 -3*6-5/2;

c. x = (3*9*(2+(8*3/(3))));

3. Write a program fragment that converts a given number of seconds into hours, minutes and seconds.


Academic Honesty and Collaboration

It is reasonable to discuss with others possible general approaches to problems. It is unreasonable to work together on a detailed solution, to copy a solution, or to give away a solution. If your common discussion can be detected by looking at the solutions, then there is too much collaboration. Such instances of academic dishonesty will result in a course grade of F or expulsion from Boston University.

Do not allow your work to be used by others:

Warning: If someone cheats by using your work, you will also be penalized


Page Created: May 16, 2000