html5-img
1 / 19

LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART

LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART. LAB INTRODUCTION. The printf function. Formatted Output with “printf”. #include < stdio.h > int main (void) { int iMonth ; float fExpense , fIncome ; iMonth = 12; fExpense = 111.1; fIncome = 1000.0;

kassia
Télécharger la présentation

LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  3. Formatted Output with “printf” #include <stdio.h> int main (void) { intiMonth; 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); } 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 ‘,’ separates string literal from variable names Correspondence between variable names and %...in string literal Prepared by: Cik Noor Syazana bt Arshad

  4. Formatted Output with printf-cont • printf (“The Month is %4d and the Expense=RM %9.2f \n” ,iMonth, fExpense); • %4d refer to variableiMonth value (decimal number format) • %9.2f refer to variablefExpense value. (floating point format) • The output of printf function will be displayed as Prepared by: Cik Noor Syazana bt Arshad

  5. The scanf function Prepared by: Cik Noor Syazana bt Arshad

  6. Formatted Output with “printf & scanf” #include <stdio.h> int main (void) { intiMonth; 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); } 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

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

  8. Data types Prepared by: Cik Noor Syazana bt Arshad

  9. Escape sequence 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 Flow direction Start/End Process Connector Input/Output Decision Prepared by: Cik Noor Syazana bt Arshad

  12. 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

  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 <stdio.h> 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 Calculate volume of a cylinder perform conversion from inches to cm using formula: 1 inches=2.54cm print output or cylinder volumein centimeter cubed (cm3) End 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*radius2*height Perform conversion from inches to cm using formula: 1inch=2.54cm Print volume of cylinder in cm3 End Prepared by: Cik Noor Syazana bt Arshad

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

  19. Program Coding (Continued) printf (“\nPleaseenter 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(“\nThevolume of a cylinder in centimeter cubed is %7.2f cm\n”,dVolume); return 0; } //end of main Prepared by: Cik Noor Syazana bt Arshad

More Related