You are on page 1of 7

PRAC / TUTORIAL 3_2

1. Determine and correct the errors in the following programs.


a. #include <stdio.h>
int main()
{
width = 15
area = length * width;
printf("The area is %d",area
return 0;
}

b. #include <stdio.h>
int main()
{
int length, width, area;
area = length * width;
length = 20;
width = 15;
printf("The area is %d",area);
return 0;

c. #include <stdio.h>
int main()
{
int length = 20; width = 15, area;
length * width = area;
printf("The area is %d",area);
return 0;
}

2. For a = 10.6, b = 13.10, c = -3.42, determine the value of:


a. (int) a
b. (int) b
c. (int) c
d. (int)(a + b)
e. (int) a + b + c
f. (int)(a + b) + c
g. (int) (a + b + c)

3. Determine why the expression a - b = 25 is invalid but the expression a - (b =25) is


valid.

1
Programming Exercises

1. a. Write a C program to calculate and display the average of the numbers 32.6, 55.2,
67.9, and 48.6.
b. Run the program written for Exercise 1a on a computer.

2. a. Write a C program to calculate the volume of a pool. The equation for determining
the volume is volume = length * width * depth. Assume that the pool has a length of 25
feet, a width of 10 feet, and a depth of 6 feet.

b. Run the program written for Exercise 2a on a computer.

3. a. Write a C program to convert temperature in degrees Fahrenheit to degrees Celsius.


The equation for this conversion is Celsius = 5.0/9.0 * (Fahrenheit - 32.0). Have your
program convert and display the Celsius temperature corresponding to 98.6 degrees
Fahrenheit.

b. Run the program written for Exercise 3a on a computer.

4. a. Write a C program to calculate the distance, in feet, of a trip that is 2.36 miles long.
One mile is equal to 5,280 feet.
b. Run the program written for Exercise 4a on a computer.

5. a. Write a C program to calculate the dollar amount contained in a piggy bank. The
bank currently contains 12 half-dollars, 20 quarters, 32 dimes, 45 nickels, and 27 pennies.
b. Run the program written for Exercise 5a on a computer.

6. a. Write a C program to determine the distance an airplane travels on a runway before


takeoff. The equation for determining the distance, d, in meters, is d = ½ a t2, where
a is the acceleration of the airplane in meters per second squared, and t is the time, in
seconds, that the airplane remains on the runway before lifting off. Use an acceleration
of 3.5 meters/sec2 and a time of 30 seconds.

b. Run the program written for Exercise 6a on a computer.

7. a. One of the longest suspension bridges in the world is New York City’s Verrazano-
Narrows Bridge, which links Brooklyn to Staten Island. In building this bridge, which is
made of steel, allowance had to be made for the expansion of the steel due to changes
in temperature. The change in the length of the bridge, _L, in meters, is given by the

2
formula _L = L * 12*10-6* _T, where L is the length, in meters, that the bridge must
span, 12*10-6 is the thermal expansion coefficient for steel, and _T is the range of
temperature, in degrees Celsius, that the bridge will experience. Using this equation,
write a C program to determine the expansion that the bridge will undergo if it spans a
length of 13,000 meters and can undergo temperature changes of 100 degrees Celsius.
(Note: The expansion is provided for by expansion joints placed in the surface of the
bridge, which automatically expand and contract as the temperature changes.)

b. Run the program written for Exercise 7a on a computer.

8. a. The work, W, in joules, required to stretch a spring a distance, d, in meters, is given


by the equation, W = ½k d 2, where k is a spring constant, in newtons per meter, that
provides a measure of the spring’s stiffness. Using this equation, write a C program to
determine the work required in stretching a spring .55 meters, for a spring having a
spring constant of 300 newtons per meter.

b. Run the program written for Exercise 8a on a computer.

EXERCISES
Short Answer Questions
1. Write C statements to determine:

a. The square root of 6.37.


b. The square root of x - y.
c. The value of 3.62 raised to the 3rd power.
d. The value of 81 raised to the .24 power.
e. The absolute value of a2 - b2.
f. The value of e raised to the 3rd power.

Programming Exercises
1. a. Write, compile, and execute a C program that calculates and returns the fourth root
of the number 81.0, which is 3. (Hint: The fourth root of a number can be obtained by
raising the number to the ¼, or .25 power).

b. When you have verified that your program works correctly, use it to determine the
fourth root of 1,728.896400.

2. Write, compile, and execute a C program that calculates the distance between two
points whose coordinates are (7, 12) and (3, 9). Use the fact that the distance between two
points having coordinates (x1, y1) and (x2, y2) is distance = sqrt([x1 - x2]2 + [y1 -
y2]2).

3
3. The formula for calculating the area of a triangle with sides a, b, and c can be
calculated using Heron’s formula: area = sqrt(s * (s - a) * (s - b) * (s - c)), where s = (a
+ b + c)/2.
Using this formula, write, compile, and execute a C program that calculates and displays
the area of a triangle having sides of 3, 4, and 5 inches.

4. A model of world population, in billions of people, after 2000 is given by the equation
Population = 6.0e.02 [Year - 2000]
Using this formula, write, compile, and execute a C program to estimate the world
population in the year 2010.

5. The time, T, in seconds, that a donut-sized space station must make one rotation in
gravity-free space to provide a human with the same gravitational force as that
experienced on Earth is given by the equation T =2_ sqrt(r/g), where g is the
gravitational force on the Earth’s surface, in meters/sec2 and r is the outside radius of the
space station, in meters. Using this equation write, compile, and run a C program that
calculates and displays the rotation time for a donut-sized space station that has an outer
radius of 700 meters and g = 9.81 meters/sec2.

6. Given an initial deposit of money, denoted as deposit, in a bank that pays interest
annually, the amount of money, amount, at a time n years later is given by the formula
amount = deposit * (1 + i)n where i is the interest rate as a decimal number (for example,
9.5% is .095). Using this formula, write, compile, and execute a C program that
determines the amount of money that will be available in four years if $10,000 is
deposited in a bank that pays 10% interest annually.

7. a. Newton’s law of cooling states that when an object with an initial temperature,
itemp, is placed in a surrounding substance of temperature a, it will reach a temperature
tfin in t minutes according to the formula tfin = a + (itemp - a) e-kt. In this formula k is a
thermal coefficient, which depends on the material being cooled. Using this formula,
write, compile, and execute a C program that determines the temperature reached by
an object after 20 minutes when it is placed in a glass of water whose temperature is
60 degrees. Assume that the object initially has a temperature of 150 degrees and has
a thermal constant of 0.0367.

b. Modify the program written for Exercise 7a to determine the temperature of the same
object, with an initial temperature of 150 degrees, after 10 minutes when it is placed
in a glass of water whose temperature is 50 degrees.

8. The number of bacteria, B, in a certain culture that is subject to refrigeration can be


approximated by the equation B = 300,000 e-.032t, where t is the time, in hours, that the
culture has been refrigerated. Using this equation, write, compile, and execute a single C

4
program that calculates the number of bacteria in the culture after it has been in the
refrigerator for 8 hours, and displays the result.

EXERCISES
Short Answer Questions

1. For each of the following declaration statements, write a scanf() function call to
correctly accept a user-entered value for the indicated variable.
a. int firstnum;
b. double grade;
c. double secnum;
d. char keyval;
e. int month, years;
double average;
f. char ch;
int num1, num2;
double grade1, grade2;
g. double interest, principal, capital;
double price, yield;
h. char ch, letter1, letter2;
int num1, num2, num3;
i. double temp1, temp2, temp3;
double volts1, volts2;

2. For the following scanf() function calls, write appropriate declaration statements.
a. scanf("%d",&day);
b. scanf("%c",&firChar);
c. scanf("%f",&grade);
d. scanf("%lf",&price);
e. scanf("%d %d %c",&num1,&num2,&ch1);
f. scanf("%f %f %d",&firstnum,&secnum,&count);
g. scanf("%c %c %d %lf",&ch1,&ch2,&flag,&average);

3. Given the following declaration statements,

int num1, num2;


double firstnum, secnum;
double price, yield;

determine and correct the errors in the following scanf() function calls.
a. scanf("%d",num1);
b. scanf("%f %f %f",&num1,firstnum,&price);
c. scanf("%c %lf %f",&num1,&secnum,&price);

5
d. scanf("%d %d %lf",num1,num2,yield);
e. scanf(&num1,&num2);
f. scanf(&num1,"%d");

Programming Exercises
1. a. Write a C program that displays the following prompt:
Enter the radius of a circle:
After accepting a value for the radius, your program should calculate and display the
circumference of the circle.
(Hint: circumference = 2 * 3.1416 * radius.)

b. Check the value displayed by the program written for Exercise 1a by calculating the
result manually. After manually determining that the result produced by your program
is correct, use your program to complete the following table:

2. a. Write a C program that first displays the following prompt:


Enter the temperature in degrees Fahrenheit:

Have your program accept a value entered from the keyboard and convert the
temperature entered to degrees Celsius, using the equation Celsius = (5.0 / 9.0) *
(Fahrenheit - 32.0). Your program should then display the temperature in degrees
Celsius, using an appropriate output message.

b. Compile and execute the program written for Exercise 2a. Verify your program by
calculating, by hand, and then using your program, the Fahrenheit equivalent of the
following test data:
Test data set 1: 0 degrees Celsius
Test data set 2: 50 degrees Celsius
Test data set 3: 100 degrees Celsius
When you are sure your program is working correctly, use it to complete the following
table:

6
3. a. Write a C program that displays the following prompts:
Enter the length of the room:
Enter the width of the room:
After each prompt is displayed, your program should use a scanf() function call to
accept data from the keyboard for the displayed prompt. After the width of the room is
entered, your program should calculate and display the area of the room. The area
displayed should be included in an appropriate message and calculated using the equation
area = length * width.

b. Check the area displayed by the program written for Exercise 3a by calculating the
result manually.

You might also like