OOP Exp1
OOP Exp1
➢ Sorting of an Array:
#CODE
#include<iostream>
using namespace std;
int main()
{
int n,arr[10];
read(arr,n);
display(arr,n);
sort(arr,n);
display(arr,n);
return 0;
}
}
void swap(int &i, int &j)
{
int temp;
temp=i;
i=j;
j=temp;
}
void sort(int a[], int &n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}
#OUTPUT
Enter number of elements in array:3
Enter data:
23
12
19
Array is:23 12 19
Array is:12 19 23
➢ Sum of Matrices:
#CODE
#include<iostream>
using namespace std;
class Add
{
public:
void sum(int r,int c)
{
int m1[r][c], m2[r][c], s[r][c];
cout<<"Enter the elements of 1st matrix:\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cin>>m1[i][j];
}
cout<<"Enter the elements of 2nd matrix:\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cin>>m2[i][j];
}
cout<<"Sum of Matrices:\n";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
s[i][j]=m1[i][j]+m2[i][j];
cout<<s[i][j]<<" ";
}
cout<<"\n";
}
}
};
int main()
{
int row,col;
cout<<"Enter number of rows(1-10):";
cin>>row;
cout<<"Enter number of columns(1-10):";
cin>>col;
Add obj;
obj.sum(row,col);
return 0;
}
#OUTPUT
Enter number of rows(1-10):3
Enter number of columns(1-10):3
Enter the elements of 1st matrix:
2
5
8
3
9
4
1
6
3
Enter the elements of 2nd matrix:
7
5
9
2
2
4
7
6
8
Sum of Matrices:
9 10 17
5 11 8
8 12 11