pseudocode
- a program’s algorithm written in plain english to provide an
outline of the execution flow of statements.
- does not use any programming language syntax; use short
phrases and mathematical notations.
- omits programming implementation details e.g. no variable
declarations.
- used as a tool for planning and to understand the program’s
structure before the actual coding.
-
example 1
pseudocode to check if the entered number by user is not a 5 or a 6.
read number
If (number = 5)
write "your number is 5"
else if (number = 6)
write "your number is 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)
set even to even + 2
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)
- a function is group of statements that perform a specific task.
syntax
function type function name( parameter list )
{
local-definitions;
function implementation (statements);
}
- a function has two parts: a header and a function body.
user defined functions cont’d
If the function returns a value then the type of that value must be
specified in function type(return type).
If the function does not return a value then the function
type must be void.
function name : same rules apply as case of identifiers.
parameter list: lists the formal parameters of the function
together with their types.
user defined functions cont’d
function signature : function name & parameter list.
function declaration : return type, function name &
parameter list.
local-definitions
- definitions of variables that are used in the function-
implementation only (no meaning outside function).
function-implementation :statements that are executed
by the functio.
function prototype : a function is declared and used in a program
before the function is actually defined.
example: sum function
#include <iostream>
using namespace std;
int sum (int a, int b)
{
int add;
add = a+b;
return add;
}
int main ()
{
int c;
c = sum (10,20);
cout << “the sum is " << c; }
tutorial
using function prototyping to write a program to
find the largest/highest of two numbers.
re-write the a program from C to C++.
highest value of two
#include <stdio.h>
// function declaration
int highest(int num1, int num2);
int main ()
{
int a = 1000, b = 1001, answer;
// calling a function
answer = highest(a, b);
printf("highest value is : %d\n", answer );
return 0;
}
highest value of two
/* function returning the highest between two
numbers */
int highest(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Recursion
“(mathematics, computing) the practice of describing
numbers, expressions, etc. in terms of the numbers,
expressions, etc. that come before them in a series:”
– Cambridge dictionary
• in a recursive process the solution to a problem
depends on solutions to smaller occurrences of the
same problem.
• recursive function : a function that calls itself
repeatedly until a terminating point is reached.
example: factorial (pseudocode)
function factorial is:
input: integer n such that n >= 0
output: [n × (n-1) × (n-2) × … × 1]
1. if n is 0, return 1
2. otherwise, return [ n × factorial(n-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;
}
int main (){
long number = 10;
cout << number << "! = " << factorial (number);
return 0;
}
Arrays
Arrays : definitions
“… An array is a series of elements of the same type placed in
contiguous memory locations that can be individually referenced
by adding an index to a unique identifier …”
“… Arrays are a data structure which hold multiple values of the
same data type...”
“… An array is a container object that holds a fixed number of
values of a single type …”
“… An array is a data structure which allows a collective name to
be given to a group of elements which all have the same type ….”
“… Arrays are an example of a structured variable …”
Arrays : definitions
think of a collection of numbered pigeon holes, each containing one
data item where the hole number is the index of the data item i.e.
each item is identified by its index.
0 1 2 3
the index is an integer value - indicates position and order of the
elements.
the index is also used to access a specific element in the array.
the length of an array is established when the array is created and
after creation the length is fixed.
Arrays : declaration
data_type array_name [ array_size ];
e.g.
int example1[20];
the array is named example, can hold 20
elements of type int.
Arrays : initialization
float example2[5] = {1, 3, 5, 7, 9};
number of values btwn braces { } should not be greater than the
size of the array.
not required to write the size of an array:
float example2[] = {1, 3, 5, 7, 9};
If declared with less elements, the remaining elements are set to
their default values.
float example2[5] = {1, 3, 5}; {1,3,5,0,0}
Arrays : example
#include <iostream>
using namespace std;
int main()
{
int i, example[5];
cout<<“enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin>>example[i];
cout<<example[i]<<endl;
}
return 0;
}
Arrays : example
#include <iostream>
using namespace std;
int sum [] = {1, 2, 3, 4, 5};
int n, answer=0;
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".
have two or more index values which are used
to specify a specific element in the array.
for a two-dimensional table (2-D array) : 1st
index value specifies a row index and 2nd
index value specifies a column index.
for a three-dimensional table (3-D array) :
1st index value specifies a row index and 2nd
index value specifies a column index and 3rd
index value specifies a depth index.
Arrays : multidimensional
2-dimensional:
int example[2][3] = {2, 4, 6, 8, 10, 12};
int example[2][3] = { {2, 4, 6}, {8, 10, 12}};
3-dimensional:
int example[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23,
12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
int example[2][3][4] = {{{3, 4, 2, 3}, {0, -3, 9,
11},{23, 12, 23, 2} },{{13, 4, 56, 3},{5, 9, 3, 5},{3,
1, 4, 9}}};
Arrays : multidimensional example
#include <iostream>
using namespace std;
int main() {
int example[3][2] = {{2, -5},{4, 0},{9, 1}}; //2-D
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout<<“example["<<i<<"]["<<j<<"]="<<example[i][j]<<
endl;
}
}
return 0; }
Arrays : multidimensional example
a program to accept 12 values from a user and store
the values in 2 X 3 X 2 3-D array and display the
indices and the corresponding values.
#include <iostream>
using namespace std;
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;
}