- Write a program that reads three variables x, y, and z. The program should check if all three are equal, or if two of the three are equal, or if none are equal. Print the result of the tests. Show the program with manual trace.
- Velocity of an object traveling at a constant speed can be expressed in terms of distance traveled in a given time, If distance, , is in feet and time, , is in seconds, the velocity in feet per second is: Write a program to read distance traveled and time taken, and calculate the velocity for a variety of input values until distance traveled is zero. Print the results for each case. Show a manual trace.
- Acceleration of an object due to gravity, , is 32 feet per second per second. The velocity of a falling body starting from rest at time, , is given by: The distance traveled in time, , by a falling body starting from rest is given by: Write a program that repeatedly reads experimental values of time taken by a body to hit the ground from various heights. The program calculates for each case: the height of the body and the velocity of the body when it hits the ground.
- Write a program that reads a set of integers until a zero is entered. Excluding zero, the program should print a count of and a sum of:
- positive numbers
- negative numbers
- even numbers
- odd numbers
- positive even numbers
- negative odd numbers.
- all numbers
- Use debug statements to show cumulative sums as each new number is read and processed.
- We wish to convert miles to kilometers, and vice versa. Use the loose definition that a kilometer is 5.0 / 8.0 of a mile. Write a program that generates two tables: a table for kilometer equivalents to miles for miles 1 through 10, and a table for mile equivalents of kilometers for kilometers from 1 to 20.
- Improve the program prime.c of Section 2.5.6 in the following ways:
- Terminate the inner loop as soon as it is detected that the number is not prime.
- Test each candidate only while (divisor * divisor <= candidate).
- Test only candidates that are odd numbers greater than 3.
- For each of these improvements, how many times is the inner loop executed when PRIME_LIM is 20? How does that compare to our original program?
- Write a program to generate Fibonacci numbers less than 100. Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, etc. The first two Fibonacci numbers are 1 and 1. All other numbers follow the pattern: a Fibonacci number is the sum of previous two Fibonacci numbers in the sequence. In words, the algorithm for this problem is as follows:
- We will use two variables, prev1 and prev2, such that prev1 is the last fibonacci number and prev2 is the one before the last. Print the first two fibonacci numbers, 1 and 1; and initialize prev1 and prev2 as 1 and 1. The new fib_number is the sum of the two previous numbers, prev1 andprev2; the new fib_number is now the last fibonacci number and prev1 is the one before the last. So, save prev1 in prev2 and save fib_number in prev1. Repeat the process while fib_number is less than 100.
- (Optional) Write a program to determine the largest positive integer that can be stored in an int type variable. An algorithm to do this is as follows:
- Initialize a variable to 1. Multiply by 2 and add 1 to the variable repeatedly until a negative value appears in the variable. The value of the variable just before it turned negative is the largest positive value.
- The above follows from the fact that multiplying by 2 shifts the binary form to the left by one position. Adding one to the result makes all ones in the less significant part and all zeros in the more significant part. Eventually a 1 appears in the leading sign bit, i.e. a negative number appears. The result just before that happens is the one with all ones except for the sign bit which is 0. This is the largest positive value.
- (Optional) Write a program to determine the negative number with the largest absolute value.
- Write a program that reads data for a number of students and computes and prints their GPR. For each student, an id number and transcript data for a number of courses is read. Transcript data for each course consists of a course number (range 100-900), number of credits (range 1-6), and grade (range 0-4). The GPR is the ratio of number of total grade points for all courses and the total number of credits for all courses. The number of grade points for one course is the product of grade and credits for the course. The end of transcript data is signaled by a zero for the course number; the end of student data is signaled by a zero id number.