lab 2 : problem solving techniques, algorithm: pseudo code and flowchart lab introduction prepared...

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

Upload: lester-parrish

Post on 25-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Prepared by: Cik Noor Syazana bt Arshad

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

LAB INTRODUCTION

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

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)

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

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

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

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

Page 5: LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION 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

Page 6: LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION 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

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

Prepared by: Cik Noor Syazana bt Arshad

Formatted input with scanf-cont

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

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

Page 9: LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION 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

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

Prepared by: Cik Noor Syazana bt Arshad

How to print a line?

• Command• printf(“Welcome to UNIMAP”);• printf(“----------------------------”);

• DisplayWelcome to UNIMAP----------------------------

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

Prepared by: Cik Noor Syazana bt Arshad

Algorithm-Basic symbols in a flowchart

Start/End

Process

Input/Output

Decision

Flow direction

Connector

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

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

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

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

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

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”);

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

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

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

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

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

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

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

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”);

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

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