Uploaded by Fatima Khalil

OOP LAB 06

advertisement
LAB TASKS
1. Make an array of [8,10,25,95,67,88,97] and arrange it in descending order.
2. Write a program to make an array of 6 integers and arrange it in ascending order using
i.
ii.
Bubble sorting
Selection sorting
Compare both the codes and algorithms.
3. Make an array of 5 integers using for loop taking input from user and use selection sorting to
arrange them in descending order.
1.
#include <iostream>
using namespace std;
int main()
{
int i,j,max=0,tem=0;
int a[7]={8,10,25,95,67,88,97};
for(i=0;i<=6;i++){
max=a[i];
for(j=i+1;j<=6;j++){
if(a[j]>=a[i]){
tem=a[i];
a[i]=a[j];
a[j]=tem;
}
}
}
cout<<"ARRAY IN DESCENDING ORDER: "<<endl;
for(i=0;i<=6;i++){
cout<<a[i]<<endl;
}
return 0;
}
2.
I
#include <iostream>
using namespace std;
int main()
{
int a[6];
int i=0,j=0;
cout<<"ENTER ELEMENTS OF ARRAY: "<<"\n";
for(i=0;i<=5;i++){
cin>>a[i];
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<=5;j++)
{
int flag;
if (a[i]>=a[j])
{
flag=a[i];
a[i]=a[j];
a[j]=flag;
}
}
}
cout<<"ARRAY IN ASCENDING ORDER"<<"\n";
for (i=0;i<=5;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
II
#include <iostream>
using namespace std;
int main()
{
int i,j,min=0,tem=0,a[6];
cout<<"ENTER ELEMENTS OF ARRAY: ";
for(i=0;i<=5;i++){
cin>>a[i];
}
for(i=0;i<=5;i++){
min=a[i];
for(j=i+1;j<=5;j++){
if(a[j]<=a[i]){
tem=a[i];
a[i]=a[j];
a[j]=tem;
}
}
}
cout<<"ARRAY IN ASCENDING ORDER: "<<endl;
for(i=0;i<=5;i++){
cout<<a[i]<<endl;
}
return 0;
}
ALGORITHM
BUBBLE SORTING
SELECTION SORTING
Assign values to array or take input from user.
Assign values to array or take input from user.
Then the code repeatedly switches the adjacent Then find the maximum or minimum number
elements if elements are in wrong order.
from the array depending on whether to
arrange array in descending or ascending order.
Arranged or sorted array is displayed.
Then switch the maximum or minimum number
with the first element in array.
Then repeat process from the remaining array
excluding the element that is arranged.
3.
#include <iostream>
using namespace std;
int main()
{
int i,j,max=0,tem=0,a[5];
cout<<"ENTER ELEMENTS OF ARRAY: ";
for(i=0;i<=4;i++){
cin>>a[i];
}
for(i=0;i<=4;i++){
max=a[i];
for(j=i+1;j<=4;j++){
if(a[j]>=a[i]){
tem=a[i];
a[i]=a[j];
a[j]=tem;
}
}
}
cout<<"ARRAY IN DESCENDING ORDER: "<<endl;
for(i=0;i<=4;i++){
cout<<a[i]<<endl;
}
return 0;
}
Download