16: Problem statement:
Write C++ program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five scores.
Program:
#include<stdio.h>
void selection(float[],int);
void bubble(float[],int);
void main()
{
int n,i,op;
float a[30];
do
{
printf("\n 1)Bubble sort \n 2)Selection sort \n 3)Quit");
printf("\n Enter your choice :");
scanf("%d", &op);
if(op==1)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
bubble(a,n);
printf("\n Sorted array is :");
for(i=n-1;i>=n-5;i--)
printf("%7.2f",a[i]);
}
if(op==2)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
selection(a,n);
printf("\n Sorted array is :");
for(i=n-1;i>=n-5;i--)
printf("%7.2f",a[i]);
}
}while(op!=3);
}
void bubble(float a[], int n)
{
int i,j;
float temp;
for(i=1; i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
void selection(float a[],int n)
{
int i,j,k;
float temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[j]<a[k])
k=j;
if(k !=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
Write C++ program to store first year percentage of students in array. Write function for sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and display top five scores.
Program:
#include<stdio.h>
void selection(float[],int);
void bubble(float[],int);
void main()
{
int n,i,op;
float a[30];
do
{
printf("\n 1)Bubble sort \n 2)Selection sort \n 3)Quit");
printf("\n Enter your choice :");
scanf("%d", &op);
if(op==1)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
bubble(a,n);
printf("\n Sorted array is :");
for(i=n-1;i>=n-5;i--)
printf("%7.2f",a[i]);
}
if(op==2)
{
printf("\n Enter no. of elements :");
scanf("%d",&n);
printf("\n Enter array elements :");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
selection(a,n);
printf("\n Sorted array is :");
for(i=n-1;i>=n-5;i--)
printf("%7.2f",a[i]);
}
}while(op!=3);
}
void bubble(float a[], int n)
{
int i,j;
float temp;
for(i=1; i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
void selection(float a[],int n)
{
int i,j,k;
float temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[j]<a[k])
k=j;
if(k !=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
Comments
Post a Comment