Sunteți pe pagina 1din 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 1 : Payroll Program Examine this program:
#include <stdio.h> #include <conio.h> /* Payroll Program Exercise No 1*/ void main (void) { long hoursWorked = 40; double payRate = 10.0, taxRate = 0.10; printf("Hours Worked: %d", hoursWorked ); printf("pay Amount : %f" , (hoursWorked * payRate) ); printf("tax Amount : %f" , (hoursWorked * payRate * taxRate) ); }

Modify it so that each variable is declared by itself and is not initialized. Next write three assignment statements to assign a value to each variable. Run the program; examine the output. Now let's break something: Remove one of the declarations from the program. Can you compile it? Now remove one of the initializations from the correct program. (For example, delete the characters "= 40" from the first declaration. Try to compile and run the program. When is a problem detected? Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 1 of 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 2 : Value of a Quadratic Say that you are interested in the value of the quadratic... 3X - 8X + 4
2

...for several values of X. Write a program that has a double precision variable X. Assign a value to it. Write statement that computes a value for the quadratic and stores the result in another double precision variable. Finally write out the result, something like: At X = 4.0 the value is 20.0 Here is some part of the codes to help you solve the problem.
#include <stdio.h> #include <conio.h> /* Payroll Program Exercise No 1*/ void main (void) {
double x = 4.0; //put your printf here. }

Run the program with several values for X (edit the program for each value of X) and examine the result. Use values with decimal points, large values, small values, negative values, and zero. Solve the equation with paper and pencil (use the quadratic formula.) The quadratic should evaluate to zero at X = 2.0 and at X = 2/3 (0.6667). Try these values for X. Are the results exactly correct? Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 2 of 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 3 : Re-assigning values to Variables Modify the program in exercise 2 so that one run of the program will evaluate and write out the value of the quadratic for three different values of X: 0.0, 2.0, and 4.0 (or any three values of your choice.) Write the program using only two variables, probably called x and value. Of course this means that you will have to put different things in these variables in different places in the program. In writing the program make use of your editor's "copy" and "paste" functions to avoid re-typing similar lines. Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 3 of 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 4 : Convert Peso to Dollar Write a program which converts amount in peso into dollars. (Use an exchange rate of 55.00 pesos per dollar). Display the conversion. 100.00 pesos is 1.82 dollars Run the program with several values for pesos (edit the program for each value of pesos) and log the result. Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 4 of 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 5 : Gravity of the moon The moons gravity is about 17 percent of Earths. Write a program that will convert your weight and computes your effective weight on the moon. My weight is 75 kg in earth. My weight is 12.75 kg in the moon. Run the program with several values for weight and log the result. Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 5 of 7

College of Information and Communications Technology

C Exercises

Module : Variables and Assignment Statements Exercise 6 : Average Rain Fall Write a program that averages the rain fall for three months, April, May, and June. Declare and initialize a variable to the rain fall for each month. Compute the average, and write out the results, something like: Rainfall for April Rainfall for May Rainfall for June Average rainfall : : : : 12 14 8 11.333333

To get the numerical values to line up use the tabulation character '\t' as part of the character string in the output statements. Check that your program prints the correct results. There is a beginner's error lurking in this program too! Did you fall victim to it? Write your findings in your notebook.

Chapter 1 Variables and Assignment Statement

Page 6 of 7

College of Information and Communications Technology

C Exercises

Chapter 1 Variables and Assignment Statement

Page 7 of 7

S-ar putea să vă placă și