lab 2 : problem solving techniques, algorithm: pseudo code and flowchart lab introduction prepared...
TRANSCRIPT
Prepared by: Cik Noor Syazana bt Arshad
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART
LAB INTRODUCTION
The printf function
Prepared by: Cik Noor Syazana bt Arshad
printfDisplay message on screen
printf(“Welcome to UNIMAP”);
Directly display
message
Use double quote (“ ”)
printf (“The expense is = %f ”,fExpense);
Display dynamic value
after data processing
% - to indicate location to
print variable value
f – float (data type)
Formatted Output with “printf”#include <stdio.h>int main (void) { int iMonth; float fExpense, fIncome; iMonth = 12; fExpense = 111.1; fIncome = 1000.0; printf (“The Month is %4d and the Expense is RM%9.2f\n”,iMonth,
fExpense);}
Prepared by: Cik Noor Syazana bt Arshad
Declaring variable (iMonth) to be integer
Declaring variables (fExpense and fIncome) to be real
Assignment statements store numerical values in the memory cells for the declared variables
Correspondence between variable names and %...in string literal
‘,’ separates string literal from variable names
Formatted Output with printf-cont
• printf (“The Month is %4d and the Expense=RM %9.2f \n” ,iMonth, fExpense);
• %4d refer to variable iMonth value (decimal number format)
• %9.2f refer to variable fExpense value. (floating point format)
• The output of printf function will be displayed as
Prepared by: Cik Noor Syazana bt Arshad
The scanf function
scanf accept user input from keyboard
scanf (“%f”,&fExpense);
%f -float (data type)
%f must be in double quote
“”
Symbol ‘&’ must be used with
scanf command
Prepared by: Cik Noor Syazana bt Arshad
Formatted Output with “printf & scanf”#include <stdio.h>int main (void) { int iMonth; float fExpense, fIncome; printf(“Please enter month”); scanf (“%d”,&iMonth); printf(“Please enter the expense and income”); scanf (“%f%f”,&fExpense,&fIncome);
printf (“The Month is %4d and the Expense is RM%9.2f\n”, iMonth, fExpense);
} Prepared by: Cik Noor Syazana bt Arshad
Declaring variable (iMonth) to be integer
Function scanf reads value typed on keyboard
Function scanf reads the first value as fExpense and the second value as fIncome
Prepared by: Cik Noor Syazana bt Arshad
Formatted input with scanf-cont
Data types
printf
• integer - %d• long integer - %ld• float - %f • double - %f• character - %c• string - %s
scanf
• integer - %d• long integer - %ld• float - %f • double - %lf• character - %s• string - %s
Prepared by: Cik Noor Syazana bt Arshad
Prepared by: Cik Noor Syazana bt Arshad
Escape sequence
\n •Next line•To display output in next line
\a •Bell sound when execute
\t •Horizontal tab•To add space between output
% •Placeholder•To indicate location to print variable value
Prepared by: Cik Noor Syazana bt Arshad
How to print a line?
• Command• printf(“Welcome to UNIMAP”);• printf(“----------------------------”);
• DisplayWelcome to UNIMAP----------------------------
Prepared by: Cik Noor Syazana bt Arshad
Algorithm-Basic symbols in a flowchart
Start/End
Process
Input/Output
Decision
Flow direction
Connector
Prepared by: Cik Noor Syazana bt Arshad
Q: Write a program to convert distance in meter(m) to centimeter(cm)...
Start
Get input or distance value in m
perform conversion from m to cm using formula: cm=100*m
print output or distance value in centimeter (cm)
End
Prepared by: Cik Noor Syazana bt Arshad
Pseudo code
BeginGet distance value in meter (m)Perform conversion from m to cm using formula: cm=100* mPrint distance value in centimeter (cm)
End
Prepared by: Cik Noor Syazana bt Arshad
Program Coding
#include <stdio.h>int main (){// variables declarationfloat fcm,fm;// to display messageprintf(“\nProgram to convert distance frommeter to centimeter”);
Prepared by: Cik Noor Syazana bt Arshad
Program Coding (Continued)
printf (“\n\nPlease enter the value of distance inmeter: ”);scanf(“%f”, &fm);fcm = 100*fm;printf(“\nThe value of distance in centimeter is %5.2f cm\n,fcm);return 0;} //end of main
Prepared by: Cik Noor Syazana bt Arshad
Q: Write a program to calculate the volume of a cylinderStart
Get radius and height values in inches
perform conversion from inches to cm using formula: 1 inches=2.54cm
print output or cylinder volume in centimeter cubed (cm3)
End
Calculate volume of a cylinder
Prepared by: Cik Noor Syazana bt Arshad
Pseudo code
BeginGet radius and height values in inches Calculate volume of cylinder using formulavolume=pi*radius2*height Perform conversion from inches to cm using formula: 1inch=2.54cmPrint volume of cylinder in cm3
End
Prepared by: Cik Noor Syazana bt Arshad
Program Coding #include <stdio.h>int main (){// variables declarationconst double PI=3.141594;double dVolume;float fR,fH;// to display messageprintf(“\nProgram to calculate the volume of a Cylinder\n”);
Prepared by: Cik Noor Syazana bt Arshad
Program Coding (Continued)
printf (“\nPlease enter the value of radius and height in inches:\n ”);scanf(“%f%f”, &fR,&fH);dVolume= PI*fR*2.54*fR*2.54*fH*2.54;printf(“\nThe volume of a cylinder in centimetercubed is %7.2f cm\n”,dVolume);return 0;} //end of main