lab 2.1 introduction to c programming

advertisement
EKT 150 – Computer Programming
Laboratory Exercise
LAB 2.1
INTRODUCTION TO C PROGRAMMING
School of Computer and Communication Engineering
Universiti Malaysia Perlis
1
EKT 150 – Computer Programming
Laboratory Exercise
1. OBJECTIVES:
1.1 To be able to apply basic rules and structures of C in writing a simple program.
1.2 To be able to use printf and scanf functions to display output and read input in a
program.
1.3 To become familiar with fundamental data types.
1.4 To be able to name, declare, assign and print values of variables used in
program.
2. TASKS:
2.1 What data types would you use to hold the following data and write C statements to
declare them all.
a. customer initial
Data type:
Declaration:
b. customer name
Data type:
Declaration:
c. your house number
Data type:
Declaration:
d. price Data
type:
Declaration:
e. car registration
Data type:
Declaration:
f.
time
Data type:
Declaration:
g. 6 digit number
Data type:
Declaration:
2
EKT 150 – Computer Programming
Laboratory Exercise
2.2 Write a statement (or comment) to accomplish each of the following:
a.
State that a program will calculate the product of three integers.
b.
Define the variables x, y, z and result to be type int.
c.
Prompt the user to enter three integers.
d.
Read three integers from the keyboard and store them in the variables x, y and
z.
e.
Compute the product of the three integers contained in variables x, y and z and
assign the answer to the variable result.
f.
Print “The product is” followed by the value of the integer variable result.
2.3 Using the statements you wrote in Task 2.2, write a complete program that
calculates the product of three integers.
3
EKT 150 – Computer Programming
Laboratory Exercise
2.4 Write a program that calculates marked area of the circle, given input from the user
r1 and r2.
r2
r1
a.
Write down the pseudo code and flowchart for the program:
b.
Write your program based on pseudo code and flowchart in (a):
4
EKT 150 – Computer Programming
Laboratory Exercise
2.5 Write a program to find the equivalent series and parallel resistance for 3 resistor
values. Your program should scan the 3 resistor values and then compute the
equivalent series and parallel resistance for all 3 resistors. For example, if the 3
resistor values are r1=100, r2=200 and r3=300 ohms, respectively, their equivalent
series resistance is r1 + r2 + r3 = 100 + 200 + 300 = 600 ohms and their equivalent
parallel resistance = 1 / [1/r1 + 1/r2 + 1/r3] = 1/[0.01+0.005+0.0033] = 54.55 ohms.
a.
Write down the pseudo code and flowchart for the program:
b.
Write your program based on pseudo code and flowchart in (a):
5
EKT 150 – Computer Programming
Laboratory Exercise
2.6 Write a program that inputs one five-digit number, separates the number into its
individual digits and prints separated from one another by three spaces each.[Hint :
Use combinations of integer division and the remainder operation.] For example, if
the user types in 42139, the program should print:
Sample Output:
Enter value : 42139
Answer :
4
2
1
3
9
a.
Write down the pseudo code and flowchart for the program:
b.
Write your program based on pseudo code and flowchart in (a):
6
EKT 150 – Computer Programming
Laboratory Exercise
Additional Exercises:
2.7 Write a program to calculate the distance travel using the given formula below:
2
S = vt + ½ at
In your program ask user to input through keyboard the value
of: v = initial velocity
a = acceleration
t = time of travel
a.
Write down the pseudo code and flowchart for the program:
b.
Write your program based on pseudo code and flowchart in (a):
7
EKT 150 – Computer Programming
Laboratory Exercise
2.8 Write a program to take a depth (in kilometers) inside the earth as input data;
compute and print the temperature at this depth in degrees Celsius and degrees
Fahrenheit.
Data Requirements
Problem Input
double dDepth
/*depth in km*/
Problem Outputs
double cCelcius
double dFahr
/*temperature in degrees Cescius*/
/*temperature in degrees Fahrenheit*/
Relevant Formulas
Celcius = 10 (depth) + 20
/*Celsius temperature at depth in km*/
Fahrenheit = 1.8 (Celsius) + 32;
a.
Write down the pseudo code and flowchart for the program:
b.
Write your program based on pseudo code and flowchart in (a):
8
Download