You are on page 1of 27

ARRAYS

S.NO PROGRAM TEACHER’S


SIGNATURE

1 Write a C++ program to search for an


element in an array using linear search.

2 Write a C++ program to search an


element in an array using binary search.

3 Write a C++ program to sort an array


using insertion sort

4 Write a C++ program to sort an array


using selection sort

5 Write a C++ program to merge and sort


two arrays into a third array.

6 Write a C++ program to find the sum of


elements on both the diagonals in a square
matrix.
7 Write a C++ program to multiply two
matrices.

8 Write a C++ program for transpose of a


matrix.
1) Write a C++ program to search for an element in an array using linear
search.

INPUT:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a[10];
int se=0;
int min;
cout<<"Enter 10 values"<<endl;
for(int i = 0;i<=9;i++)
{
cin>>a[i];
}
cout<<" Enter the value you want to search for "<<endl;
cin>>se;
for(int j = 0;j<=9;j++)
{
if(se==a[j])
{
min = j;
break;
}
}
if(se == 0)
{
cout<<" The value you have entered is not in this array"<<endl;
}
else
{
cout<<" Position is "<<min+1;
}
getch();
}
OUTPUT:
2) Write a C++ program to search an element in an array using binary search.

INPUT:
#include<iostream.h>
#include<conio.h>
void bsearch(int a[10],int size,int item );
void main()
{
clrscr();
int i,j,a[10],size,item;
cout<<"Enter the size of array A ";
cin>>size;
cout<<"Enter the elements of array A"<<endl;
for(i=0;i<size;i++)
cin>>a[i];
cout<<"Array A \n";
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"item to be searched";
cin>>item;
bsearch(a,size,item);
getche();
}
void bsearch(int a[10],int size,int item)
{
int ino,l=0,u=size-1,m,j,temp,i;
int pos=-1;
for(i=0;i<size-1;i++)
{
for(j=0;j<size-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
cout<<"\n Sorted Array C: \n";
for(i=0;i<size;i++)cout<<a[i]<<" ";
do
{
m=(l+u)/2;
if(a[m]==item)
{
pos=m;
break;
}
else if(a[m]>item)
u=m-1;
else
l=m+1;
}while(l<=u);
if(pos==-1)
cout<<endl<<"No such item exist";
else
cout<<endl<<"Element found in position:- "<<pos+1;
}

OUTPUT:
3) Write a C++ program to sort an array using insertion sort

INPUT:
#include<iostream.h>
#include<conio.h>
void isort(int a[10],int size );
void main()
{
clrscr();
int i,j,a[10],size;
cout<<"Enter the size of array A ";
cin>>size;
cout<<"Enter the elements of array A"<<endl;
for(i=0;i<size;i++)
cin>>a[i];
cout<<"Array A \n";
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
isort(a,size);
getche();
}
void isort(int a[10],int size)
{
int i,j,k,temp;
for(i=1;i<size;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
cout<<"After PASS "<<i+1<<endl;
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
}
}
OUTPUT:
4) Write a C++ program to sort an array using selection sort

INPUT:
#include<iostream.h>
#include<conio.h>
void ssort(int a[10],int size );
void main()
{
clrscr();
int i,j,a[10],size;
cout<<"Enter the size of array A ";
cin>>size;
cout<<"Enter the elements of array A"<<endl;
for(i=0;i<size;i++)
cin>>a[i];
cout<<"Array A \n";
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
ssort(a,size);
getche();
}
void ssort(int a[10],int size)
{
int i,j,small,temp,pos;
for(i=0;i<size;i++)
{
small=a[i];
pos=i;
for(j=i+1;j<size;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=small;
a[pos]=temp;
cout<<"After PASS "<<i+1<<endl;
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
}
}

OUTPUT:
5) Write a C++ program to merge and sort two arrays into a third array.

INPUT:
#include<iostream.h>
#include<conio.h>
void merge(int a[10],int b[10],int m,int n );
void main()
{
clrscr();
int i,j,a[10],m,n,b[10];
cout<<"Enter the size of array A ";
cin>>m;
cout<<"Enter the elements of array A"<<endl;
for(i=0;i<m;i++)
cin>>a[i];
cout<<"Array A \n";
for(i=0;i<m;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<endl<<"Enter the size of array B ";
cin>>n;
cout<<"Enter the elements of the array B";
for(i=0;i<n;i++)
cin>>b[i];
cout<<"Array B \n";
for(i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<endl;
merge(a,b,m,n);
getche();
}
void merge(int a[10],int b[10],int m,int n)
{
int size;
int i=0,j=n-1,c[30],k=0;
while(i<n && j>=0)
{
if(a[i]<b[j])
c[k++]=a[i++];
else
c[k++]=b[j--];
}
while(i<n)
c[k++]=a[i++];
while(j>=0)
c[k++]=b[j--];
cout<<"\n Sorted Array C: \n";
for(i=0;i<size;i++)
cout<<c[i]<<" ";
}

OUTPUT:
6) Write a C++ program to find the sum of elements on both the diagonals in a square
matrix.

INPUT:
#include<iostream.h>
#include<conio.h>
void diagsum(int[50][50],int m,int n);
void main()
{
clrscr();
int i,j,a[50][50],m,n;
cout<<"Enter the rows and columns for the matrix "<<endl;
cin>>m>>n;
cout<<"Enter the elements of the array"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
diagsum(a,m,n);
getche();
}
void diagsum(int a[50][50],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j || i+j==(m-1))
sum+=a[i][j];
else
a[i][j]=0;
}
}
cout<<"\n Diagonals: \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"SUM OF THE DIAGONALS:- "<<sum;
}

OUTPUT:
7) Write a C++ program to multiply two matrices.

INPUT:
#include<iostream.h>
#include<conio.h>
void multiply(int a[10][10],int b[10][10],int m,int n,int x );
void main()
{
clrscr();
int i,j,a[10][10],m,n,b[10][10],x,y;
cout<<"Enter the rows and columns for the matrix A ";
cin>>m>>n;
cout<<"Enter the elements of the array A"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix A \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl<<"Enter the rows and columns for the matrix B ";
cin>>x>>y;
cout<<"Enter the elements of the array B";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>b[i][j];
}
}
cout<<"Matrix B \n";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
multiply(a,b,m,n,x);
getche();
}
void multiply(int a[10][10],int b[10][10],int m,int n, int x)
{
int i,j,k,c[10][10];
if(n==x)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<x;k++)
{
c[i][j]+=(a[i][k]*b[k][j]);
}
}
}
}
else
{
cout<<"Product cannot be found";
goto end;
}
cout<<"\nProduct matrix C: \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
end:
}
OUTPUT:
8) Write a C++ program for transpose of a matrix.

INPUT:
#include<iostream.h>
#include<conio.h>
void transpose(int[50][50],int m,int n);
void main()
{
clrscr();
int i,j,a[50][50],m,n;
cout<<"Enter the rows and columns for the matrix ";
cin>>m>>n;
cout<<"Enter the elements of the array";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Matrix \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
transpose(a,m,n);
getche();
}
void transpose(int a[50][50],int m,int n)
{
int i,j,temp=0,t=0;
for(i=0;i<m;i++)
{
for(j=t;j<n;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
t++;
}
cout<<"\nTransposed matrix: \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}

OUTPUT:
POINTERS

S.NO PROGRAMS TEACHER’S


SIGNATURE
1. Write a program to print each string
in reverse order in array of char
pointers.

2. Write a program having function to


swap values of two variables using
pass by reference method

3. Write a program having function to


swap values of two variables by
passing pointers

4. Write a program to find a substring in


a string using pointer

5. Write a program to exchange


positions of strings at even position
with odd position stored in array
using array of pointers.
1) Write a program to print each string in reverse order in array of char pointers.

INPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[45][20];
char *ptr[20];
int no,i,j;
int len[20];
cout<<"Enter no of strings to entered"<<endl;
cin>>no;
for(i=0;i<no;i++)
{
cout<<"Enter the string "<<i+1<<" to be reversed "<<endl;
gets(str[i]);
}
for(i=0;i<no;i++)
len[i]=strlen(str[i]);
for(i=0;i<no;i++)
{
ptr[i]=str[i]+len[i];
}
for(j=0;j<no;j++)
{
cout<<"Reversed string "<<j+1<<endl;
for(i=0;i<len[j]+1;i++)
{
cout<<*ptr[j];
ptr[j]--;
}
cout<<endl;
}
getche();
}
OUTPUT
2) Write a program having function to swap values of two variables using pass by
reference method

INPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void swap(int &x,int &y)
{
int temp=0;
temp=x;
x=y;
y=temp;
}
void main()
{
clrscr();
int x,y;
cout<<"Enter the values of x and y"<<endl;
cin>>x>>y;
int *ptr1,*ptr2;
ptr1=&x;
ptr2=&y;
swap(x,y);
cout<<"Swapped values of x and y are:"<<endl;
cout<<*ptr1<<" "<<*ptr2;
getche();
}

OUTPUT:
3) Write a program having function to swap values of two variables by passing
pointers

INPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void swap(int *ptr1,int *ptr2)
{
int temp=0;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
}
void main()
{
clrscr();
int x,y;
cout<<"Enter the values of x and y"<<endl;
cin>>x>>y;
int *ptr1,*ptr2;
ptr1=&x;
ptr2=&y;
swap(ptr1,ptr2);
cout<<"Swapped values of x and y are:"<<endl;
cout<<*ptr1<<" "<<*ptr2;
getche();
}

OUTPUT:
4) Write a program to find a substring in a string using pointer

INPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr;
char str[45];
int no,i,count=0;
int len;
cout<<"Enter the string to find substring "<<endl;
gets(str);
len=strlen(str);
ptr=str;
cout<<endl<<"Substrings of a string are: "<<endl;
for(i=0;i<len+1;i++)
{
if(*ptr!=' ')
cout<<*ptr;
else
{
cout<<endl;
count++;
}
ptr++;
}
cout<<"No of substrings = "<<count+1;
getche();
}

OUTPUT:
5) Write a program to exchange positions of strings at even position with odd
position stored in array using array of pointers.

INPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char *ptr[20];
char str[45][20];
int no,i,j;
int len[20];
cout<<"Enter no of strings to entered"<<endl;
cin>>no;
for(i=0;i<no;i++)
{
cout<<"Enter the string "<<i+1<<endl;
gets(str[i]);
}
cout<<endl;
for(i=0;i<no;i++)
cout<<str[i]<<" ";
cout<<endl;
for(i=0;i<no;i++)
{
if(i%2==0)
{
if(i!=(no-1))
len[i+1]=strlen(str[i]);
else
len[i]=strlen(str[i]);
}
else
len[i-1]=strlen(str[i]);
}
for(i=0;i<no;i++)
{
if(i%2==0)
{
if(i!=(no-1))
ptr[i+1]=str[i];
else
ptr[i]=str[i];
}
else
ptr[i-1]=str[i];
}
cout<<endl<<"Positions of strings exchanged"<<endl;
for(j=0;j<no;j++)
{
for(i=0;i<len[j]+1;i++)
{
cout<<*ptr[j];
ptr[j]++;
}
cout<<" ";
}
getche();
}

OUTPUT:

You might also like