Saturday, 3 January 2026

Array in C++


 Array Notation in C++
In C++, array notation refers to the way we declare, access, and use arrays using square brackets [].

1. Declaration of an Array
data_type array_name[size];
Example:
int marks[5];
Here:
int → data type
marks → array name
5 → number of elements

2. Initialization of an Array
(a) At the time of declaration
int a[5] = {10, 20, 30, 40, 50};

(b) Without specifying size
int a[] = {1, 2, 3, 4};

(c) Partial initialization
int a[5] = {1, 2};
Remaining elements become 0.

3. Accessing Array Elements (Array Notation)
Array elements are accessed using index notation:
array_name[index];
Example:
cout << a[0];   // prints first element
Note: Index starts from 0 and ends at size - 1.

4. Storing Values Using Array Notation
for(int i = 0; i < 5; i++)
{
    cin >> marks[i];
}

5. Printing Array Elements
for(int i = 0; i < 5; i++)
{
    cout << marks[i] << " ";
}

6. One-Dimensional Array Example
#include <iostream>
using namespace std;
int main()
{
    int a[3] = {10, 20, 30};
    cout << a[1];   // Output: 20
    return 0;
}

7. Two-Dimensional Array Notation
int mat[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
Access element:
mat[1][2];   // value is 6

Array Declaration in C++
An array in C++ is a collection of elements of the same data type stored in contiguous memory locations.

General Syntax
data_type array_name[size];

Examples of Array Declaration
1. Integer Array
int a[5];
Declares an array a that can store 5 integers.

2. Float Array
float marks[10];
Declares an array to store 10 float values.

3. Character Array
char name[20];
Declares an array to store 20 characters.

4. Declaration with Initialization
int num[4] = {10, 20, 30, 40};

5. Declaration Without Size
int num[] = {1, 2, 3, 4, 5};
The compiler automatically decides the size.

6. Partial Initialization
int num[5] = {1, 2};
Remaining elements are initialized to 0.

7. Two-Dimensional Array Declaration
int matrix[3][3];

With initialization:

int matrix[2][2] = {{1, 2}, {3, 4}};

Array Initialization in C++
Array initialization in C++ means assigning values to array elements at the time of declaration or later in the program.

1. Initialization at the Time of Declaration
int a[5] = {10, 20, 30, 40, 50};

2. Initialization Without Specifying Size
int a[] = {1, 2, 3, 4};
Compiler automatically calculates the size.

3. Partial Initialization
int a[5] = {10, 20};
Remaining elements are initialized to 0.

4. Initialization Using Loop
int a[5];
for(int i = 0; i < 5; i++)
{
    a[i] = i * 2;
}

5. Character Array Initialization
char name[6] = {'I', 'N', 'D', 'I', 'A', '\0'};
Or using string literal:
char name[] = "INDIA";

6. Two-Dimensional Array Initialization
int mat[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

7. Initializing All Elements with Zero
int a[10] = {0};

Processing with Array in C++
Processing with arrays in C++ means performing operations on array elements such as input, output, searching, sorting, updating, and calculations using loops.

1. Input (Reading) Elements into an Array
int a[5];
for(int i = 0; i < 5; i++)
{
    cin >> a[i];
}

2. Output (Displaying) Array Elements
for(int i = 0; i < 5; i++)
{
    cout << a[i] << " ";
}

3. Finding Sum of Array Elements
int sum = 0;
for(int i = 0; i < 5; i++)
{
    sum += a[i];
}

4. Finding Maximum and Minimum Element
int max = a[0], min = a[0];
for(int i = 1; i < 5; i++)
{
    if(a[i] > max) max = a[i];
    if(a[i] < min) min = a[i];
}

5. Searching an Element (Linear Search)
int key, pos = -1;
cin >> key;
for(int i = 0; i < 5; i++)
{
    if(a[i] == key)
    {
        pos = i;
        break;
    }
}

6. Sorting Array (Ascending Order – Bubble Sort)
for(int i = 0; i < 4; i++)
{
    for(int j = 0; j < 4 - i; j++)
    {
        if(a[j] > a[j + 1])
        {
            int temp = a[j];
            a[j] = a[j + 1];
            a[j + 1] = temp;
        }
    }
}

7. Updating Array Elements
a[2] = 100;   // Change value at index 2

8. Counting Even and Odd Numbers
int even = 0, odd = 0;

for(int i = 0; i < 5; i++)
{
    if(a[i] % 2 == 0)
        even++;
    else
        odd++;
}

Arrays and Functions in C++
In C++, arrays can be passed to functions so that operations can be performed on multiple values efficiently.

1. Passing Array to a Function
When an array is passed to a function, its base address is passed, not a copy of the array.

Syntax
return_type function_name(data_type array_name[], int size);

2. Example: Display Array Elements
#include <iostream>
using namespace std;
void display(int a[], int n)
{
    for(int i = 0; i < n; i++)
        cout << a[i] << " ";
}
int main()
{
    int arr[5] = {10, 20, 30, 40, 50};
    display(arr, 5);
    return 0;
}

3. Example: Sum of Array Elements
int sum(int a[], int n)
{
    int s = 0;
    for(int i = 0; i < n; i++)
        s += a[i];
    return s;
}

4. Passing Two-Dimensional Array to Function
For 2D arrays, the number of columns must be specified.

void show(int a[][3], int r)
{
    for(int i = 0; i < r; i++)
        for(int j = 0; j < 3; j++)
            cout << a[i][j] << " ";
}

5. Modifying Array Inside Function
Changes made inside the function affect the original array.

void update(int a[], int n)
{
    for(int i = 0; i < n; i++)
        a[i] *= 2;
}

6. Array as Function Return Type
A function cannot directly return an array, but it can return:
  • Pointer to array
  • vector
  • Dynamically allocated array
Example using vector:
#include <vector>
vector<int> getArray()
{
    return {1, 2, 3, 4};
}

Important Points
  • Array name acts as a pointer when passed to function.
  • Size of array should be passed separately.
  • No copy of array is created.
  • Efficient for handling large data.
Multidimensional Arrays in C++
A multidimensional array in C++ is an array that contains more than one dimension. The most commonly used multidimensional array is the two-dimensional array, which is used to store data in row–column (table or matrix) form.

1. Declaration of Multidimensional Array
Two-Dimensional Array
data_type array_name[rows][columns];

Example:
int a[3][4];

2. Initialization of Two-Dimensional Array
(a) Row-wise Initialization
int a[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

(b) Single-line Initialization
int a[2][3] = {1, 2, 3, 4, 5, 6};

3. Accessing Elements
a[row_index][column_index];

Example:
cout << a[1][2];   // Output: 6

4. Input and Output of 2D Array
Input
for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 3; j++)
        cin >> a[i][j];
}

Output
for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 3; j++)
        cout << a[i][j] << " ";
    cout << endl;
}

5. Passing Multidimensional Array to Function
void display(int a[][3], int rows)
{
    for(int i = 0; i < rows; i++)
        for(int j = 0; j < 3; j++)
            cout << a[i][j] << " ";
}
Note: Number of columns must be specified.

6. Example Program: Matrix Addition
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2];
for(int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
        c[i][j] = a[i][j] + b[i][j];
}

7. Three-Dimensional Array (Brief)
int a[2][3][4];
  • Used in advanced applications like graphics and simulations.
Important Points
  • Stores data in tabular form.
  • Index starts from 0.
  • Uses nested loops.
  • Memory is allocated contiguously row-wise.
Character Array in C++
A character array in C++ is an array of characters used to store strings. It ends with a null character '\0', which marks the end of the string.

1. Declaration of Character Array
char name[20];

2. Initialization of Character Array
(a) Character-by-Character Initialization
char city[6] = {'D', 'E', 'L', 'H', 'I', '\0'};

(b) String Literal Initialization
char city[] = "DELHI";

Note:- Compiler automatically adds '\0'.

3. Input of Character Array
Using cin
char name[20];
cin >> name;

Note:- Reads input only up to space.

Using cin.getline()
cin.getline(name, 20);

Note:- Reads full line including spaces.

4. Output of Character Array
cout << name;

5. String Handling Functions (<cstring>)
Function     Purpose
strlen(str)     Finds length
strcpy(a, b)     Copies string
strcat(a, b)     Concatenates
strcmp(a, b)     Compares

Example:
#include <cstring>
cout << strlen(name);

6. Example Program
#include <iostream>
using namespace std;
int main()
{
    char name[20];
    cout << "Enter name: ";
    cin.getline(name, 20);
    cout << "Name: " << name;
    return 0;
}

7. Difference: Character Array vs String Class
Character Array string Class
Fixed size                 Dynamic size
Needs '\0'                 Automatic
Uses <cstring>         Uses <string>

Important Points
  • Last character must be '\0'.
  • Size should be one more than number of characters.
  • Suitable for low-level string handling.
Example 1.
#include <iostream>
using namespace std;
int main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int i;
cout <<“Contents of the array ” << endl;
for ( i = 0; i <= 9; ++i)
cout << a[i];
return 0;
}
Output of the above program
Contents of the array
0123456789

Example 2.
#include <iostream>
using namespace std;
int main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int i;
cout <<“Contents of the array ” << endl;
for ( i = 0; i <= 9; ++i)
cout << a[i] << ‘\n’;
return 0;
}
Output of the above program
Contents of the array
0
1
2
3
4
5
6
7
8
9

Example 3.
#include <iostream>
using namespace std;
int main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int i;
cout <<“Contents of the array ” << endl;
for ( i = 0; i <= 9; ++i)
cout << a[i] << ‘\t’;
return 0;
}
Output of the above program
Contents of the array
0 1 2 3 4 5 6 7 8 9

Example 4.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i;
cout <<“Contents of the array ” << endl;
for (i = 0; i <= 9; ++i)
cout << setw(5) << a[i];
return 0;
}
Output of the above program
Contents of the array
0 1 2 3 4 5 6 7 8 9

Example 5.
#include <iostream>
using namespace std;
int main()
{
int a[100];
int i,n;
cout <<“ How many numbers are in the array ? \n”;
cin >> n;
cout <<“Enter the elements \n”;
for (i = 0; i <= n-1; ++i)
cin >> a[i];
cout <<“ Contents of the array \n”;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
return 0;
}
Output of the above program
How many numbers are in the array?
5
Enter the elements
10 11 12 13 14
Contents of the array
10 11 12 13 14

Example 6.
#include <iostream>
using namespace std;
int main()
{
int a[100];
cout <<“ How many numbers are in the array ? \n”;
cin >> n;
cout <<“Enter the elements \n”;
for (i = 0; i <= n-1; ++i)
cin >> a[i];
cout <<“Contents of the array \n”;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
larg = a[0];
for ( i = 0; i <= n-1; ++i) {
if ( larg < a[i])
larg = a[i];
}
cout <<“ \n Largest value in the array = ” << larg;
return 0;
}
Output of the above program
How many numbers are in the array?
6
Enter the elements
11 22 -33 44 -65 3
Contents of the array
11 22 -33 44 -65 3
Largest value in the array = 44

Example 7.
#include <iostream>
using namespace std;
int main()
{
int a[100];
int i,j,n,temp;
cout <<“How many numbers are in the array ? \n”;
cin >> n;
cout <<“Enter the elements \n”;
for (i = 0; i <= n-1; ++i)
cin >> a[i];
cout <<“Contents of the array (unsorted form) \n”;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
//sorting block
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
cout <<“Contents of the array (sorted form)\n”;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
return 0;
}
Output of the above program
How many numbers are in the array?
7
Enter the elements
44 -24 33 2 80 76 -7
Contents of the array (unsorted form)
44 -24 33 2 80 76 -7
Contents of the array (sorted form)
-24 -7 2 33 44 76 80

Example 8.
#include <iostream>
using namespace std;
const int MAX = 100;
int main()
{
void display (int a[],int n); // function declaration
int sumarray(int a[], int n);
int a[MAX];
int i,n,sum;
cout <<“How many numbers are in the array ? \n”;
cin >> n;
cout <<“Enter the elements \n”;
for (i = 0; i <= n-1; ++i)
cin >> a[i];
cout <<“contents of the array \n”;
display (a,n);
sum = sumarray(a,n);
cout <<“\n sum of the elements of the array = ” << sum;
cout <<“\n”;
return 0;
}
void display (int a[],int n)
{
int i;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
}
int sumarray(int x[] ,int max)
{
int i,temp = 0;
for (i = 0; i <= max-1; ++i)
temp = temp +x[i];
return(temp);
}
Output of the above program
How many numbers are in the array?
5
Enter the elements
1 2 3 4 5
Contents of the array
1 2 3 4 5
sum of the elements of the array = 15

Example 9.
#include <iostream>
using namespace std;
const int MAX = 100;
int a[MAX];
int main()
{
void getdata (int n); // function declaration
void display(int a[], int n);
void sort(int a[],int n);
int n;
cout <<“How many numbers are in the array ?\n”;
cin >> n;
getdata(n);
cout <<“Unsorted array” << endl;
display(a,n);
sort(a,n);
cout <<“\n Sorted array ” << endl;
display(a,n);
return 0;
} // end of the main program
void getdata(int n)
{
int i;
cout <<“Enter the elements \n”;
for (i = 0; i <= n-1; ++i)
cin >> a[i];
}
void display (int a[],int n)
{
int i;
for (i = 0; i <= n-1; ++i)
cout << a[i] << ‘\t’;
}
int sort (int a[], int n)
{
int temp,i,j;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-2; ++j)
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
Output of the above program
How many numbers are in the array?
6
Enter the elements
11 22 -34 5 -6 47
Unsorted array
11 22 -34 5 -6 47
Sorted array
-34 -6 5 11 22 47

Example 10.
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 20;
int a[MAX];
int main()
{
void nd(long int number);
long int n;
start:
cout <<“Enter a number” << endl;
cin >> n;
if ( n < 0){
cout <<“ enter only positive numbers” << endl;
goto start;
}
nd(n);
return 0;
}
void nd(long int n)
{
void display (int b[], int max);
int b[MAX];
int i,j,max;
i = 0;
while (n > 0)
{
a[i] = n % 10;
n = n / 10;
++i;
}
cout <<“ Number of digits = ” << i;
cout << “\n”;
max = i;
--i;
for ( j = 0; j <= max-1; ++j){
b[j] = a[i];
--i;
}
display(b,max);
cout <<“\n”;
}
void display (int b[], int max)
{
int j;
cout <<“number and its words” << endl;
for ( j = 0; j <= max-1; ++j)
cout << b[j];
cout << ‘\t’;
j = 0;
while (j != max) {
switch (b[j]) {
case 1:
cout <<“ one ” << setw(6);
break;
case 2:
cout <<“ two ” << setw(6);
break;
case 3:
cout <<“ three ” << setw(6);
break;
case 4:
cout <<“ four ” << setw(6);
break;
case 5:
cout <<“ ve ” << setw(6);
break;
case 6:
cout <<“ six ” << setw(6);
break;
case 7:
cout <<“ seven ” << setw(6);
break;
case 8:
cout <<“ eight ” << setw(6);
break;
case 9:
cout <<“ nine ” << setw(6);
break;
case 0:
cout <<“ zero ” << setw(6);
break;
} // end of switch-case statement
j = j+1;
} // end of while statement
}
Output of the above program
Enter a number
45678
Number of digits = 5
number and its words
45678 four five six seven eight

Example 11.
#include <iostream>
using namespace std;
const int MAX = 20;
int a[MAX];
bool ag;
int main()
{
void nd(long int number, int digit);
long int n;
int digit;
st_one:
cout <<“Enter a number” << endl;
cin >> n;
if (n < 0){
cout <<“ enter only positive numbers” << endl;
goto st_one;
}
st_two:
cout <<“Enter a digit to be checked ?” << endl;
cin >> digit;
if ( (digit < 0) || (digit > 9){
cout <<“ enter only positive and single digit number\n”;
goto st_two;
}
nd(n, digit);
return 0;
}
void nd(long int n, int digit)
{
void display (int b[], int max, int digit);
int b[MAX];
int i,j,max;
i = 0;
while (n > 0)
{
a[i] = n % 10;
n = n / 10;
++i;
}
max = i;
--i;
for ( j = 0; j <= max-1; ++j){
b[j] = a[i];
--i;
}
display(b,max,digit);
}
void display (int b[], int max, int digit)
{
int j,counter, ag;
counter = 0;
ag = false;
for ( j = 0; j <= max-1; ++j){
if ( b[j] == digit) {
counter = counter + 1;
ag = true;
}
} // end of for loop
if ( ag == true) {
cout <<“ given digit = ” << digit <<“ is present ”;
cout <<“in the number ”;
for ( j = 0; j <= max-1; ++j)
cout << b[j];
cout <<“\n and repeats = ” << counter <<“ times \n”;
}
else
{
cout <<“entered digit = ” << digit <<“ is not present ”;
cout <<“in the number “;
for ( j = 0; j <= max-1; ++j)
cout << b[j];
cout << “\n”;
}
}
Output of the above program
Enter a number
23452
Enter a digit to be checked?
2
given digit = 2 is present in the number 23452
and repeats = 2 times

Example 12.
#include <iostream>
using namespace std;
const int MAX = 20;
int a[MAX];
bool ag;
int main()
{
void nd(long int number, int digit);
long int n;
int digit;
st_one:
cout <<“Enter a number \n”;
cin >> n;
if (n < 0){
cout <<“ enter only positive numbers\n”;
goto st_one;
}
st_two:
cout <<“Enter a digit to be checked ?\n”;
cin >> digit;
if ( (digit < 0) || (digit > 9)){
cout <<“ enter only positive and single digit number\n”;
goto st_two;
}
nd(n, digit);
return 0;
}
void nd(long int n, int digit)
{
void display (int b[], int max, int digit);
int b[MAX];
int i,j,max;
i = 0;
while (n > 0)
{
a[i] = n % 10;
n = n / 10;
++i;
}
max = i;
--i;
for ( j = 0; j <= max-1; ++j){
b[j] = a[i];
--i;
}
display(b,max,digit);
}
void display (int b[], int max, int digit)
{
int j,k,counter, ag;
counter = 0;
ag = false;
k = 0;
for ( j = 0; j <= max-1; ++j){
if ( b[j] == digit) {
counter = counter + 1;
ag = true;
a[k] = 1+j;
k++;
}
} // end of for loop
if ( ag == true) {
cout <<“ given digit = ”<< digit << ” is present \n”;
cout <<“ in the number ”;
for ( j = 0; j <= max-1; ++j)
cout << b[j];
cout <<“\n and its position is ”;
for ( j = 0; j <= k-1; ++j)
cout << a[j];
cout <<“ from left to right \n”;
}
else
{
cout <<“ entered digit = ” << digit <<“ is not present \n”;
for ( j = 0; j <= max-1; ++j)
cout << b[j];
cout << “\n”;
}
}
Output of the above program
Enter a number
34562
Enter a digit to be checked?
2
given digit = 2 is present
in the number 34562
and its position is 5 from left to right
Enter a number
12345
Enter a digit to be checked?
9
entered digit = 9 is not present
in the number 12345

Example 13.
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 10;
int main()
{
oat a[MAX][MAX];
int i,j,n;
cout <<“ order of the matrix ” << endl;
cin >> n;
cout <<“ enter the elements” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> a[i][j];
}
cout <<“ output of the matrix” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j){
cout << setw(4) << a[i][j];
}
cout <<“” << endl;
}
return 0;
}
Output of the above program
order of the matrix
3
enter the elements
1 2 3
4 5 6
7 8 9
output of the matrix
1 2 3
4 5 6
7 8 9

Example 14.
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 3;
const int M = 4;
int main()
{
int i,j;
double a[N][M] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
cout <<“Contents of the array ” << endl;
for (i = 0; i <= N-1; ++i) {
for (j = 0; j <= M-1; ++j) {
cout << setprecision(2);
cout << setw(4) << a[i][j];
}
cout <<“” << endl;
}
return 0;
}
Output of the above program
Contents of the array
1 2 3 4
5 6 7 8
9 10 11 12

Example 15.
#include <iomanip>
using namespace std;
const int N = 3;
const int M = 4;
int main()
{
int i,j;
oat a[N][M] = {
{1,2,3},
{5,6,7},
{9,10,11}
};
cout <<“Contents of the array ” << endl;
for (i = 0; i <= N-1; ++i) {
for (j = 0; j <= M-1; ++j)
cout << setw(4) << a[i][j];
cout <<“” << endl;
}
return 0;
}
Output of the above program
Contents of the array
1 2 3 0
5 6 7 0
9 10 11 0

Example 16.
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 100;
int main()
{
// function declaration
void output ( oat a[MAX][MAX],int n);
void add ( oat a[MAX][MAX], oat b[MAX][MAX],int n);
oat a[MAX][MAX],b[MAX][MAX];
int i,j,n;
cout <<“Order of matrix ” << endl;
cin >> n;
cout <<“Enter the elements of A Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> a[i][j];
}
cout <<“Enter the elements of B Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> b[i][j];
}
cout <<“Output A[i][j] ” << endl;
output (a,n);
cout <<“” << endl;
cout <<“Output B[i][j] ” << endl;
output (b,n);
add (a,b,n);
return 0;
}
void add ( oat a[MAX][MAX], oat b[MAX][MAX] , int n)
{
void output ( oat c[MAX][MAX],int n); // function declaration
oat c[MAX][MAX];
int i,j,k;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
c[i][j] = a[i][j]+b[i][j];
}
cout <<“” << endl;
cout <<“Output of C[i][j] matrix” << endl;
output(c,n);
}
void output ( oat x[MAX][MAX],int n)
{
int i,j;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cout << setw(4) << x[i][j];
cout <<“” << endl;
}
}
Output of the above program
Order of matrix
4
Enter the elements of A Matrix
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Enter the elements of B Matrix
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
Output A[i][j]
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Output B[i][j]
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
Output of C[i][j] matrix
3 3 3 3
3 3 3 3
3 3 3 3
3 3 3 3

Example 17.
// matrix subtraction
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 100;
int main()
{
// function declaration
void output ( oat a[MAX][MAX],int n);
void sub ( oat a[MAX][MAX], oat b[MAX][MAX],int n);
oat a[MAX][MAX],b[MAX][MAX];
int i,j,n;
cout <<“Order of matrix ” << endl;
cin >> n;
cout <<“Enter the elements of A Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> a[i][j];
}
cout <<“Enter the elements of B Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> b[i][j];
}
cout <<“Output A[i][j] ” << endl;
output (a,n);
cout <<“” << endl;
cout <<“Output B[i][j] ” << endl;
output (b,n);
sub (a,b,n);
return 0;
}
void sub ( oat a[MAX][MAX], oat b[MAX][MAX] , int n)
{
void output ( oat c[MAX][MAX],int n); // function declaration
oat c[MAX][MAX];
int i,j,k;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j) {
c[i][j] = a[i][j]-b[i][j];
}
}
cout <<“” << endl;
cout <<“Output of C[i][j] matrix” << endl;
output(c,n);
}
void output ( oat x[MAX][MAX],int n)
{
int i,j;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cout << setw(4) << x[i][j];
cout <<“” << endl;
}
}
Output of the above program
Order of matrix
3
Enter the elements of A Matrix
1 1 1
1 1 1
1 1 1
Enter the elements of B Matrix
2 2 2
2 2 2
2 2 2
Output A [i][j]
1 1 1
1 1 1
1 1 1
Output B [i][j]
2 2 2
2 2 2
2 2 2
Output of C [i][j] matrix
-1 -1 -1
-1 -1 -1
-1 -1 -1

Example 18.
// matrix multiplication
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 100;
int main()
{
// function declaration
void output ( oat a[MAX][MAX],int n);
void mul ( oat a[MAX][MAX], oat b[MAX][MAX],int n);
oat a[MAX][MAX],b[MAX][MAX];
int i,j,n;
cout <<“Order of matrix ” << endl;
cin >> n;
cout <<“Enter the elements of A Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> a[i][j];
}
cout <<“Enter the elements of B Matrix ” << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> b[i][j];
}
cout <<“Output A[i][j] ” << endl;
output (a,n);
cout <<“” << endl;
cout <<“Output B[i][j] ” << endl;
output (b,n);
mul (a,b,n);
return 0;
}
void mul ( oat a[MAX][MAX], oat b[MAX][MAX], int n)
{
void output ( oat c[MAX][MAX],int n); // function declaration
oat c[MAX][MAX];
int i,j,k;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j) {
for (k = 0; k <= n-1; ++k)
c[i][j] = c[i][j]+a[i][k]*b[k][j];
}
}
cout <<“” << endl;
cout <<“Output of C[i][j] matrix” << endl;
output(c,n);
}
void output ( oat x[MAX][MAX],int n)
{
int i,j;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cout << setw(4) << x[i][j];
cout <<“” << endl;
}
}
Output of the above program
Order of matrix
3
Enter the elements of A Matrix
1 1 1
1 1 1
1 1 1
Enter the elements of B Matrix
1 1 1
1 1 1
1 1 1
Output A [i][j]
1 1 1
1 1 1
1 1 1
Output B [i][j]
1 1 1
1 1 1
1 1 1
Output of C [i][j] matrix
3 3 3
3 3 3
3 3 3

Example 19.
// three dimensional array
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 10;
int main()
{
oat a[MAX][MAX][MAX];
int i,j,k,n;
oat total;
cout <<“Order of the three dimensional matrix ?”;
cin >> n;
cout <<“Enter the elements ” << endl;
for (i = 0; i <= n-1; ++i) {
for ( j = 0; j <= n-1; ++j){
for ( k = 0; k <= n-1; ++k)
cin >> a[i][j][k];
}
}
// nding the sum of the elements
total = 0;
for (i = 0; i <= n-1; ++i) {
for ( j = 0; j <= n-1; ++j){
for ( k = 0; k <= n-1; ++k)
total = total+a[i][j][k];
}
}
// displaying the contents of the array
cout <<“ contents of the array ” << endl;
for (i = 0; i <= n-1; ++i) {
for ( j = 0; j <= n-1; ++j){
for ( k = 0; k <= n-1; ++k){
cout <<“ [” << i << j << k << “] = ” ;
cout << setw(4) << a[i][j][k];
}
cout <<“” << endl;
}
cout <<“” << endl;
}
cout <<“ ” << endl;
cout <<“ sum of the elements = ” << total;
return 0;
}// end of the main program
Output of the above program
Order of the three dimensional matrix ?
2
Enter the elements
1 1
2 2
3 3
4 4
contents of the array
[000] = 1 [001] = 1
[010] = 2 [011] = 2
[100] = 3 [101] = 3
[110] = 4 [111] = 4
sum of the elements = 20

CHARACTER ARRAY
Example 20.
#include <iostream>
using namespace std;
int main ()
{
int i;
static char name[5] = { ‘r’,‘a’,‘v’,‘i’,‘c’};
cout <<“Contents of the array” << endl;
for (i = 0; i <= 4; ++i)
cout <<“name[” << i <<“] = ” << name[i] << ‘\n’;
return 0;
}
Output of the above program
Contents of the array
name[0] = r
name[1] = a
name[2] = v
name[3] = i
name[4] = c

Example 21.
#include <iostream>
using namespace std;
int main()
{
int i;
static char name[] = “this is a test program”;
cout <<“Contents of the array” << endl;
for (i = 0; name[i] != ‘\0’; ++i)
cout <<“name[” << i <<“] = ” << name[i] << ‘\n’;
return 0;
}
Output of the above program
Contents of the array
name[0] = t
name[1] = h
name[2] = i
name[3] = s
name[4] =
name[5] = i
name[6] = s
name[7] =
name[8] = a
name[9] =
name[10] = t
name[11] = e
name[12] = s
name[13] = t
name[14] =
name[15] = p
name[16] = r
name[17] = o
name[18] = g
name[19] = r
name[20] = a
name[21] = m

Example 22.
// reading a set of lines from keyboard
// and displaying onto the video screen
#include <iostream>
using namespace std;
const int MAX = 1000;
int main()
{
char line[MAX];
char ch;
int i;
cout <<“enter a set of lines and terminate with @\n”;
i = 0;
while (( ch = cin.get()) != ‘@’) {
line[i++] = ch;
}
line[i++] = ‘\0’;
cout <<“ output from the array\n”;
for ( i = 0; line[i] != ‘\0’; ++i){
cout.put(line[i]);
}
return 0;
}
Output of the above program
enter a set of lines and terminate with @
output from the array

Example 23.
// nding a number of characters of a given text
#include <iostream>
using namespace std;
const int MAX = 1000;
int main()
{
int number (char line[] );
char line[MAX];
char ch;
int i,n_ch;;
cout <<“enter a set of lines and terminate with @\n”;
i = 0;
while (( ch = cin.get()) != ‘@’) {
line[i++] = ch;
}
line[i++] = ‘\0’;
n_ch = number (line);
cout <<“ output from the array\n”;
for ( i = 0; line[i] != ‘\0’; ++i){
cout.put(line[i]);
}
cout <<“ Number of characters = ” << n_ch;
return 0;
}
// function to nd the number of characters
int number (char s[])
{
int i = 0;
while (s[i] != ‘\0’)
++i;
return(i);
}
Output of the above program
enter a set of lines and terminate with @
output from the array
Number of characters = 24

Example 24.
// nding a number of characters and lines of a given text
#include <iostream>
using namespace std;
const int MAX = 1000;
int main()
{
int number_ch (char line[]);
int number_line(char line[]);
char line[MAX];
char ch;
int i,n_ch,n_ln;
cout <<“enter a set of lines and terminate with @\n”;
i = 0;
while ((ch = cin.get()) != ‘@’) {
line[i++] = ch;
}
line[i++] = ‘\0’;
n_ch = number_ch (line);
n_ln = number_line(line);
cout <<“ output from the array\n”;
for ( i = 0; line[i] != ‘\0’; ++i){
cout.put(line[i]);
}
cout <<“ Number of characters = ” << n_ch;
cout <<“\n Number of lines = ” << n_ln;
cout << ‘\n’;
return 0;
}
// function to nd the number of characters
int number_ch (char s[])
{
int i = 0;
while (s[i] != ‘\0’)
i++;
return(i-1);
}
// function to nd the number of lines
int number_line (char s[])
{
int i = 0, n_ln = 0;
while (s[i] != ‘\0’)
{
if ( s[i] == ‘\n’)
n_ln++;
++i;
}
return(n_ln);
}
Output of the above program
enter a set of lines and terminate with @
Number of characters = 12
Number of lines = 3

Example 25.
// string copy
#include <iostream>
using namespace std;
const int MAX = 1000;
int main()
{
void stringcopy (char dest[], char source[] );
char line[MAX],page[MAX];
char ch;
int i;
cout <<“enter a set of lines and terminate with @\n”;
i = 0;
while (( ch = cin.get()) != ‘@’) {
line[i++] = ch;
}
line[i++] = ‘\0’;
stringcopy (page,line);
cout <<“ output from the array (line)\n”;
for ( i = 0; line[i] != ‘\0’; ++i){
cout.put(line[i]);
}
cout <<“ output from the array (page)\n”;
for ( i = 0; page[i] != ‘\0’; ++i){
cout.put(page[i]);
}
return 0;
}
// function to perform the string copy
void stringcopy (char dest[], char source [])
{
int i = 0;
while (source[i] != ‘\0’) {
dest[i] = source[i];
i++;
}
dest[i++] = ‘\0’;
}
Output of the above program
enter a set of lines and terminate with @
output from the array (line)
output from the array (page)

Example 26.
// string concatenate
#include <iostream>
using namespace std;
const int MAX = 1000;
int main()
{
void stringconcat (char dest[], char sour1[],char sour2[]);
char source1[MAX],source2[MAX],total[MAX];
char ch;
int i;
cout <<“enter a set of lines and terminate with @ \n”;
i = 0;
while ((ch = cin.get()) != ‘@’) {
source1[i++] = ch;
}
source1[i++] = ‘\0’;
cin.get(); // delete an extra line feed character
cout <<“ Another input data ” << endl;
cout <<“enter a set of lines and terminate with @ \n”;
i = 0;
while ((ch = cin.get()) != ‘@’) {
source2[i++] = ch;
}
source2[i++] = ‘\0’;
stringconcat (total,source1,source2);
cout <<“ output from the array (source1)\n”;
for ( i = 0; source1[i] != ‘\0’; ++i){
cout.put(source1[i]);
}
cout <<“ output from the array (source2)\n”;
for ( i = 0; source2[i] != ‘\0’; ++i){
cout.put(source2[i]);
}
cout <<“ output from the array (total)\n”;
for ( i = 0; total[i] != ‘\0’; ++i){
cout.put(total[i]);
}
return 0;
}
// function to perform the string concatenate
void stringconcat (char dest[], char sour1 [], char sour2[])
{
int i = 0, j;
while (sour1[i] != ‘\0’) {
dest[i] = sour1[i];
i++;
}
j = 0;
while (sour2[j] != ‘\0’) {
dest[i] = sour2[j];
i++;
j++;
}
dest[i++] = ‘\0’;
}
Output of the above program
enter a set of lines and terminate with @
enter a set of lines and terminate with @
output from the array (source1)
output from the array (source2)
output from the array (total)

Example 27.
// removing white space
const int SIZE = 1000;
#include <iostream>
using namespace std;
int main()
{
char source[SIZE];
char ch;
int i,max;
cout <<“enter a set of lines and terminate with @ \n”;
i = 0;
while ((ch = cin.get()) != ‘@’) {
source[i++] = ch;
}
source[i++] = ‘\0’;
max = i;
cout <<“ output from the array\n”;
for (i = 0; i <= max-1; ++i)
cout.put(source[i]);
cout <<“ output after removing white space \n”;
// A single space between ‘ ’ is for a space character
for (i = 0; i <= max-1; ++i) {
if ((source[i] == ‘ ’) || (source[i] == ‘\t’) ||
(source[i] == ‘\n’) || ( source[i] == ‘\\’) ||
(source [i] == ‘\r’) || (source [i] == ‘\f’))
cout <<“”;
else
cout.put (source[i]);
}
return 0;
}
Output of the above program
enter a set of lines and terminate with @

output from the array
output after removing white space
thisisatestprogrambySampathKReddy

Example 28.
#include <iostream>
using namespace std;
const int SIZE = 1000;
int main()
{
void reverse(char s[SIZE],char d[SIZE]);
char source[SIZE],dest[SIZE];
char ch;
int i,max;
cout <<“enter a set of lines and terminate with @ \n”;
i = 0;
while ((ch = cin.get()) != ‘@’) {
source[i++] = ch;
}
source[i++] = ‘\0’;
max = i;
cout <<“ output from the array\n”;
for (i = 0; i <= max-1; ++i)
cout.put(source[i]);
reverse(source,dest);
cout <<“ After string reversal \n”;
for (i = 0; i <= max-1; ++i)
cout.put(dest[i]);
return 0;
}
void reverse(char s[SIZE], char d[SIZE])
{
int stringlen(char a[SIZE]);
int i,j;
j = stringlen(s);
i = 0;
while ( j >= 0) {
d[i] = s[j];
i++;
j–—;
}
}
int stringlen(char a[SIZE])
{
int i=0;
while ( a[i] != ‘\0’)
i++;
return (i);
}
Output of the above program
enter a set of lines and terminate with @
output from the array
After string reversal margorp tset a si siht

0 comments

Post a Comment