Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad.

Similar presentations


Presentation on theme: "LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad."— Presentation transcript:

1 LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad

2 The printf function Prepared by: Cik Noor Syazana bt Arshad printf Display 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)

3 Formatted Output with “printf” #include 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

4 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

5 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

6 Formatted Output with “printf & scanf” #include 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

7 Prepared by: Cik Noor Syazana bt Arshad Formatted input with scanf-cont

8 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

9 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

10 How to print a line? Command printf(“Welcome to UNIMAP”); printf(“----------------------------”); Display Welcome to UNIMAP ---------------------------- Prepared by: Cik Noor Syazana bt Arshad

11 Algorithm-Basic symbols in a flowchart Start/End Process Input/Output Decision Flow direction Connector

12 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

13 Pseudo code Begin Get distance value in meter (m) Perform conversion from m to cm using formula: cm=100* m Print distance value in centimeter (cm) End Prepared by: Cik Noor Syazana bt Arshad

14 Program Coding #include int main () { // variables declaration float fcm,fm; // to display message printf(“\nProgram to convert distance from meter to centimeter”); Prepared by: Cik Noor Syazana bt Arshad

15 Program Coding (Continued) printf (“\n\nPlease enter the value of distance in meter: ”); 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

16 Q: Write a program to calculate the volume of a cylinder Start 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 (cm 3 ) End Calculate volume of a cylinder Prepared by: Cik Noor Syazana bt Arshad

17 Pseudo code Begin Get radius and height values in inches Calculate volume of cylinder using formula volume=pi*radius 2 *height Perform conversion from inches to cm using formula: 1inch=2.54cm Print volume of cylinder in cm 3 End Prepared by: Cik Noor Syazana bt Arshad

18 Program Coding #include int main () { // variables declaration const double PI=3.141594; double dVolume; float fR,fH; // to display message printf(“\nProgram to calculate the volume of a Cylinder\n”); Prepared by: Cik Noor Syazana bt Arshad

19 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 centimeter cubed is %7.2f cm\n”,dVolume); return 0; } //end of main Prepared by: Cik Noor Syazana bt Arshad


Download ppt "LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad."

Similar presentations


Ads by Google