COS1511 Revision Notes
COS1511 Revision Notes
COS1511
School of Computing
Revision Notes
© UNISA 2018 1
Overview
© UNISA 2018 2
Suggested Topics
1. Variable diagrams;
2. Functions;
3. Data structures (Arrays & Structs); and
4. Two-dimensional arrays;
5. Reference parameters.
© UNISA 2018 3
Introduction
© UNISA 2018 4
Basic Principles
© UNISA 2018 5
Basic Principles - continued
– The use of OR as well as AND are mathematical concepts from Discreet
mathematics, NOT the general English language use.
These questions are essentially different ways of asking the same thing.
– See their implementation in the next slide.
© UNISA 2018 6
Basic Principles - continued
• A loop should exit if age >21 and • A statement should execute if age >21
averageMark < 60; and averageMark < 60;
• This is probably a while loop or a do • This statement should only execute if both
while loop that should run and stop or conditions are met, therefore:
EXIT when the condition is met;
• In this case, if the age is less than 21, if ((age > 21) && (averageMark < 60))
the loop should carry on; {
• Also if the averageMark is greater cout << “Loop executes when the condition
is met.“ <<endl;
thank 60 then the loop should carry }
on.
• If the age is more than 21, the loop – Then that implies IF (A && B ); both of the
should stop; conditions should be true, for the statement to
• Also if the averageMark is less than execute.
60, the loop should stop.
• It All implies while (A || B):
while ( (age < 21) || (averageMark > 60))
{
cout << “Loop executes… then stops when
condition is met“ <<endl;
}
© UNISA 2018 7
Basic Principles
– Keep together what belongs together.
int a = 10;
a /= 1+1;
cout << a;
You might guess the answer as 11 because most of
us translate the compound assignment operator as
follows:
a = a / 1 + 1; //this is wrong
The statement a /= 1 + 1; is equivalent to
a = a / (1 + 1);
What was on the right hand side stays together. So
the output is 5, not 11.© UNISA 2018 8
Sample loop question
• Suppose two char variables c1 and c2 are initialised and then input repeatedly in the body of a while loop.
The loop has to be executed as long as the value of c1 is not equal to ‘A’ or the value of c2 is equal to ‘A’.
Which of the options below is a correct condition for the while loop?
1. ((c1 == 'A') || (c2 != 'A'))
2. ((c1 != 'A') || (c2 == 'A'))
3. ((c1 == 'A') && (c2 != 'A'))
4. ((c1 != 'A') && (c2 == 'A'))
5. None of the above options is correct.
Correct option: 2
• The loop has to be executed as long as the value of c1 is not equal to ‘A’ or the value of c2 is equal to ‘A’.
• Therefore, as soon as one of the two conditions is not valid anymore, the loop must stop.
• Look at the wording in the question. The question dictates the conditions for which the loop has to be
executed which means the wording can be directly translated to condition.
• However, if the wording is like – the loop has to exit when the value of c1 is equal to ‘A’ or c2 not equal to
A, then it needs a bit more effort to arrive at the condition. See the next question.
•
© UNISA 2018 9
Sample loop question contd…
• Suppose two char variables c1 and c2 are initialised and then input repeatedly in the body of a while loop.
The loop has to exit when the value of c1 is equal to ‘B’ and the value of c2 is not equal to ‘B’. Which of
the options below is a correct condition for the while loop?
1. ((c1 == 'B') && (c2 != 'B'))
2. ((c1 == 'B') || (c2 != 'B'))
3. ((c1 != 'B') && (c2 == 'B'))
4. ((c1 != 'B') || (c2 == 'B'))
5. None of the above options is correct.
Correct option: 4
• Contrary to the previous question, where the conditions under which the loop must be executed, is
described, the condition under which the loop must be exited, are described in this question. The loop must
exit when c1 is equal to ‘B’ and c2 is not equal to ‘B’. Therefore, the condition in the loop should be
‘opposite’ of this.
Opposite of c1 is equal to ‘B’ c1 != ‘B’
Opposite of c2 not equal to B c2 == ‘B’
Opposite of ‘AND’ which connects the above two conditions OR
• Combine all, and you have the answer!
© UNISA 2018 10
1. Variable Diagrams
© UNISA 2018 11
1. Variable Diagrams- Example
The variable diagram should indicate what changes took place in variables.
We DO NOT represent every single line of a program in a variable diagram;it is
only those lines where a variable is declared and/or changes made to them
represented.
The most simplified way to work out the variable diagrams is to label the line
numbers and substitute the values of the variables as you go along;e.g.:
1. valueP = 6;
2. {
3. a = 2;
4. count= 0;
5. while ( 0 < 6)// count =0; valueP = 6; count = 2; count = 4;
//count = 6 then loop exits;
6. {
7. a = a + count +a /2 ;
// a = 2 + 0 +(2 /2) = 3;
a= 3 +2 +(3/2) = 3 ; a = 3 +4 +3 /2 ; a = 0 +2 /2 ;
8. count = 0 +2; count =2;count =4; count= 6;
9. }
10. return a; //current value of a after line 7 updated
11. }
12.
Note: The quality of the past exam papers uploaded on myunisa is very poor. Some of the semicolons are displayed as
commas. Consider them as semicolons, especially those at the end of C++ statements.
© UNISA 2018 12
Solution to previous example
© UNISA 2018 13
Solution contd…
© UNISA 2018 14
1. Variable Diagrams- Example
© UNISA 2018 15
2. Functions
© UNISA 2018 16
Functions contd…
• Built-in functions:
C++ has built-in functions for which we do not
have to write the code. eg: rand(), abs(), sqrt()
etc. These functions are already defined in the
C++ library. We just have to invoke them through
a function call.
• User-defined functions
Users can define their own function. It takes the
form:
return-type function-name(parameter list)
{
body of the function;
} © UNISA 2018 17
3. Data Structures- Arrays & Structs
© UNISA 2018 18
4. Data Structures-2D Arrays
• Two-dimensional Arrays
• eg: int arr[2][3]; declares a 2D array arr
with two rows and 3 columns.
• When a 2D array is initialized as follows the
first dimension is optional.
• eg:- int arr[][3] = { {1,2,3}, {4,5,6}};
© UNISA 2018 19
Solution to Q 6.2.1 of Oct/Nov 2016
exam paper
bool checkNumber(const int arr1[size_of_array], int num1, int & pos1)
{
bool flag = false;
//checking the number in the array
for(int i=0;i< size_of_array;i++)
{
if(arr1[i]==num1)
{
pos1=i;
flag=true;
}
}
return flag;
}
© UNISA 2018 20
5. Reference Parameters
© UNISA 2018 21
5. Reference Parameters - example
Reference parameter variables
declared with the ampersand (&).
Image placeholder
The function twice is called with the
two variables that it should updated.
© UNISA 2018 22
5. Reference Parameter example Code
#include <iostream>
using namespace std;
twice(first, second);
cout << "The first number times two = " << first << endl;
cout << "The second number times two = " << second << endl;
return 0;
}
© UNISA 2018 23
Sample question
Answer is option 3.
© UNISA 2018 24
Sample Question
• [In your past exam papers because
of the poor quality, you are seeing
commas where it should be
semicolon in some places].
• The indentation is given just to
confuse you.
• Here x is not greater than 10. so the
statement after the if structure will
be executed.
• Output is Low
© UNISA 2018 25