Home>

### pass the array to the function in the sort program and sort it by the function side

The following sort program
I want to change it so that the function side sorts by passing an array to the function.
How should I do it?
(Of course, there are various sorting methods, but
I hope you can use the following sort as it is. )
I am a beginner. Please teach because it is for the future.
The following sort program generates 10 random numbers from numbers up to 1000,
It is to sort it.

``````#include<stdio.h>
#include<stdlib.h>
int main(void) {
int i, j, n;
n=10;
int a[n+1],b;

for (i=0;i<=n;i++){
a[i]=rand()%1000;//random number generation
}
for (i=0;i<=n-1;i++){
for (j=i+1;j<=n;j++){
if (a[i]>a[j]){
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
for (i=0;i<=n;i++){
printf("%3d<",a[i] );
}
return 0;
}``````
• Answer # 1

Does that mean you don't understand how to create a function?

``````#include<stdio.h>
#include<stdlib.h>
void mysort(int a[], int n){
int i, j, b;
for (i=0;i<n-1;i++){
for (j=i+1;j<n;j++){
if (a[i]>a[j]){
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
}
int main(void) {
int i, n;
n=10;
int a[n];
for (i=0;i<n;i++){
a[i]=rand()%1000;//random number generation
}
mysort(a,n);
for (i=0;i<n;i++){
printf("%3d<",a[i] );
}
printf("\n");
return 0;
}``````

Although it was written as 10, it was 11, so I fixed it.