Uploaded by Muhammad Zaidan

EKT150 Lab2

advertisement
EKT 150 – Introduction to Computer Programming
LAB 2
INTRODUCTION TO C PROGRAMMING AND
PROBLEM SOLVING TECHNIQUES
Aim:
1.
2.
3.
4.
Learning the Open Source IDE Platform: CodeBlock Environment
Writing simple C Program based on Pseudo Code and Flow chart
To apply the “printf” and “scanf” functions to write simple input and output statements.
To use arithmetic operators and understand the arithmetic operators precedence
INTRODUCTION TO CODE BLOCK
1. Open Code Block application as shown in Figure 1.
Figure. 1: Code Block Environment
2. Click at new file and choose empty file
Figure 2
EKT 150 – Introduction to Computer Programming
LAB 2
Figure. 3: Code Block Editor
3. Type “Hello World” program source code in the code block editor.
Figure. 4: Program "Hello World"
4. To compile the C code press F9 button or go to Build → build.
5. Observe the lower window – this is where the information whether your code is
successfully compiled.
6. If there were no errors display, to run and execute your program press F10 button or
go to Run → run
7. The execution of your program will look like Figure 5
Figure. 5: Execution of program "Hello World"
EKT 150 – Introduction to Computer Programming
LAB 2
INTRODUCTION TO C
1.
In C language, “printf” command is used to display any message or output to the screen.
The format of printf is:
printf(“The text to be displayed”);
2.
The text that you want to display must be within the double quote “the text”.
3.
You can send parameter to the printf function. The printf function enables us to display
the dynamic value after we have done the data processing, for example after calculating
mathematical problem and we want to display the answer to the screen. The command
we use is as below. For example, to calculate sum of two numbers using the equation,
sum=num1 + num2
printf(“The sum is %f”, sum);
• sum is the variable that contains the answer value, and it is passed to the printf
function.
• the symbol of % must be used to tell the printf function where to print the answer
value.
• The format “f” is used if we define the variable area as “float” data type. Other
format types are:
%d – for integer data type
%c – for character data type
%s – for string data type
%f – for floating points data type
and many more.
4.
In the C language, scanf is used to accept the user input from the keyboard. The
command of scanf is as below:
scanf (“%f”,&base);
5.
%f is the format of data type that will be entered. For example, if the variable “base” is
defined as float the format “f” is used.
6.
The “%f” must be in the double quote “ ”.
7.
The symbol “&” must be used with scanf command. This is to tell the compiler the
address of variable “base”, thus the keyed in data will be located to the address of “base”
in the computer memory.
EKT 150 – Introduction to Computer Programming
LAB 2
LAB EXERCISES
PART I: SHORT ANSWERED QUESTIONS
1.
Write the output of the following program.
#include<stdio.h>
int main(void)
{
printf("Introduction to Computer Programming\n");
printf("Total credit: \t 3 \n");
printf("Consist of 5 topics which are \nIntroduction \nSelection\n");
printf("Function \nRepetition \nArray \nPointers\n");
return 0;
}
Figure 1
EKT 150 – Introduction to Computer Programming
LAB 2
2.
The following question are based on C code in Figure 2. Answer all questions.
#include<stdio.h>
int main()
{
//declaration of variables
int sum=10;
char letter='Z';
float set1=10;
double num1=10;
double num2=10e+10;
printf("Integer variable is %d\n\n",sum);
printf("Character is %c\n\n",letter);
printf("Float variable is %f\n",set1);
printf("Double variable for floating number is %lf\n",num1);
printf("Double variable for exponential is %e\n",num2);
Figure 2
return 0;
}
Figure 2
i.
Write the output of the program.
ii.
Using the same C code in Figure 2, set the decimal place for float variable,
set1 to 3 decimal points, and double variable, num1 to 9 decimal points.
EKT 150 – Introduction to Computer Programming
LAB 2
3.
C code in Figure 3 determines the average of 2 numbers input by users and display the
result at the end.
#include<stdio.h>
int main()
{
float number1,number2;
float average=0;
printf("Enter 2 numbers\n");
scanf("%f %f",&number1,&number2);
average=(number1+number2)/2;
printf("Average of %f and %f is %f\n",number1, number2, average);
return 0;
}
Figure 3
(i)
Write the output of the program.
(ii)
Rewrite the program in Figure 3 to accept input of 3 numbers and display the average
of those 3 numbers.
EKT 150 – Introduction to Computer Programming
LAB 2
PART II: PROBLEM SOLVING QUESTIONS
1. Write a C program to read input of 2 floating numbers. The program must perform
addition, subtraction, multiplication and division against entered numbers. Display
each result of each mathematical operation. Your program output should be as
follows:
Enter 2 numbers:
45 23
45.00 + 23.00 = 68.00
45.00 – 23.00 = 22.00
45.00 x 23.00 = 1035.00
45.00 / 23.00 = 1.96
Your solution must include an algorithm design using flow chart.
EKT 150 – Introduction to Computer Programming
LAB 2
2. Design an algorithm using flowchart for a program that measure area of triangle.
Then, write a complete C program to implement the algorithm. The program must
read the value of base and height of the triangle entered by users, measure the area of
the triangle and display the result at the end. The formula for triangle area given as
follows:
1
area = × base × height
2
EKT 150 – Introduction to Computer Programming
LAB 2
3. Write a program to convert the temperature from degrees in Celsius to degrees in
Fahrenheit. The conversion formula given as follows:
F = (9.0/5.0 x C) + 32
where C is the temperature in Celsius and F the temperature in Fahrenheit.
(i)
(ii)
Design the algorithm, i.e write the pseudo code and draw the flowchart of the
solution.
Implement the algorithm designed in (i) in your program. Test and verify the
completed program and write down your completed program.
Download