2
UNIVERSITI KUALA LUMPUR MALAYSIA FRANCE INSTITUTE Lab 01 - Introduction Algorithm (PRG3012/PRG0012) EXERCISE 1 Draw the flowchart for the above pseudocode. Following is the equivalent C++ program for Exercise 1 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { float area, radius; //var area, var radius const float PI = 3.14; //const PI <- 3.14 cout<<"Input radius: "; //print "Input Radius: " cin>>radius; //read radius area=PI*radius*radius; //area <- PI * radius * radius cout<<"Area :"<<area; //print "Area :" area cout<<endl; system("PAUSE"); return EXIT_SUCCESS; } EXERCISE 2 Write the above C++ program using Dev C++. Compile and execute. Compare the C++ with the given pseudocode. Study at the algorithm (flow chart and pseudocode), how the it is translated into C++ program. Write a pseudocode to find the area of a circle using the following formula: Area = pi x radius 2 Prompts the user to enter a value for radius and reads it. Display the result on the screen. function main() begin var area, var radius const PI 3.14 print “Input Radius: “ read radius area PI * radius * radius print “Area :” area end

Lab C++ 01 - Pseudo Code & Flowchart

Embed Size (px)

Citation preview

Page 1: Lab C++ 01 - Pseudo Code & Flowchart

UNIVERSITI KUALA LUMPUR

MALAYSIA FRANCE INSTITUTE

Lab 01 - Introduction Algorithm (PRG3012/PRG0012)

EXERCISE 1

Draw the flowchart for the above pseudocode.

Following is the equivalent C++ program for Exercise 1

#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { float area, radius; //var area, var radius const float PI = 3.14; //const PI <- 3.14 cout<<"Input radius: "; //print "Input Radius: " cin>>radius; //read radius area=PI*radius*radius; //area <- PI * radius * radius cout<<"Area :"<<area; //print "Area :" area cout<<endl; system("PAUSE"); return EXIT_SUCCESS; }

EXERCISE 2

Write the above C++ program using Dev C++. Compile and execute. Compare the

C++ with the given pseudocode. Study at the algorithm (flow chart and pseudocode),

how the it is translated into C++ program.

Write a pseudocode to find the area of a circle using the following formula:

Area = pi x radius2

• Prompts the user to enter a value for radius and reads it. Display the result on the

screen.

function main()

begin

var area, var radius

const PI ← 3.14

print “Input Radius: “

read radius

area ← PI * radius * radius

print “Area :” area

end

Page 2: Lab C++ 01 - Pseudo Code & Flowchart

UNIVERSITI KUALA LUMPUR

MALAYSIA FRANCE INSTITUTE

EXERCISE 3 (To be discussed during lecture)

Draw flowchart and write pseudocode for the following problem:

1. Solve the boolean logic AND, OR and XOR. The algorithm will two boolean

variables i.e. 1 (TRUE), 0 (FALSE)

2. Perform a sum of a finite arithmetic series for a given number. e.g. reads a number 5,

then output is 15 (5+4+3+2+1)

3. Using the S = 7z4 + z2 – 1, find the value of S. (the algorithm reads the value of z)

4. Perform n multiplication for a given number e.g. number=2, n=5 then the output is

32 (2×2×2×2×2)

5. Find the biggest and smallest value from a series of number e.g. 2, 70, 14, 15, -1, 70.

Smallest = -1, biggest = 70