0% found this document useful (0 votes)
14 views13 pages

Unit III C++

Uploaded by

Big Bro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Unit III C++

Uploaded by

Big Bro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit – III

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:

o Array is the simplest kind of data structure.


o The calculation of lists & matrices become easy with the use of arrays.
o Array makes loops very effective.
o Array permits efficient random access.
o Array elements can be assessed instantly.

Types of an array

An array is of three types:


 Single Dimensional Array
 Two Dimensional Array
 Multidimensional 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’};

Programs: 1. To find the maximum and minimum value in an array.

#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];

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( );
}

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

data_type array_name [no. of rows] [no. of columns];

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];
}
}

for( i = 0; i < 10; i++ )


{
for(j = 0; j<10; j++)
{
C[i][j] = a[i][j] + b[i][j];
}
}
cout << endl <<“The Sum of the matrices are :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cout<< c[i][j] << “\t”;
}
cout<<endl;
}
getch( );
}

2. Multiplication of two matrices.


#include<iostream.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10], i, j, k;
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];
}
}

for( i = 0; i < 10; i++ )


{
for(j = 0; j<10; j++)
{
c[i][j] = 0;
for(k = 0; k<10; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
cout << endl <<“The Multiplication of the matrices are :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cout<< c[i][j] << “\t”;
}
cout<<endl;
}
getch( );
}

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];
}
}

for( i = 0; i < 3; i++ )


{
for(j = 0; j<5; j++)
{
b[i][j] = a[j][i];
}
}
cout << endl <<“The Transpose of the matrix is :-“;
for( i = 0; i < 3; i++ )
{
for(j = 0; j<5; j++)
{
cout<< b[i][j] << “\t”;
}
cout<<endl;
}
getch( );
}

 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( );
}

int sum(int m[ ], int n)


{
int add, j;
add = 0;
for(j = 0; j<n; j++)
{
add = add + m[j];
}
return(add);
}

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.

Let’s define a one dimensional array ‘b’ of 5 elements


int b[5] = {5, 6, 2, 8, 9};
Now, the statement
int *ptr;
ptr = b;

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.

Example: Accessing elements of two dimensional array using pointers.

#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.

The syntax for declaring array of pointers is

data_type *pointer_array[size];

For example:

int *b[3];

Here, b is an array of pointers to integer data type.

Example: Use of array of pointers.

#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:

pointer variable = new datatype;

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;
}

delete [] a; // free memory pointed to by a.


getch( );
}

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 (“
").

Strings are declared as an array of characters. The general syntax is

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:

char str[ ]="abcd";


or
char str[5]="abcd";
or
char str[5]={'a','b','c','d','\0'};
or
char str[ ]={'a','b','c','d','\0'};

C++ programming language supports number of library string functions. All the library function of String is
available in String.h header file.

S.N. Function Purpose


1 strcpy(s1, s2) Copies string s2 into string s1.
2 strcat(s1, s2) Concatenates string s2 at the end of string s1.
3 strlen(s1) Returns the length of string s1.
4 strcmp(s1, s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strrev(s1) Reverse the string s1, except the null character.
6 strlwr(s1) Converts the string letters into lowercase.

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( );
}

Program 2: To calculate the length of the string using library function.

#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( );
}

Program 3: To copy a string to another without using library function.

#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( );
}

Program 4: To copy a string to another using library function.

#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( );
}

Program 5: To concatenate two strings without using library function.

#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( );
}

Program 6: To concatenate two strings using library function.

#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( );
}

Introduction to Class and Object:

Already defined in Features/Concepts of OOPS.

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( );
}

You might also like