Lecture - Notes - II Pseudocode
Lecture - Notes - II Pseudocode
read number
If (number = 5)
else if (number = 6)
else
write "your number is not 5 or 6“
https://www.qacps.org/cms/lib02/MD01001006/Centricity/Domain/847/Pseudo_Code%20Practice_Proble ms.pdf
example 2
pseudocode that will count all the even numbers up to a user
defined stopping point.
read count
set i to 0
while (i < count)
i = i + 1
write even
https://www.qacps.org/cms/lib02/MD01001006/Centricity/Domain/847/Pseudo_Code%20Practice_Proble ms.pdf
user-defined functions
programmers can define their own functions (procedures, sub-
routines)
syntax
If the function returns a value then the type of that value must be
specified in function type(return type).
local-definitions
- definitions of variables that are used in the function-
implementation only (no meaning outside function).
int main ()
{
int a = 1000, b = 1001, answer;
// calling a function
answer = highest(a, b);
1. if n is 0, return 1
end factorial
https://en.wikipedia.org/wiki/Recursion_(computer_science)
example: factorial
n is 4 = 4 * f3
= 4 * (3 * f2)
= 4 * (3 * (2 * f1))
= 4 * (3 * (2 * (1 * f0)))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
= 4 * 6
= 24
example: factorial
#include <iostream>
using namespace std;
long factorial (long a){
if (a > 1)
return (a * factorial(a-1));
else
return 1;
}
0 1 2 3
e.g.
int example1[20];
int main()
{
int i, example[5];
cout<<“enter 5 numbers: ";
int main ()
{
for ( n=0 ; n<5 ; ++n )
{
answer += sum[n];
}
cout <<"sum is " << answer;
return 0;
}
Arrays : multidimensional
can be considered "arrays of arrays".
3-dimensional:
cout<<“example["<<i<<"]["<<j<<"]="<<example[i][j]<<
endl;
}
}
return 0; }
Arrays : multidimensional example
int main() {
int example[2][3][2]; // 3-D array
cout<<“enter 12 values: \n";
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cin>>example[i][j][k];
}
}
}
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cout<< “example["<<i<<"]["<<j<<"]["<<k<<"] = "
<< example[i][j][k] << endl;
}
}
}
return 0;
}