Unit III C++
Unit III C++
Array: An array is a collection of variables of same type i.e. collection of homogeneous data referred by
a common name. In memory, array elements are stored in a continuous location. Each element of an array can
be referred to by an array name and a subscript or index. The subscript is enclosed in square brackets.
Syntax:
data_type array_name[size];
Example:
int age[20];
According to the rules of C++ language, 1st element of array is stored at location age[0] , 2nd at
age[1] & so on.
Advantages of array:
Types of an array
Single Dimensional Array: A single dimensional array has only a single subscript. A
subscript is a number in brackets that follows an array’s name. This number can identify the
number of individual elements in the array. Single dimensional arrays are mostly used for linear
problems.
Syntax:
data_type array_name[size];
Example:
int age[20];
A one dimension array can be initialized as simple variables are initialized. The numbers of
values equal to its size are supplied. For example:
int age[5] = {14, 34, 23, 67, 16};
char name[4] = {‘A’, ‘M’, ‘I’, ‘T’};
#include<iostream.h>
#include<conio.h>
void main( )
{
int max, min, a[15], i;
clrscr( );
cout << “Enter the values:-“;
for( i = 0; i < 15; i++ )
{
cin >> a[i];
}
max = a[0];
min = a[0];
cout << endl << “The maximum value is :-“ << max;
cout << endl << “The minimum value is :-“ << min;
getch( );
}
2. Sorting of an array.
#include<iostream.h>
#include<conio.h>
void main( )
{
int temp, a[15], i;
clrscr( );
for( i = 0; i < 15; i++ )
{
cin >> a[i];
}
for( i = 0; i < 15; i++ )
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
cout << endl << “The maximum value is :-“ << max;
cout << endl << “The minimum value is :-“ << min;
getch( );
}
Two Dimensional Arrays: A multidimensional array has more than one subscript. A two
dimensional array has two subscripts, a three dimensional array has three subscripts, and so on.
The two dimensional array is used in matrices. The first subscript tells the number of rows and
the second subscript tells about the columns.
The syntax of two dimensional array is
Example
int a[3][3];
the above statement declare two dimensional array having 3 rows and 3 columns.
Programs: 1. Sum of two matrices.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10], i, j;
clrscr( );
cout << “Enter the elements of first matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> a[i][j];
}
}
cout << endl <<“Enter the elements of Second matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> b[i][j];
}
}
3. Transpose of a matrix.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[5][3], b[3][5], i, j;
clrscr( );
cout << “Enter the elements of the matrix :-“;
for( i = 0; i < 5; i++ )
{
for(j = 0; j<3; j++)
{
cin >> a[i][j];
}
}
Multidimensional Arrays: Arrays can have higher dimensions. An array with more
than one subscript is defined as multidimensional array.
The syntax of multidimensional array is
type array_name[s1][s2]…..[sn];
here si is the size of the ith dimension.
The number of size specifiers depends upon the dimension of the array.
For example:
int a[3][5][12];
float num[5][4][5][3];
here, a is the three dimensional array declared to contain 180 integer type elements.
Similarly num is four dimensional array containing 300 data elements of floating point
type.
Passing Array to a Function: Just like passing the primitive type values to a function, it is also
possible to pass an array to a function. To pass an array to a function, the array name must be specified without
any square brackets as an actual argument within function call.
Syntax:
r_type function_name(array_name[ ], size)
{
body of function;
}
Example: Sum of the elements
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[15], i, s;
int sum(int [ ], int);
clrscr( );
cout<<”\n Enter the numbers:-“;
for( i = 0; i<15; i++)
{
cin>>a[i];
}
s = sum(a, 15);
cout<<”\n The sum of elements are :-“ << s;
getch( );
}
Pointer to One Dimensional Array: In C++, there is a close resemblance between arrays and
pointers and can be used interchangeably. Pointers provide an efficient and faster way for accessing and
manipulating array elements. An array name can serve as a pointer to the first element of an array. This
relationship makes both pointer and array more versatile and simplifies pointer arithmetic.
Here, ptr is a pointer variable pointing to integer type array ‘b’ and contains the address of the first
element of an array. So the statements
ptr = b is similar to ptr = &b[0]
In order to access the value stored at b[0], the following statement is used
cout<<*ptr;
Example:
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[15];
int *ptr, i, sum = 0;
clrscr( );
cout<<”\n Enter the numbers:-“;
for( i = 0; i<15; i++)
{
cin>>a[i];
}
ptr = a;
for( i = 0; i<15; i++)
{
sum = sum + *(ptr + i);
}
cout<<”\n The sum of elements are :-“ << sum;
getch( );
}
Pointer to Two Dimensional Array: Pointers can not only point to one dimensional array but
can extend the use of pointers to access elements of two dimensional arrays and so on.
Let’s define a two dimensional array ‘b’ of 2 rows and 3 columns
int b[2][3] = {{5, 6, 2}, {8, 9, 3}};
Now, the statement
*(*(b+0))
show the value of b[0][0] i.e. value of the first element of two dimensional array. Similarly, to access
the second element of first row
*(*(b+0)+1) and so on.
#include<iostream.h>
#include<conio.h>
void main( )
{
int b[2][3] = {{5, 6, 2}, {8, 9, 3}};
int i, j, sum = 0;
clrscr( );
cout<<”\n Elements of two dimensional are:-\n“;
for( i = 0; i<2; i++)
{
for( j = 0; j<3; j++)
{
cout<<*(*(b+i)+j) << “ “;
}
cout<<”\n”;
}
getch( );
}
Array of Pointers: Just as defining an array of basic data types, in the same way an array of pointers
can also be defined. The only difference is that it contains a collection of addresses instead of values.
data_type *pointer_array[size];
For example:
int *b[3];
#include<iostream.h>
#include<conio.h>
void main( )
{
int *a[2];
int k = 9, m = 10, i;
clrscr( );
a[0] = &k;
a[1] = &m;
for( i = 0; i<2; i++)
{
cout<<”\n Address : “<< a[i] << “Pointing to :” << *a[i];
}
getch( );
}
Dynamic Arrays: In programming there may be scenarios where programmers may not know how
much the memory needed to store particular information in a defined variable and the size of required memory
can be determined at run time. To allocate memory at run time a special operator is used in C++ which returns
the address of the space allocated. This operator is called new operator.
The dynamic arrays are created to alter the size of the array at run time or reallocating an array. Both of these
are done by declaring an array as a pointer and using the new operator to allocate memory, and delete to free
memory that is no longer needed.
Syntax:
Example:
#include<iostream.h>
#include<conio.h>
void main( )
{
int* a = NULL; // Pointer to int, initialize to nothing.
int n;
cout<<”Enter the size for array:-“;
cin >> n;
a = new int[n]; // dynamically allocation of memory for array.
for (int i=0; i<n; i++)
{
a[i] = 0;
}
Strings: String is a collection of character or group of character, it is achieve in C++ language by using
array of characters. The string in C++ language is one-dimensional array of character which is terminated by a
null character '\0'. In other words string is a collection of character which is enclose between double quotes (“
").
char var_name[length];
For Example:
char ch[10];
Character arrays can be initialized when they are declared. The different ways to initialize the array are:
C++ programming language supports number of library string functions. All the library function of String is
available in String.h header file.
Programs on strings
Program 1: To calculate the length of the string without using library function.
#include<iostream.h>
#include<conio.h>
void main( )
{
char ch[20];
int i;
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
i = 0;
while(ch[i]!=’\0’)
{
i++;
}
cout<<”\nThe length of the string is:-“<<i;
getch( );
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main( )
{
char ch[20];
int i;
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
i = strlen(ch);
cout<<”\nThe length of the string is:-“<<i;
getch( );
}
#include<iostream.h>
#include<conio.h>
void main( )
{
char ch[20], ch1[20];
int i;
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
i = 0;
while(ch[i]!=’\0’)
{
ch1[i] = ch[i];
}
ch1[i] = ‘\0’;
cout<<”\nThe copied string is:-“<<ch1;
getch( );
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main( )
{
char ch[20], ch1[20];
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
strcpy(ch1, ch);
cout<<”\nThe copied string is:-“<<ch1;
getch( );
}
#include<iostream.h>
#include<conio.h>
void main( )
{
char ch[20], ch1[40];
int i, j;
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
cout<<”\n Enter the second string:-“;
cin.get(ch1, 20);
i = 0;
j = 0;
while(ch1[i]!=’\0’)
{
i++;
}
while(ch[j]!=’\0’)
{
ch1[i] = ch[j];
i++;
j++;
}
ch1[i] = ‘\0’;
cout<<”\nString after concatenation is:-“<<ch1;
getch( );
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main( )
{
char ch[20], ch1[40];
clrscr( );
cout<<”Enter the String:-“;
cin.get(ch, 20);
cout<<”\n Enter the second string:-“;
cin.get(ch1, 20);
strcat(ch1, ch);
cout<<”\n String after concatenation is:-“<<ch1;
getch( );
}
Declaring Members and Methods in a Class: The body of a class consists of variables and
associated functions which are collectively known as members of the class. The variables declared inside a
class are known as data members and functions are known as member function or methods. The definition of a
member function or method of class can be inside or outside the class construct.
class rectangle
{
private:
int a, b; // data members
public:
void setdata(int x, int y) // member function
{
a = x;
b = y;
}
void area( ) // member function
{
int ar = a * b;
cout<<”\n Area of Rectangle is :-“ << ar;
}
};
Declaring Objects: Once the class is created, one or more objects can be created from the class as
objects are instance of the class. Objects are also declared same as the declaration of normal variables.
Syntax:
class_name object_name;
Example:
class addition
{
private:
int x, y, c;
public:
void sum( )
{
x = 10;
y = 20;
c = x+y;
cout << “The sum is :-“ << c;
}
};
void main( )
{
addition a1; //object declaration
a1.sum( );
getch( );
}