Lecture Slides
Lecture Slides
start/stop
- always have one start and one
stop.
input/output
compute sum
display sum
stop
pseudocode
display sum
class exercises
write pseudocode and draw a flowchart for a program that checks if
the entered number is even or odd.
read number
If ( number % 2 = 0 )
else
if the number is not in the range, the program will stop with an
appropriate message else the program will check if N is even or
odd.
programming tool used – dev c++ ide, an environment for the c++
language editor and compiler.
programming tools
interpreter
3rd generation language
(3GL)
−machine language.
−assembly language.
e.g. SQL
5GL
?
program translators
?
writing source code / building a program
building expressions / program statements
operator vs operand ?
expression ?
program statement ?
example
6 primary operators:
• addition +
• subtraction / negation -
• division /
• modulus %
• multiplication *
increment / decrement operators
prefix form:
++i i=i+1
--i i=i-1
postfix form:
i++ i=i+1
i-- i=i-1
relational / comparison operators
• not equal to !=
• logical OR ||
• logical NOT !
example
example
example
repetition / looping
example
unconditional/fixed conditional
test condition
if true, execute block
increment/decrement counter loop
test condition
do
{
block of statements
++ / -- counter
}
while ( test condition )
execution flow
8. }
program breakdown
1. comment
&
/* multiple lines
... commenting */
2. preprocessor
#include <iostream>
cin >> is used to enter data. cout << is used to access output.
can only process input from the can be used to access more than
keyboard once the enter key has one output values
been pressed.
most programs use the C++ standard library hence the line is
included in most of the programs.
4. main function
int main ()
return 0;
return statement causes the main function to finish the execution.
5 8
{… …}
opening curly brace/bracket closing curly brace/bracket for
for a block of statements a block of statements
variable declaration
declaration is done after the opening brace of main() (or before the
main() function).
identifier ? initilisation ?
display sum
selection
else
display sum
user-defined functions
user-defined functions
programmers can define other functions (procedures, subroutines) in
order to perform specific tasks, which are then called in the main.
syntax
1. if n is 0, return 1
end factorial
https://en.wikipedia.org/wiki/Recursion_(computer_science)
example
4 factorial – 4!
4! = 4 * 3!
= 4 * (3 * 2!)
= 4 * (3 * (2 * 1!))
= 4 * (3 * (2 * (1 * f0)))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
= 4 * 6
= 24
#include <iostream>
using namespace std;
long factorial (long a) {
if (a > 1)
return (a * factorial(a-1));
else
return 1;
}
int main () {
long number = 4;
cout << number << "! = " << factorial(number);
return 0;
}
class exercise
change the program:
- to check base case (n==0) if true, else execute the recursive case.
fibonacci numbers / sequence
for n > 2 :
fibonacci(0) = 0
fibonacci(1) = 1
class task
(a)
the program will then display the nth number in the fibonacci sequence
using recursion (a recursive function).
(b)
revise the program to ensure that the program must not allow
negative numbers.
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 "
0 1 2 3
the index is an integer value - indicates position and order of the elements,
starting at 0.
the length of an array is set when the array is declared and is fixed.
declaration
e.g.
int numbers[20];
the array is named numbers and can hold 20 elements of type int.
initialisation
int numbers[5] = {1,3,5,7,9};
number of values btwn braces { } is not greater than the size of the
array.
not required to write the size of an array if all elements are initialised:
if initialised with less elements, the remaining elements are set to the
default values of the data type.
int numbers[5]={1,3,5}; {1,3,5,0,0}
examples
int main() {
int i, numbers[5];
cout<<"enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin>>numbers[i];
cout<<numbers[i]<<endl;
}
return 0; }
int numbers[] = {1, 2, 3, 4, 5};
int n, sum=0;
int main () {
for ( n=0 ; n<5 ; ++n )
{
sum += numbers[n];
}
cout <<"sum is " << sum;
return 0; }
write a program to compute and display the sum of the first 20
natural numbers stored in an array, which is initialised with default
values.
the program has to generate the elements of the array - the first 20
natural numbers.
steps :
- display sum
revise / edit the
source code for
the range to be
(1,2,3,...,20)
2-D & 3-D (multidimensional) arrays
1st index value specifies a row index and 2nd index value specifies a
column index.
0 1 2 3
0
1
initialising 2-D
int numbers[2][3]={2,4,6,8,10,12};
int numbers[2][3]={{2,4,6},{8,10,12}};
2-D example
int m[3][2]={{2,-5},{4,0},{9,1}};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout << "m[" << i << "][" << j << "]= " << m[i][j]<< endl;
}
}
return 0;
}
3-D arrays
1st index value specifies a row index and 2nd index value specifies a
column index and 3rd index value specifies a depth index.
1
0 0 1 2 3
0
1
initialising 3-D
- size of the character array containing the string is one more than
the number of characters in the word. ?
int main()
{
char name[100];
cout<<"what is your full name? ";
cin.get(name,100);
cout << "my full name is " << name << endl;
return 0;
}
e.g.
pointer1 = &sum ;
e.g.
pointer1 = *pointer2 ;
? ? ?
int main () {
float num1 = 15.1; // variable declaration
float *pointer1; // pointer declaration
pointer1 = &num1; // store address of num1 in pointer1
cout << "value of num1 is " << num1 << endl;
// display the address stored in pointer1
cout << " address stored in pointer1 is " << pointer1 << endl;
// access the value at the address available in pointer1
cout << "value of *pointer1 is " << *pointer1 << endl;
return 0; }
value of num1 is 15.1
address stored in pointer1 is hexadecimal value ?
value of *pointer1 is 15.1
pointer arithmetic and arrays
++ , -- , + , –
pointers and arrays
#include <iostream>
using namespace std;
int main ()
{
int a[5];
int *b;
b = a; *b = 10;
b++; *b = 20;
b = &a[2]; *b = 30;
b = a + 3; *b = 40;
10 20 30 40 50
b = a; *(b+4) = 50;
struct statement - defines a new data type, with more than one member.
object_names - a set of valid identifiers for objects that have the type of
this structure.
-{} hold a list of data members, each one with a type and
a valid identifier (name) .
variable / member definition
type_name var1_name ;
.
dot operator ( ) is used.
e.g.
stud1.gender = F ;
example
#include <iostream>
#include <cstring>
using namespace std;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
Books Book1; // declare Book1 of type Book
// Book1 details
strcpy( Book1.title, "..." ) ;
strcpy( Book1.author, "..." ) ;
strcpy( Book1.subject, "..." ) ;
Book1.book_id = ... ;
// display Book1 details
cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;
return 0; }
'overviews'