Computer Science using C Write a function that you pass it 5 integers. The funct
ID: 3640488 • Letter: C
Question
Computer Science using CWrite a function that you pass it 5 integers. The function should print the 5 numbers sorted.
For example, if you pass to the function the numbers: 6 8 -1 9 0
the function should print the numbers in this order: -1 0 6 8 9
If there are no duplicates the function returns 1 otherwise it returns 0.
In main() you also print a message saying either that there were duplicates or not, something like the following: There were duplicates in the input or No duplicates in the input.
Explanation / Answer
#include<stdio.h>
int sort(int []);
void main()
{
int a[5],i;
printf("Enter the 5 integers: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
if(sort(a))printf(" There were duplicates in the input");
else printf(" No duplicates in the input");
}
int sort(int a[])
{
int i,j,t,c=0;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[j]>=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
if(a[j]==a[j+1])c++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
if(c>0)return 1; else return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.