C++ Chapter 3
C++ Chapter 3
An Overview of C++
PART ONE
Simple Programs
Example(1)
/*Program #1 A first C++ program.
Enter this program/, then compile and run it.
*/
# include <iostream.h>
# include <conio.h>
// main ( ) is where program execution begins.
int main ) (
return 0 ;
}
Source
C++
Compiler
Code
Object
Code
Exercise:
Write a C++ program to print the following
statement on the screen
"Hi, my name is Saied , What is your name?"
{
int value ; // this declares a variable
value = 1023 ; // this assigns 1023 to value
cout << " This program prints the value=";
cout << value ; // This displays 1023
getch ( );
return 0;
}
Example( 3)
Write a program to convert gallons to liters
( gallon = 4 liters)
The program:
// This program converts gallons to liters.
# include < iostream.h>
# include <conio.h>
int main ( )
{
int gallons , liters ;
cout << "Enter number of gallons :";
cin >> gallons ; // this inputs from the user
liters = gallons * 4 ; // convert to liters
cout << "Liters = " << liters ;
getch ( );
return 0 ;
}
Liters = 40
10
float ,
cout<<"
getch( );
return 0;
}
"<< -------- ;
Exercises:
1- Explain the above program. And use float value instead of
integer value
2- Write C++ program converting US dollars($) to Egyptian
pounds
)1$=5.52 Egyptian pounds (
3- Write C++ program converting Egyptian pounds to US
dollars
(1Egyptian pound= 0.18115942 $ (
4- Write C++ program converting Kilo Bytes to Bytes
) 1 K.B. = 1024 B(
5- Write C++ program converting Mega Bytes to Kilo Bytes
1 M.B.= 1024 K.B(
//AAAAAA
include <iostream.k> ;
# include>conio.k<
Correction is: # include <conio.h >
int main
int main ( )
Correction is: int main( ) ;
return o;
gtch ( )
Correction is : getch( ) ;
getkh ( ) ;
Correction is : getch( ) ;
getch ( ),
Correction is : getch ( );
getch ( );
cout<"Hi",
Correction is : cout<<"Hi" ;
cout << "Hi";
cin <<gallons;
Correction is : cin>>gallons;
cim>>gallons;
Correction is : cin>>gallons;
cin>>gallons;
value = 100 ;
cout << " value = " value ;
getch ( ) ;
return 0 ;
PART TWO
FUNCTIONS
Example(4)
#include <conio.h>
void myfunc ( );
int main ( )
{
cout <<"In main( ) ";
return 0 ;
}
void myfunc ( )
{
cout << " Inside myfunc( ) ";
}
Example(5)
mul (8 , 9 );
getch ( );
return 0;
This program will print 200, 30 and 72 on the screen. When mul( ) is
called, the C++ compiler copies the value of each argument into the
matching parameter. That is, in the first call to mul( ), 10 is copied into x
and 20 is copied into y. In the second call, 5 is copied into x and 6 into y.
in the third call, 8 is copied into x and 9into y.
REMEMBER:
The term argument refers to the value that is used to call a function. The
variable that receives the value of an argument is called a parameter.
Example(6)
// Returning a value
#include<iostream.h>
#include<conio.h>
int mul (int x , int y);
int main ( )
{
int answer ;
answer = mul( 10 , 11) ; // assign return value
Example(7)
Write C++ program which containing 4-functions for addition ,
subtraction, multiplication and division of two numbers x , y.
The program
#include<iostream.h>
#include<conio.h>
int add (int x , int y) ;
int sub (int x , int y) ;
int mul (int x , int y ) ;
int answer;
Example(8)
To see an example, try the following program.
/*This program demonstrates the \n code, which generates a new
line.*/
#include<iostream.h>
#include<conio.h>
int main ) (
{
cout << "one\n ";
cout << "two\n ";
cout << "three ";
cout << "four ";
getch ( );
return 0 ;
}
This program produces the following output:
one
two
threefour
Example(9)
Write C++ program which prints the statements Hi ,My name is Saied,
What is your name?. Each statement in a separate line.
The program
#include<iostream.h>
#include<conio.h>
int main ( )
{
cout << "Hi\n" ;
cout << "My name is Saied\n " ;
cout << "What is your name " ;
getch ( ) ;
return 0 ;
}
Example(10)
Write C++ program which containing 4-functions for addition,
subtraction, multiplication and division of two numbers x , y.
The program
# include<iostream.h>
# include<conio.h>
int add (int x , int y );
int sub (int x , int y ) ;
int mul (int x , int y ) ;
int answer ;
getch ( );
return 0;
}
}
int sub ( int x , int y)
{
return x-y;
int mul ( int x , int y)
{
return x*y ;
}
{
return x/y;
}
Exercise:
1- Write a program which displays the following figure on the screen
*
*
b) #include<iostream.h>
#include<conio.h>
int main ( )
{
return 0;
}
PART THREE
if Statement & for Loop
Condition
False
True
Statements
Executed when
condition is true
Greater than
>=
<
Less than
<=
==
Equality
Example(11)
The following program shows an example of the if statement. It
prompts the user
for two numbers, and reports if the first value is less than the
second.
getch ( ) ;
return 0 ;
}
Example(12)
The following program prints the numbers 1 through 100 on
the screen.
//This program illustrates the for loop statement.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int count ;
for( count=1;count<=100;count=count+1)cout<<count<< ;
getch ( ) ;
return 0 ;
}
The output is :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100
Exercise:
Write a program that prints the numbers 122 through 1000 on the
screen.
Remark:
count=count + 1 equivalent to count ++
The two statements after the if and between the curly braces are
both executed only if x is less than 10.
These two statements, together with the braces, represent a block of
code.
Example(13)
The following program uses a block of code. Enter and run the
program so that
you can see the effect of the block.
//This program demonstrates a block of code.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int a , b ;
cout << Enter first number : ;
cin >> a ;
cout << Enter second number : ;
cin >> b ;
if( a < b ) {
cout << First number is less than second number.\n
;
cout<< Their difference is : <<b-a ;
getch ( ) ;
return 0 ;
}
x=y;
y=y+1;
mul( x , y ) ;
is the same as x = y ; y = y +1 ; mul( x , y ) ;
to a C++ compiler.
Exercise:
Use the above section in all pervious programs.
PART FOUR
Arrays
3.4 ARRAYS
This section discusses the array.
An array is a collection of variables of the same type that are referred
to by a common name.
In C++, arrays may have from one to several dimensions, although the
one-dimensional array
is the most common.
Sample[1]
Sample[2]
Sample[3]
Sample[4]
Sample[5]
Sample[6]
Sample[7]
Sample[8]
Sample[9]
List[0]
List[1]
List[2]
List[3]
List[4]
List[5]
List[6]
Example(14)
The following program loads the array sample with the numbers 0
through 9:
#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=t;//load the array
for (t=0;t<10;t++)cout<<sample [t]<<" ";//display the array
getch( ) ;
return 0 ;
}
1
SAMPLE{!}
2
Sample[2]
3
Sample[3]
4
Sample[4]
5
Sample[5]
6
Sample[6
7
SAMLE{7]
8
Sample[8
9
Sample[9]
Example(14.1)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=9-t;//load the array
for(t=0;t<10;t++)cout<<"sample["<<t<<"]="<<sample[t]<<"\n
";//display the array
getch( ) ;
return 0 ;
}
Example(14.2)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=9-t;//load the array
for(t=0;t<10;t++)cout<<"sample["<<t<<"]="<<sample[t]<<"\n
";//display the array
getch( ) ;
return 0 ;
}
Example(15)
The following program creates an array of ten elements, assigns
each element a random value, and then calculates and displays its
maximum and minimum values.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>//Header for the function rand( )
int main ( )
{
int i, minvalue , maxvalue;
int list[10];
for (i=0;i<10;i++)list[i]=rand( );
//find minimum value
minvalue =32767;
for (i=0;i<10;i++)if(minvalue> list[i]) minvalue= list[i] ;
cout<<"minimum value="<<minvalue<<"\n";
//find maximum value
maxvalue =0;
for (i=0;i<10;i++)if(maxvalue< list[i]) maxvalue=list[i];
cout<<"maximum value="<<maxvalue<<"\n";
getch( ) ;
return 0 ;}
Example(16)
Write a program to find the maximum and minimum values
for the array
called saied with 12 elements such that:
22
55
27
15
99
-2
51
#include <iostream.h>
#include <conio.h>
int main ( )
{
int i, minvalue ,maxvalue;
int saied[12];
saied[0]=22;saied[1]=4;saied[2]=9;saied[3]=55;saied[4]=1;
saied[5]=6;saied[6]=27;saied[7]=15;saied[8]=99;saied[9]=3;
saied[10]=-2;saied[11]=51;
for (i=0;i<12;i++)cout<< saied[i]<<" "; //display the array
//find minimum value
minvalue=100;
for (i=0;i<12;i++)if(minvalue> saied[i]) minvalue= saied[i];
cout<<"minimum value="<<minvalue<<"\n";
3.4.2Array Initialization
C++ allows the initialization of arrays. The general form of array
initialization is
Similar to that of other variables, as shown here:
type array name [size] = { value list} ;
Where the value list is a comma-separated list of constants that are typecompatible
with the base type of the array.
The first constant will be placed in the first position of the array, the
second constant in the second position , and so on.
In the following example, a 10-element integer array is initialized with
the numbers
1through 10 :
Example(16.1)
Use the above notation in example(16)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int i, minvalue ,maxvalue;
int saied[12]={22,4,9,55,1,6,27,15,99,3,-2,51};
for (i=0;i<10;i++)cout<< saied[i]<<" "; //display the array
//find minimum value
minvalue=100;
for (i=0;i<12;i++)if(minvalue> saied[i]) minvalue= saied[i];
cout<<"minimum value="<<minvalue<<"\n";
Example(17)
The following program sorts an array of integers (ascending order) that
contains random values.
// ascending order
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main( )
{
int nums[10];
int a ,b ,t ,size;
size=10;//number of elements to sort
for (t=0;t< size; t++) nums[t]=rand( );//loading the array
//displaying the array
cout<<" original array is : ";
for (t=0;t< size; t++) cout<<nums[t]<<" ";
cout<<"\n";
EXERCISE:
Write a program for sorting an array of random integers (descending
order) with size 10
// descending order
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main( )
{
int nums[10];
int a ,b ,t ,size;
size=10;//number of elements to sort
for (t=0;t< size; t++) nums[t]=rand( );//loading the array
//displaying the array
cout<<" original array is : ";
for (t=0;t< size; t++) cout<<nums[t]<<" ";
cout<<"\n";
PART FIVE
String
3.5 Strings
In C++, a string is defined as a character array that is terminated by a
null.
A null is specified using \0 , and is zero.
Because of the null terminator, it is necessary to declare a character array
to be one character longer than the largest string that it would hold.
Forexample, if you want to declare an array str that could hold a 10
character string, you would write:
char str[11] ;
Specifying the size as 11 makes room for the null at the end of the string.
A string constant is a list of characters enclosed in double quotes.
Here are some examples:
HELLO
I like C++
#$%@@#$
like this:
It is not necessary to manually add the null onto the end of string
constants ;
\the C++ compiler does this for you automatically.
Therefore, the string HELLO will appear in memory
\0
Example(18)
The following program reads a string entered by the user:
//Using cin to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
int main( )
{
char str[80];
cout<<" Enter a string: ";
cin>>str ; //read string from keyboard
cout<< "Here is your string : ";
cout<<str ;
getch( ) ;
return 0 ; }
Example(19)
This version of the preceding program uses gets( ) to allow the entry of
the strings containing spaces.
//Using gets( ) to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
char str[80];
cout<<" Enter a string: ";
gets (str) ; //read string from keyboard
cout<< "Here is your string : ";
cout<<str ;
getch( ) ;
return 0 ;
}
Example(20)
Write a program to read a string from the keyboard using cin.
The Program
//Using cin to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
int main( )
{
char name[80];
cout<<What is your name? ;
cin>>name;
cout<< Here is your name :;
cout<<name;
getch( ); return 0 ; }
Example(20.1)
Write a program to read a string from the keyboard using gets.
The Program
//Using gets to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
char name[80];
cout<<" What is your name? ";
gets(name) ;
cout<< "Here is your name : ";
cout<<name ;
getch( ) ;
return 0 ;
}