Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

CPP Assignment

Uploaded by

harshitachawla49
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CPP Assignment

Uploaded by

harshitachawla49
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1.

//Program to search an element in 1d array

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int arr[20],x,n,pos,count=0;

cout<<"Enter array size:";

cin>>n;

cout<<"Enter array elements:\n";

for(int i=0;i<n;i++)

cout<<" ";

cin>>arr[i];

cout<<"Enter elements to be searched:";

cin>>x;

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

if(arr[i]==x)

count=1;

pos=i+1;

break;

}
}

if(count==0)

cout<<"\nElement not found";

else

cout<<"\nElement"<<x<<" found at position"<<pos;

getch();

OUTPUT:-

Enter array size:5

Enter array elements:

10

Enter elements to be searched:8

Element8 found at position4


2. //Program to find sum ofeven and odd numbers in 1d array

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int n, es,os,i;

//es using even sum

es=0;

//os using odd sum

os=0;

cout<<"\nEnter a number:";

cin>>n;

for(i=1;i<=n;i++)

if(i%2==0)

es=es+i;

else

os=os+i;

cout<<"Sum of all even number="<<es<<endl;

cout<<"\nSum of all odd number="<<os<<endl;

cout<<endl;

getch();
}

Output:-

Enter a number:20

Sum of all even number=110

Sum of all odd number=100

3. //Program to find largest element from 1d array

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int i,n;

float arr[100];

cout<<"Enter total number of element(1 to 100):";

cin>>n;

cout<<endl;

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

cout<<"Enter Number"<<i+1<<" : ";

cin>>arr[i];

for(i=1;i<n;i++)

{
if(arr[0]<arr[i]);

arr[0]=arr[i];

cout<<endl<<"Largest element="<<arr[0];

getch();

Output:-

Enter total number of element(1 to 100):5

Enter Number1 : 34

Enter Number2 : 66

Enter Number3 : 78

Enter Number4 : 45

Enter Number5 : 86

Largest element=86

4.//Program to perform binary search

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

int main()

clrscr();

int array[20],n,x,start,end,mid;

cout<<"Enter size of array";

cin>>n;

cout<<"Enter array elements no.";

for(int i=0;i<n;i++)

cin>>array[i];

cout<<"\n";

cout<<"Enter number to find \n";

cin>>x;

start=0;

end=n-1;

while(start<=end)

mid=(start+end)/2;

if(array[mid]==x)

cout<<x<<" found at location "<<mid+1<<"\n";

break;

}
else if(array[mid]<x)

start=mid+1;

else

end=mid-1;

if(start>end)

cout<<"Element is not found :";

getch();

Output:-

Enter size of array5

Enter array elements no.5

10

14
Enter number to find

10

10 found at location 3

5. //Program to perform bubble sort

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int i,j,n,temp,arr[30];

cout<<"Enter the total elements you want to sort";

cin>>n;

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

cin>>arr[i];

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

for(j=0;j<n-i-1;j++)

if(arr[j]>arr[j+1])

temp=arr[j];
arr[j]=arr[j+1];

arr[j+1]=temp;

cout<<"The array elements after bubble sorting is";

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

cout<<"\n"<<arr[i];

getch();

Output:-

Enter the total elements you want to sort10

14

23

35

45

20

18

38

12

24

27

The array elements after bubble sorting is


12

14

18

20

23

24

27

35

38

45

6. //Program to perform selection sort

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int i,j,t,n,min,arr[30];

cout<<"Enter total elements you want to sort";

cin>>n;

cout<<"Enter elements";

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

cin>>arr[i];

for(i=0;i<n;i++)
{

min=i;

for(j=i+1;j<n;j++)

if(arr[j]>arr[min])

min=j;

t=arr[i];

arr[i]=arr[min];

arr[min]=t;

cout<<"Sorted elements";

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

cout<<"\n"<<arr[i];

getch();

Output:-

Enter total elements you want to sort6

Enter elements45

55
67

28

32

Sorted elements

67

55

45

32

28

7. //Program to perform insertion sort

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int i,j,k,n,temp,arr[30];

cout<<"Enter the total number of elements";

cin>>n;

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

cin>>arr[i];

}
for(i=1;i<n;i++)

for(j=i;j>=1;j--)

if(arr[j]<arr[j-1])

temp=arr[j];

arr[j]=arr[j-1];

arr[j-1]=temp;

else

break;

cout<<"sorted array";

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

cout<<"\n"<<arr[i];

getch();

Output:-

Enter the total number of elements6


12

15

20

sorted array

12

15

20

8. //Program to perform matrix in 2d array

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int A[2][2],B[2][2],C[2][2];

int j,k;

cout<<"Enter a value for matrix A;"<<endl;

for(j=0;j<2;j++)

{
for(k=0;k<2;k++)

cin>>A[1][0]; }

cout<<"Enter a value for matrix B;"<<endl;

for(j=0;j<2;j++)

for(k=0;k<2;k++)

cin>>B[j][k];

for(j=0;j<2;j++)

for(k=0;k<2;k++)

C[j][k]=A[j][k]+B[j][k];

cout<<"Addition of matrix A and B is:"<<endl;

for(j=0;j<2;j++)

for(k=0;k<2;k++)

cout<<C[j][k]<<" ";
}

cout<<endl;

getch();

Output:-

Enter a value for matrix A;

12

13

32

10

Enter a value for matrix B;

Addition of matrix A and B is:

1574 1549

12 7979

9. //Program to perform transpose of matrix

#include<iostream.h>

#include<conio.h>

int main()
{

clrscr();

int mark[3][3];

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

for(int j=0;j<3;j++)

cout<<"Enter element in row"<<i+1<<" Column "<<j+1<<" : ";

cin>>mark[i][j];

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

for(int j=0;j<3;j++)

cout<<mark[j][i]<<" ";

cout<<endl;

getch();

Output:-

Enter element in row1 Column 1 : 1


Enter element in row1 Column 2 : 2

Enter element in row1 Column 3 : 3

Enter element in row2 Column 1 : 7

Enter element in row2 Column 2 : 6

Enter element in row2 Column 3 : 5

Enter element in row3 Column 1 : 3

Enter element in row3 Column 2 : 2

Enter element in row3 Column 3 : 1

173

262

351

10. //Program to perform matrix multiplication in 2d array

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int product[10][10],r1=3,c1=3,r2=3,c2=3,i,j,k;

int a[3][3]={ {2,4,1},{2,3,9},{3,1,8} };

int b[3][3]={ {1,2,3},{3,6,1},{2,4,7} };

if(c1!=r2)

cout<<"column of first matrix should be equal to row of second matrix";

}
else

cout<<"The first matrix is:"<<endl;

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

for(j=0;j<c1;j++)

cout<<a[i][j]<<" ";

cout<<endl;

cout<<endl;

cout<<"The second matrix is:"<<endl;

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

for(j=0;j<c2;j++)

cout<<b[i][j]<<" ";

cout<<endl;

cout<<endl;

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

{
for(j=0;j<c2;j++)

product[i][j]=0;

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

for(j=0;j<c2;j++)

for(k=0;k<c1;k++)

product[i][j]+=a[i][k]*b[k][j];

cout<<"Product of two matrix is:"<<endl;

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

for(j=0;j<c2;j++)

cout<<product[i][j]<<" ";

cout<<endl;

}
getch();

Output:-

The first matrix is:

The second matrix is:

123

361

247

Product of two matrix is:

16

32

17

29

58

72

22

44

66

You might also like