Professional Documents
Culture Documents
Untitled
Untitled
Untitled
• OUTCOME
• volume
Algorithm Sample (A4)
• PROCEDURE
• Let
•volume be the volume of the cube
• volume = side * side * side
• volume = pow (side, 3)
Pseudo code
• is an informal high-level description of the operating
principle of a computer program or other algorithm.
( := or )
START
INITIALIZE (CONSTANT)
COMPUTE
READ or INPUT
WRITE or OUTPUT
END
Pseudo code (A)
START
INITIALIZE side, volume
READ side
COMPUTE
volume := side * side * side
WRITE volume
END
Source code
• is any collection of code, possibly with comments, written
using a human-readable programming language, usually
as plain text.
• is specially designed to facilitate the work of computer
programmers, who specify the actions to be performed
by a computer mostly by writing source code.
• is often transformed by an assembler or compiler into
binary machine code understood by the computer.
Source code
// comment
#include <iostream>
using namespace std;
main(){
declaration;
cout << “…”;
cin >> variable;
}
Source code
– Save your file as C++ source file (*.cpp)
– #include <cmath>
– #include <string>
– /*comments…
…
…
*/
Source code (A1) Source code (A2)
#include <iostream> #include <iostream>
using namespace std; #include <cmath>
main(){ using namespace std;
float volume, side; main(){
cout << "Enter Side: "; float volume, side;
cin >> side; cout << "Enter Side: ";
volume = side * side * side; cin >> side;
cout << “Volume: “ << volume; volume = pow(side,3);
} cout << “Volume: “ << volume;
}
Algorithm Sample (B1)
• PROBLEM
• Create a program that will compute
and display the peso equivalent of a
dollar. (1 dollar = 46.75 pesos)
Algorithm Sample (B2)
• GIVEN
• Let :
• dollar be the dollar value
• $1 = Php 46.75
Algorithm Sample (B3)
• OUTCOME
• peso
Algorithm Sample (B4)
• PROCEDURE
• peso be the peso value
peso = dollar * 46.75
Pseudo code (B1)
START
INITIALIZE peso, dollar
READ dollar
COMPUTE
peso := dollar * 46.75
WRITE peso
END
Source code (B1)
#include <iostream>
using namespace std;
main(){
float peso, dollar;
cout << "Enter dollar: ";
cin >> dollar;
peso = dollar * 46.75;
cout << “The peso equivalent is” << peso;
}
Pseudo code (B2)
START
INITIALIZE peso, dollar
CONSTANT val := 46.75
READ dollar
COMPUTE
peso := dollar * val
WRITE peso
END
Source code (B2)
#include <iostream>
using namespace std;
main(){
const float val = 46.75;
float peso, dollar;
cout << "Enter dollar: ";
cin >> dollar;
peso = dollar * val;
cout << “The peso equivalent is” << peso;
}
Algorithm Sample (C1)
• PROBLEM
• Create a program that will compute
and display the area of a circle.
(pi = 3.1416)
Algorithm Sample (C2)
• GIVEN
• Let :
• radius be the radius of the
circle
• pi = 3.1416
Algorithm Sample (C3)
• OUTCOME
• area
Algorithm Sample (C4)
• PROCEDURE
•Let :
• area be the area of the circle
• area = pi * radius * radius
• area = pi * pow(radius, 2)
Pseudo code (C)
START
INITIALIZE area, radius
CONSTANT pi := 3.1416
READ radius
COMPUTE
area := pi * radius * radius
WRITE area
END
Source code (C1)
#include <iostream>
using namespace std;
main(){
const float pi = 3.1416;
float radius, area;
cout << "Enter radius: ";
cin >> radius;
area = pi * radius * radius;
cout << "Area is " << area;}
Source code (C2)
#include <iostream>
#include <cmath>
using namespace std;
main(){
const float pi = 3.1416;
float radius, area;
cout << "Enter Radius: ";
cin >> radius;
area = pi * pow(radius,2);
cout << "Area: " << area;}
Algorithm Sample (D1)
• PROBLEM
• Create a program that will
compute and display the
hypotenuse of a right triangle.
Algorithm Sample (D2)
• GIVEN
• Let :
• a be a side 1 of a right triangle
• The adjacent side
• b be a side 2 of a right triangle
• The opposite side
Algorithm Sample (D3)
• OUTCOME
• c be the hypotenuse
Algorithm Sample (D4)
• PROCEDURE
• c be the longest side of a right
triangle
c = sqrt(a*a + b*b)
c = sqrt(pow(a,2) + pow(b,2))
Pseudo code (D1)
START
INITIALIZE c, a, b
READ a,b
COMPUTE
c:= sqrt(a*a + b*b)
WRITE c
END
Pseudo code (D2)
START
INITIALIZE c, a, b
READ a,b
COMPUTE
c:= sqrt(pow(a,2) + pow(b,2))
WRITE c
END
Source code (D1)
#include <iostream>
#include <cmath>
using namespace std;
main(){
float a,b,c;
cout << "A: ";
cin >> a;
cout << "B: ";
cin >> b;
c = sqrt(pow(a,2) + pow(b,2));
cout << "The hypotenuse of the triangle is " << c;
}
Algorithm Sample (E1)
• PROBLEM
• A salesman sold twice as much pears in the
afternoon than in the morning. Create a program
that, if the total kilo of pears sold on that day is
given by the user, identifies how many kilograms
did he sell in the morning and how many in the
afternoon?
Algorithm Sample (E2)
• GIVEN
• Let :
• x_kilo be the total kilo of pears
sold by the salesman
(AM and PM)
Algorithm Sample (E3)
• OUTCOME
• am_kilo sales
• pm_kilo sales
Algorithm Sample (E4)
• PROCEDURE
• am_kilo be the kilo of pears sold in the AM
• pm_kilo be the kilo of pears sold in the PM
• x_kilo = am_kilo + pm_kilo
• pm_kilo = 2*am_kilo
• x_kilo = am_kilo + (2*am_kilo)
• x_kilo = 3*am_kilo
• am_kilo = x_kilo/3
Pseudo code (E)
START
INITIALIZE x_kilo, am_kilo, pm_kilo
READ x_kilo
COMPUTE
am_kilo := x_kilo/3
pm_kilo := am_kilo*2
WRITE am_kilo, pm_kilo
END
Source code (E)
#include <iostream>
using namespace std;
main(){
float x_kilo, am_kilo, pm_kilo;
cout << “Enter Total Sales: ”;
cin >> x_kilo;
am_kilo = x_kilo/3;
pm_kilo = am_kilo*2;
cout << “AM: ” << am_kilo << “ PM: ” << pm_kilo;}
Algorithm Sample (F1)
• PROBLEM
• Mary, Peter, and Boris were picking chestnuts. Mary
picked twice as much chestnuts than Peter. Boris
picked 2 kg more than Peter. Together the three of
them picked x_kilo kg of chestnuts. Create a
program that, if the total kilo of chestnuts is entered
by the user, identifies how many kilograms did each
of them pick?
Algorithm Sample (F2)
• GIVEN
• Let :
• x_kilo be the total kilo of
chestnuts picked by the three
of them
Algorithm Sample (F3)
• OUTCOME
• k_peter
• k_mary
• k_boris
Algorithm Sample (F4)
• PROCEDURE
• k_peter be the kilo of chestnuts he picked
• k_mary = 2*k_peter
• k_boris = 2 + k_peter
x_kilo = k_peter + k_mary + k_boris
x_kilo = k_peter + 2k_peter + k_peter + 2
x_kilo = 4k_peter + 2
4k_peter + 2 = x_kilo
4k_peter = x_kilo-2
k_peter = (x_kilo-2)/4
k_mary = 2*k_peter
k_boris = 2 + k_peter
Pseudo code (F)
START
INITIALIZE x_kilo, k_peter, k_mary, k_boris
READ x_kilo
COMPUTE
k_peter := (x_kilo-2)/4
k_mary := 2*k_peter
k_boris := 2 + k_peter
WRITE k_peter, k_mary, k_boris
END
Source code (F)
#include <iostream>
using namespace std;
main(){
float x_kilo, k_peter, k_boris,k_marry;
cout << “Enter Total Kilograms: ”;
cin >> x_kilo;
k_peter = (x_kilo-2)/4;
k_mary = k_peter*2;
k_boris = k_peter+2;
cout << “PETER: ” << k_peter << endl;
cout << “Mary: ” << k_mary << endl;
cout << “Boris: ” << k_boris;}