Grade 11 ICTNote
Grade 11 ICTNote
1. Start
2. Sum = 0 0
3. Get a value
4. sum = sum + value
5. Go to step 3 to get next Value
6. Output the sum
7. Stop
PSEUDOCODE
Pseudocode is one of the tools that can be used to write a preliminary plan that can be developed into a computer program.
Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax. It is, as
the name suggests, pseudo code it cannot be executed on a real computer, but it models and resembles real programming
code, and is written at roughly the same level of detail.
For example, for finding the average of six numbers, and the
sum of the numbers is given.
The pseudocode will be as follows
Start
Get the sum
Average = sum / 6
Output the average
Stop
C++ = C plus plus
C++ is a middle-level programming language developed by Bjarne Stroustrup at Bell Labs. C++ runs on a variety of platforms,
such as Windows, Mac OS, and the various versions of UNIX. C++ adds object-oriented features to its predecessor, C.
#include <iostream>
int main() {
return 0;
After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program
follows the C++ syntax
-indentation is for the convenience of the reader; compiler ignores all spaces and new line; the delimiter for the compile is
the semicolon.
-If there are no errors, it translates the C++ program into a program in machine language which you can execute
The istream and ostream classes derived from ios form a user-friendly interface for stream manipulation. The istream class
is used for reading streams and the ostream class is used for writing to streams.
The operator >> is defined in istream and << is defined in ostream. The iostream class is derived by multiple inheritance from
istream and ostream and thus offers the functionality of both classes. Further stream classes , a file management class. This
allows the developer to use the techniques described for file manipulation. These classes, which also contain methods for
opening and closing files.
Any C++ program file should be saved with file name extension “ .CPP ”
The first character is the #. This character is a signal to the preprocessor. Each time you start your
compiler, the preprocessor runs through the program and looks for the pound (#) symbols and act on
those lines before the compiler runs.
include instruction is a preprocessor instruction that directs the compiler to include a copy of the file
specified in the angle brackets in the source code.
The effects of line 1, i.e. include<iostream> is to include the file iostream into the program as if the
programmer had actually typed it.
When the program starts, main() is called automatically.
Every C++ program has a main() function.
If the return value type for main() is void, means main function will not return a value to the caller
(which is the operating system).
The main function can be made to return a value to the operating system.
The Left French brace “{“signals the beginning of the main function body and the corresponding Right
French Brace “}” signals the end of the main function body. Every Left French Brace needs to have a
corresponding Right French Brace.
The lines we find between the braces are statements or said to be the body of the function.
A statement is a computation step which may produce a value or interact with input and output streams.
The end of a single statement ends with semicolon (;).
A brief look at cout and cin
cout is an object used for printing data to the screen.
To print a value to the screen, write the word cout, followed by the insertion operator also called output
redirection operator (<<) and the object to be printed on the screen.
Syntax: Cout<<Object;
The object at the right hand side can be:
• A literal string: “Hello World”
• A variable: a place holder in memory
cin is an object used for taking input from the keyboard.
To take input from the keyboard, write the word cin, followed by the input redirection operator (>>)
and the object name to hold the input value.
Syntax: Cin>>Object
cin will take value from the keyboard and store it in the memory. Thus the cin statement needs a
variable which is a reserved memory place holder.
Both << and >> return their right operand as their result, enabling multiple input or multiple output
operations to be combined into one statement.
The following example will illustrate how multiple input and output can be performed:
E.g.: cin>>var1>>var2>>var3;
Here three different values will be entered for the three variables. The input should be
separated by a space, tan or newline for each variable.
cout<<var1<<”, “<<var2<<” and “<<var3;
Here the values of the three variables will be printed where there is a “,” (comma) between
the first and the second variables and the “and” word between the second and the third.
Putting Comments on C++ programs
A comment is a piece of descriptive text which explains some aspect of a program.
Program comments are text totally ignored by the compiler and are only intended to inform the reader
how the source code is working at any particular point in the program.
C++ provides two types of comment delimiters:
Single Line Comment: Anything after // {double forward slash} (until the end of the line on which it
appears) is considered a comment.
o Eg: cout<<var1; //this line prints the value of var1
Multiple Line Comment: Anything enclosed by the pair /* and */ is considered a comment.
Eg:
/*this is a kind of comment where
Multiple lines can be enclosed in
one C++ program */
Comments should be used to enhance (not to hinder) the readability of a program. A comment should
be easier to read and understand than the code which it tries to explain. Over-use of comments can
lead to even less readability.
Use of descriptive names for variables and other entities in a program, and proper indentation of the
code can reduce the need for using comments.
Operators
An operator is a symbol that makes the machine to take an action.
Different Operators act on one or more operands and can also have different kinds of operators.
C++ provides several categories of operators, including the following:
Assignment operator
Arithmetic operator
Relational operator
Logical operator
Increment/decrement operator
Conditional operator
Comma operator
Assignment operator (=).
The assignment operator causes the operand on the left side of the assignment statement to have its value
changed to the value on the right side of the statement.
Syntax: Operand1=Operand2;
Operand1 is always a variable
Operand2 can be one or combination of:
• A literal constant: Eg: x=12;
• A variable: Eg: x=y;
• An expression: Eg: x=y+2;
Compound assignment operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=).
Compound assignment operator is the combination of the assignment operator with other operators
like arithmetic and bit wise operators.
The assignment operator has a number of variants, obtained by combining it with other operators.
E.g.: Value += increase; is equivalent to value = value + increase;
a -= 5; is equivalent to a = a – 5;
a /= b; is equivalent to a = a / b;
price *= units + 1 is equivalent to price = price * (units + 1);
Arithmetic operators (+, -, *, /, %).
Except for remainder or modulo (%), all other arithmetic operators can accept a mix of integers and real
operands. Generally, if both operands are integers then, the result will be an integer. However, if one or
both operands are real then the result will be real.
When both operands of the division operator (/) are integers, then the division is performed as an
integer division and not the normal division we are used to.
Integer division always results in an integer outcome.
Division of integer by integer will not round off to the next integer
E.g.: 9/2 gives 4 not 4.5
-9/2 gives -4 not -4.5
To obtain a real division when both operands are integers, you should cast one of the operands to
be real. E.g.: int cost = 100;
int volume = 80;
double unitPrice = cost/(double)volume;
The module (%) is an operator that gives the remainder of a division of two integer values. For
instance, 13 % 3 is calculated by integer dividing 13 by 3 to give an outcome of 4 and a remainder of
1; the result is therefore 1.
E.g.:
a = 11 % 3 => a is 2
Relational operator (==, ! =, > , <, >=, <=).
In order to evaluate a comparison between two expressions, we can use the relational operator.
The result of a relational operator is a Boolean value that can only be true or false according to the
result of the comparison.
E.g.:
(7 = = 5) would return false or returns 0
(5 > 4) would return true or returns 1
The operands of a relational operator must evaluate to a number. Characters are valid operands
since they are represented by numeric values.
For E.g.: ‘A’ < ‘F’ would return true or 1. it is like (65 < 70)
Logical Operators (! , &&, ||):
Logical negation (!) is a unary operator, which negates the logical value of its operand. If its operand is
non zero, it produces 0, and if it is 0 it produce 1.
Logical AND (&&) produces 0 if one or both of its operands evaluate to 0 otherwise it produces 1.
Logical OR (||) produces 0 if both of its operands evaluate to 0 otherwise, it produces 1.
E.g.: !20 //gives 0
10 && 5 //gives 1
10 || 5.5 //gives 1
10 && 0 // gives 0
N.B. In general, any non-zero value can be used to represent the logical true, whereas only zero represents the
logical false.
Increment/Decrement Operators: (++) and (--)
The auto increment (++) and auto decrement (--) operators provide a convenient way of, respectively,
adding and subtracting 1 from a numeric variable.
E.g.: if a was 10 and if a++ is executed then a will automatically changed to 11.
Prefix and Postfix:
The prefix type is written before the variable. Eg (++ myAge), whereas the postfix type appears after
the variable name (myAge ++).
Prefix and postfix operators cannot be used at once on a single variable: Eg: ++age-- or --age++ or
++age++ or - - age - - is invalid
In a simple statement, either type may be used. But in complex statements, there will be a difference.
The prefix operator is evaluated before the assignment, and the postfix operator is evaluated after the
assignment.
E.g.
int k = 5;
(auto increment prefix) y= ++k + 10; //gives 16 for y
(auto increment postfix) y= k++ + 10; //gives 15 for y
(auto decrement prefix) y= --k + 10; //gives 14 for y
(auto decrement postfix) y= k-- + 10; //gives 15 for y
Looping
Many programming problems are solved by repeatedly acting on the same data. There are two ways to do this: recursion
and iteration. Iteration means doing the same thing again and again. The principal method of iteration is the loop.
The condition tested in a whileloop can be any valid C++ expression. As long as that condition remains true, the
whileloop will continue. You can create a loop that will never end by using the number 1 for the condition to be
tested. Since 1 is always true, the loop will never end, unless a breakstatement is reached.
#include<iostream>
using namespace std;
int main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << “sum is “ << sum << endl;
}
return 0;
}
for Loops
A for loop combines three steps into one statement. The three steps are initialization, test, and increment. A for statement
consists of the keyword for followed by a pair of parentheses. Within the parentheses are three statements separated by
semicolons. A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
Syntax
for ( init; condition; increment ) {
statement(s);
}
The if-else Statement
• A branching statement that chooses between two possible actions.
• syntax
if (Boolean_Expression)
Statement_1
else
Statement_2
example
if (count < 3)
total = 0;
else
total = total + count;
1. Write a program that asks the user to type 2 integers A and B and exchange the value of A and B.
Solution #1
#include<iostream>
using namespace std;
int main()
{
double a,b,temp;
temp=a;
a=b;
b=temp;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
return 0;
}
2. Write a program that asks the user to type the price without tax of one kilogram of tomatoes, the number of
kilograms you want to buy and the tax in percent units. The program must write the total price including taxes.
Solution #1
//by sniper.org
#include<iostream>
using namespace std;
int main()
{
double price_without_taxes,weight,taxes,total;
return 0;
}
3. Write a program that asks the user to two find the square of a given number.
Solution #1
#include <iostream>
using namespace std;
int main()
{
int j;
for(j=0; j<15; j++)
cout << j * j << " ";
cout << endl;
return 0;
}
4. Write a program that asks the user to two find the cube of a given number.
#include <iostream>
Using namespace std;
#include <iomanip.h>
int main(){
int numb;
for(numb=1; numb<=10; numb++)
{
cout << setw(4) << numb;
int cube = numb*numb*numb;
cout << setw(6) << cube << endl;
}
return 0;
}