develop a c prgram that sorts theelements of an unsorted vector as a sorted incr
ID: 3842999 • Letter: D
Question
develop a c prgram that sorts theelements of an unsorted vector as a sorted increasing vector, where the elements are real numbers. the lenth of the unsorted vector is not a set value. the program gets the vector as an input.
1) sorts the vector with dupicates included and displays that vector
2) sorts the vector without dupicates ( ie with vector does not contain dupicate values) and displays the vector
3)is a vector only containing the duplicate vector elements.
a sample run is as shown
Explanation / Answer
Ans : 1) sorts the vector with dupicates included and displays that vector
#include <stdio.h>
#include<conio.h>
void main()
{
int i, j, temp, n, num[30];
printf("Enter the value of N ");
scanf("%d", &n);
printf("Enter the no. ");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("The numbers arranged in ascending order ");
for (i = 0; i < n; ++i)
printf("%d ", num[i]);
getch();
}
Ans : 2) sorts the vector without duplicates and displays the vector
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,n,v[30];
printf(“enter the no.”);
scanf("%d",&n);
printf(“ Enter elements of array ”);
for(i=0;i<n;++i)
scanf(“%d”,&v[i]);
for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(v[i]=v[j])
{
for(k=j;k<n-1;++k)
v[k]=v[k+1];
--n;
}
else
++j;
}
printf(“ ”);
for(i=0;i<n;++i)
printf(“%d “,v[i]);
getch();
}
Ans 3)
No, a vector is not only containing the duplicate vector elements.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.