Professional Documents
Culture Documents
E.C
Chapter 2
Introduction to C++ programing
CHAPTER-3
Control statements
Boolean Expressions
Example
int num1=100,num2=4;
!(num2);//returns false.
Selection Statements
Selection statements are mostly used with Boolean expressions. There are two
types of selection statements
If statements
Switch statements
1. If statement
The if statements enables the programmer to test the condition and branch to
different parts of the code.
Syntax
if (expression)
(Statements)
Example
#include<iostream.h>
By Deres G. RVU Page 3
Introduction to programming I 2007
E.C
void main()
int x;
cin>>x;
if(x>0)
else [ if ]
often the program should take one branch if the condition is true and another
branch if the condition false. In such situation you can use else and or else if to
handle multiple conditions.
Syntax
If(Boolean expression)
Statment(s)
Statement (s)
Statement(s)
else
Statement(s)
Example
#include<iostream.h>
void main()
int x;
cin>>x;
if(x>0)
else if(x<0)
else
Nested if
Syntax
if(Boolean expression)
if(Boolean expression)
Statement(s)
Statement(s)
else
Statement(s)
Statement(s)
else
Statement(s)
Example
//Demonstrates nested if
#include<iostream.h>
void main()
int x;
cin>>x;
if(x>=0)
if(x>0)
else
else
Syntax
switch expression
default: statement(s);
Switch evaluates expression or equality with case values. If much is found the
corresponding statement is executed. The key word break prevents execution from
following through the next statement. The default statement will be executed only
if expression does not any of case statement.
Example
#include<iostream.h>
void main()
char grade;
cin>>grade;
swich (grade)
default: cout<<"Invalid\n";
LOOPS
Loops statements are useful when you want the same piece of code to be executed
a number of times. There are different loops statements; each one is designed for
slightly different programming situations.
Syntax
statement;
Example
#include<iostream.h>
void main()
cout<<ch<<endl;
syntax
while(expression)
statement(s);
Example
#include<iostream.h>
void main()
char ch;
ch='a';
while (ch<='z')
cout<<ch<<endl;
By Deres G. RVU Page 10
Introduction to programming I 2007
E.C
ch++;
Exercise
Write a program that accepts any non-negative integer, and return the factorial of
the number.
The Do statement
with the do loop the evaluation expression comes after the execution statement.
hence the do statement will always executed the loop body at least once.
Syntax
do
statement(s);
}while (expression)
Example
#include<iostream.h>
void main()
int n, i=1;
long sum=0;
cin>>n;
if(n>0)
do
sum=sum+i;
i++;
}while (i<=n);
else
Example
for(int j=0;j<4;j++)
cout<<(i+j)<<" ";
cout<<endl;
}
By Deres G. RVU Page 12
Introduction to programming I 2007
E.C
CHAPTER -4
Functions (Modules)
Function is a subprogram that can act on data return value. a function is always
called by another function(exception of main function) which is called by operator
system. The called function must often able to receive data from calling function.
This data is passed to called function through its parentheses.
There are various type of function such as void functions and a function that return
a value
Using function in a program requires declaring and defining the function. The
declaration is called the function prototype. It tells the compiler Name, return type
and parameters of the function. Function definition tells how the function works.
Syntax
function prototype
function call
function Definition
Example
int sample( );
void main( )
int y=sample( );
.
}
Example
void main( )
void main ( )
cout<<"Input 1: ";
cin>>num1;
cin>>num2;
int add;
add=j+k;
return(add)
syntax
Type of argument contain data types of argument that must be passed to the
function and optional names of arguments.
The calling statement should not include type name in the argument list.
By Deres G. RVU Page 15
Introduction to programming I 2007
E.C
The definition of a function consists of function header and its body. the header is
exactly like a function prototype.
The body of a function is a set of statement enclosed in Brasses. every function has
a return type. The function that has not return value its return type will be void.
#include<iostream.h>
void main( )
int nolines;
cin>>nolines;
while(nolines<0 || nolines>20
cin>>nolines;
cout<<endl<<endl;
noblanks=30;
noblanks- -;
int count
cout<<" ";
cout<<" * ";
cout<<endl;
Chapter 5
Arrays
What is array?
Array is a structured with homogenous elements of data type. It is a collection of
data storage locations each of which holds the same type of data. It is storage
location is called an element of the array.
The program declares an array by writing data type followed by array name and
subscript. The subscript (index) is number of elements in array sorrowended by
square brackets[ ].
One dimensional array is column data items. all of them have the same data type
that are collectively referred by the same name.
Example
float result[50]
The program access each of the array element by specifying array name followed
by index enclosed by square brackets [ ]. Array elements are count from zero to n-
1 when n is the size of the array.
Example
Example
#include<iostream.h>
void main( )
float result[50];
cin>>result[i];
Home work
Write a simple code that accepts results of n students and displays number of
students who scored less than average .
Multi-Definitional array
Example
int A[10][20]
initializing array
Example
int A[4]={1,2,3,4};
int A[ ]={1,2,3,4,5,6};
float A[2][3]={{1,2,3}{4,5,6}};
Example.
#include<iostream.h>
void main( )
{
int row, col, i, j ;
folat table[100][100];
cout<<"Enter number of rows : ";
cin>>row;
cout<<" Enter number columns :";
cin>>col;
cout<<" Now enter "<<row<<" X "<<col<<"real numbers\n";
A. Write a program that accepts n real numbers and displays max and min from
the list.
B. Write a program that returns the sum of two Matrix
Example
/* The following program finds average of numbers that are stored in array*/
#include<iostream.h>
void main( )
float av, sum=0;
int num[10];
cout num[10]; i;
cout<<"Enter the numbers \n ";
for (i=0; i<10; i++)
{
cout<<"Enter number "<<i+1 <<" : ";
By Deres G. RVU Page 21
Introduction to programming I 2007
E.C
cin>>num[i];
}
for (i=0; i<10; i++)
sum+=num[i];
av=sum/10;
cout<<"\n\n The average is "<<av;
}
Example
/* The following program finds the highest temperature out of the three cities of four
seasons*/
void main( )
{
const int rows=3;
const int columns=4;
int seasontemp[rows][columns]={{26,34,22,17},{24,32,19,13},{28,38,25,20}};
int highest=0, i, j;
for (j=0; i<rows, ++i)
for( j=0; j<columns; ++j)
{
if(temp[i][j]>highest
highest=temp[i][j];
}
cout<<" The highest temperature is ="<<highest;
}