Total Array program in C++


Content


1. Program to use Simple Array

#include <iostream.h>
#include <conio.h>

void  main()
{
clrscr();
int num[5];
int  i, sum = 0;

for( i=0 ; i<5 ; i++ )
{
cout << " Enter value " << (i + 1) << " : ";
cin >> num[ i ];
}

cout << "\n The elements are: ";
for( i=0 ; i<5 ; i++ )
{
cout << "  " << num[ i ];
}

getch();
}

Output:

  Enter value 1: 5
  Enter value 2: 10
  Enter value 3: 15
  Enter value 4: 20
  Enter value 5: 25

  The elements are: 5 10 15 20 25





2. Program to explain Initialization of an Array

#include <iostream.h>
#include <conio.h>

void  main()
{
clrscr();
int  num[10] = { 10, 20, 30, 40, 50};

for( int i=0 ; i<5 ; i++ )
{
cout << "  " << i+1 << " : " << num[ i ] << endl;
}

getch();
}

Output:

1 : 10
2 : 20
3 : 30
4 : 40
5 : 50





3. Program to find Sum of Array elements.

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  sum;
int  arr[5];

cout << " Enter any five Integer Numbers : ";
for( int i=0 ; i<5 ; i++ )
{
cin >> arr[ i ];
}

sum = 0;
for( i=0 ; i<5 ; i++ )
{
sum = sum + arr[ i ];
}
cout << " The sum of the Array elements is : " << sum;

getch();
}

Output:

Enter any five Integer Numbers : 1  2  3  4  5
The sum of the Array elements is : 15





4. Program to find Average of an Array

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
float a[5];
int i;

for ( i=0 ; i<5 ; i++ )
{
cout << " Enter value : ";
cin >> a[ i ];
}

float  s = 0;
for( i=0 ; i<5 ; i++ )
{
s = s + a[ i ];
}

float  avg = s / 5;
cout << " Average : " << avg;

getch();
}

Output:

Enter value : 1
Enter value : 2
Enter value : 3
Enter value : 4
Enter value : 5

Average : 3






5. Program to find Maximum and Minimum of Array elements.

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  i, j, arr[5], min, max;

cout << "\n Enter Five Integer Numbers : ";
for( i=0 ; i<5 ; i++ )      
{
cin >> arr[ i ];
}

min = arr[0];
max = arr[0];
for( i=0 ; i<5 ; i++ )
{
if( min > arr[ i ] ) 
min = arr[ i ]; 
if( max < arr[ i ] )
max = arr[ i ];
}

cout << "\n Minimum value is = " << min;
cout << "\n Maximum value is = " << max;

getch();
}

Output:

Enter Five Integer Numbers :   12  45  64  34  20
Minimum value is = 12
Maximum value is = 64





6. Program to explain Linear Search

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  num[10], i, pos = -1, value;

cout << "Enter Ten Numbers : ";
for( i=0 ; i<10 ; i++ )
{
cin >> num[ i ];
}

cout << " Enter the number to be searched : ";
cin >> value;

for( i=0 ; i<10 ; i++ )
{
if( value == num[ i ] )
{
pos = i + 1;
break;
}
}

if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is : " << pos;

getch();
}

Output:

Enter Ten Numbers :
10
20
30
40
50
60
70
80
90
100
Enter the number to be searched : 60
The position of 60 is : 6




7. Program to explain Binary Search

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int num[10], i, beg, end, mid, pos = -1, value;

cout << "Enter Ten Numbers in Ascending Order: ";
for( i=0 ; i<10 ; i++ )
{
cin >> num[i];
}

cout << " Enter the number to be searched : ";
cin >> value;

beg = 0;
end = 10 - 1;
while(beg <= end)
{
mid = (beg + end) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if ( value >= num[mid] )
beg = mid + 1;
else
end = mid - 1;
}

if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is : " << pos;

getch();
}

Output:

Enter Ten Numbers in Ascending Order:
1
2
3
4
5
6
7
8
9
10
Enter the number to be searched : 6
The position of 6 is : 6






8. Program to explain Bubble Sort

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  i, j;
int  n[ ] = { 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :";
for( i=0 ; i<5 ; i++ )
{
cout << "   " << n[ i ];
}

for( i=0 ; i<5-1 ; i++ )
{
for( j=0 ; j<5-i-1 ; j++ )
{
if( n[ j ] > n[ j+1] )
{
int  t = n[ j ];
n[ j ] = n[ j+1];
n[ j+1] = t;
}
}
}

cout << "\n\n After Sorting :";
for( i=0 ; i<5 ; i++ )
{
cout << "   " << n[ i ];
}

getch();
}

Output:

Before Sorting : 30  40  50  10  20

After Sorting : 10  20  30  40  50






9. Program to explain Selection Sort

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  i, j, small, pos, tmp ;
int  n[ ] = { 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :";
for( i=0 ; i<5 ; i++ )
{
cout << "   " << n[ i ];
}

for( i=0 ; i<5 ; i++ )
{
small = n[ i ];
pos = i;
for( j=i+1 ; j<5 ; j++ )
{
if( n[ j ] < small )
{
small = n[ j ];
pos = j;
}
}
tmp = n[ i ];
n[ i ] = n[pos];
n[pos] = tmp;
}

cout << "\n\n After Sorting :";
for( i=0 ; i<5 ; i++ )
{
cout << "   " << n[ i ];
}

getch();
}

Output:

Before Sorting : 30  40  50  10  20

After Sorting : 10  20  30  40  50





10. Program to explain Insertion Sort

#include <iostream.h>
#include <conio.h>
#include <limits.h>

void main()
{
clrscr();
int  i, j, tmp;
int  n[ ] = { 0, 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :";
for( i=1 ; i<=5 ; i++ )
{
cout << "   " << n[ i ];
}

n[0] = INT_MIN;
for( i=1 ; i<=5 ; i++ )
{
tmp = n[ i ];
j = i - 1;
while( tmp < n[ j ] )
{
n[ j+1] = n[ j ];
j--;
}
n[ j+1] = tmp;
}

cout << "\n\n After Sorting :";
for( i=1 ; i<=5 ; i++ )
{
cout << "   " << n[ i ];
}

getch();
}

Output:

Before Sorting : 30  40  50  10  20

After Sorting : 10  20  30  40  50






11. Program to show use of quick sort

#include <iostream.h>
#include <conio.h>

void main()
{
void srt(int[],int,int);
int a[10],count=0,n;
clrscr();
cout<<"Enter 10 values in unsorted order : \n";
for (n=0;n<10;n++)
     {
      cout<<"value no.: "<<(n+1)<<"\t";
      cin>>a[n];
      count++;
     }
n=0;
clrscr();
srt(a,n,count-1);
clrscr();
cout<<"\t\tThe Sorted order is : \n";
for (n=0;n<10;n++)
     {
      cout<<"\t\tposition : "<<(n+1)<<"\t"<<a[n]<<"\n";
     }
getch();
}
void srt(int k[20],int lb,int ub)
{
int i,j,key,flag=0,temp;
clrscr();
if (lb<ub)
    {
     i=lb;
     j=ub+1;
     key=k[i];
     while(flag!=1)
      {
       i++;
       while(k[i]<key)
        {
         i++;
        }
       j--;
       while(k[j]>key)
        {
         j--;
        }
       if (i<j)
          {
           temp=k[i];
           k[i]=k[j];
           k[j]=temp;
          }
       else
          {
           flag=1;
           temp=k[lb];
           k[lb]=k[j];
           k[j]=temp;
          }
      }
     srt(k,lb,j-1);
     srt(k,j+1,ub);
    }
}

Output:

Enter 10 values in unsorted order :
value no.: 1    87
value no.: 2    23
value no.: 3    45
value no.: 4    12
value no.: 5    90
value no.: 6    56
value no.: 7    34
value no.: 8    23
value no.: 9    25
value no.: 10   65

The Sorted order is :
position : 1    12
position : 2    23
position : 3    23
position : 4    25
position : 5    34
position : 6    45
position : 7    56
position : 8    65
position : 9    87
position : 10   90







12. Program to Implement Merge Sort

#include <iostream>
using namespace std;
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
    int mid;
    if (low < high)
    {
        mid=(low+high)/2;
        mergesort(a,low,mid);
        mergesort(a,mid+1,high);
        merge(a,low,high,mid);
    }
    return;
}
void merge(int *a, int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    k = low;
    j = mid + 1;
    while (i <= mid && j <= high)
    {
        if (a[i] < a[j])
        {
            c[k] = a[i];
            k++;
            i++;
        }
        else
        {
            c[k] = a[j];
            k++;
            j++;
        }
    }
    while (i <= mid)
    {
        c[k] = a[i];
        k++;
        i++;
    }
    while (j <= high)
    {
        c[k] = a[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++)
    {
        a[i] = c[i];
    }
}
int main()
{
    int a[20], i, b[20];
    cout<<"enter  the elements\n";
    for (i = 0; i < 5; i++)
    {
        cin>>a[i];
    }
    mergesort(a, 0, 4);
    cout<<"sorted array\n";
    for (i = 0; i < 5; i++)
    {
        cout<<a[i];
    }
    cout<<"enter  the elements\n";
    for (i = 0; i < 5; i++)
    {
        cin>>b[i];
    }
    mergesort(b, 0, 4);
    cout<<"sorted array\n";
    for (i = 0; i < 5; i++)
    {
        cout<<b[i];
    }
    getch();
}

Output:

enter  the elements
78
45
80
32
67
sorted array
32
45
67
78
80







13. Program to Reverse an Array elements without using Second Array

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int  arr[5];
int  i, j, t;

for( i=0 ; i<5 ; i++ )
{
cout << " Enter value " << (i + 1) << " : ";
cin >> arr[ i ];
}

cout << "\n The elements before reversing are : ";
for( i=0 ; i<5 ; i++ )
{
cout << "  " << arr[ i ];
}

for( i=0, j=5-1 ; i<5/2 ; i++, j-- )
{
t = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = t;
}

cout << "\n The elements after reversing are : ";
for( i=0 ; i<5 ; i++ )
{
cout << "  " << arr[ i ];
}

getch();
}

Output:

Enter value 1 : 1
Enter value 2 : 2
Enter value 3 : 3
Enter value 4 : 4
Enter value 5 : 5

The elements before reversing are : 1  2  3  4  5
The elements after reversing are : 5  4  3  2  1




14. Program to create Fibonacci Series in an Array

#include <iostream.h>
#include <conio.h>

void main()
{
int n, a[50];
clrscr();

  cout << " Enter the Number of Terms : ";
  cin >> n;
 
a[0] = 0;
  a[1] = 1;
 
for( int  i=2 ; i<n ; i++ )
   {
a[ i ] = a[ i-1] + a[ i-2];
   }
  
cout << endl;
for( i=0 ; i<n ; i++ )
   {
cout << "   " << a[ i ];
}
   
getch();
}

Output:

Enter the Number of Terms : 10

0  1  1  2  3  5  8  13  21  34






15. Program to Input and display a Matrix (2-D Array)

#include <iostream.h>
#include <conio.h>

void  main()
{
clrscr();
int  a[2][3];
int  i, j;

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value : ";
cin >> a[ i ][ j ];
}
}

cout << "\n The Matrix is : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << a[ i ][ j ] << " ";
}
cout << "\n";
}

getch();
}

Output:

Enter value : 10
Enter value : 20
Enter value : 30
Enter value : 40
Enter value : 50
Enter value : 60

The Matrix is :
10     20     30
40     50     60






16. Program for Addition of Two Matrices

#include <iostream.h>
#include <conio.h>

void  main()
{
clrscr();
int  a[2][3], b[2][3], c[2][3];
int  i, j;

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value of A : ";
cin >> a[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value of B : ";
cin >> b[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
c[ i ][ j ] = b[ i ][ j ] + a[ i ][ j ] ;
}
}

cout << "\n Matrix A is : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << a[ i ][ j ] << " ";
}
cout << "\n";
}

cout << "\n Matrix B is : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << b[ i ][ j ] << " ";
}
cout << "\n";
}

cout << "\n Matrix C is : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << c[ i ][ j ] << " ";
}
cout << "\n";
}

getch();
}

Output:

Enter value of A : 10
Enter value of A : 20
Enter value of A : 30
Enter value of A : 40
Enter value of A : 50
Enter value of A : 60
Enter value of B : 10
Enter value of B : 20
Enter value of B : 30
Enter value of B : 40
Enter value of B : 50
Enter value of B : 60

  Matrix A is :
10      20      30
40      50      60

  Matrix B is :
10      20      30
40      50      60

  Matrix C is :
20      40      60
80      100      120





17. Program to find Transpose of given Matrix

#include <iostream.h>
#include <conio.h>

void  main()
{
clrscr();
int  a[2][3], b[3][2];
int  i, j;

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value : ";
cin >> a[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
b[ j ][ i ] = a[ i ][ j ] ;
}
}

cout << "\n Matrix A is : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << a[ i ][ j ] << " ";
}
cout << "\n";
}

cout << "\n Matrix B is : \n";
for( i=0 ; i<3 ; i++ )
{
for( j=0 ; j<2 ; j++ )
{
cout << b[ i ][ j ] << " ";
}
cout << "\n";
}

getch();
}

Output:

Enter value  : 10
Enter value  : 20
Enter value  : 30
Enter value  : 40
Enter value  : 50
Enter value  : 60

  Matrix A is :
10      20      30
40      50      60

  Matrix B is :
10      40
20      50
30      60





18. Program to Multiply Two Matrices

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a[2][3], b[3][2], c[2][2];
int i, j, k;

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter Value of A : ";
cin >> a[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter Value of B : ";
cin >> b[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<2 ; j++ )
{
c[ i ][ j ] = 0;
for( k=0 ; k<3 ; k++ )
{
c[ i ][ j ] = c[ i ][ j ] + (a[ i ][ k ] * b[ k ][ j ]);
}
}
}

cout << "\n Matrix A : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << "  " << a[ i ][ j ];
}
cout << endl;
}

cout << "\n Matrix B : \n";
for( i=0 ; i<3 ; i++ )
{
for( j=0 ; j<2 ; j++ )
{
cout << "  " << b[ i ][ j ];
}
cout << endl;
}

cout << "\n Matrix C : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<2 ; j++ )
{
cout << "  " << c[ i ][ j ];
}
cout << endl;
}

getch();
}

Output:

Enter value of A : 1
Enter value of A : 2
Enter value of A : 3
Enter value of A : 4
Enter value of A : 5
Enter value of A : 6
Enter value of B : 1
Enter value of B : 2
Enter value of B : 3
Enter value of B : 4
Enter value of B : 5
Enter value of B : 6

  Matrix A :
   1     2     3
   4     5     6

  Matrix B :
   1      4
   2      5
   3      6

  Matrix C :
   14    32
   32    77


Reactions

Post a Comment

0 Comments