作业帮 > 综合 > 作业

A C language program

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/24 04:25:40
A C language program
Write a complete program to do the following specification Using Functions :
1. Pickup 10 numbers and store it in an array using function fillArray.
2. Print the content of array with the message “Array not sorted” using printArray function.
3. Copy array a into array b using copyArray(a,b,10) function.
4. Sort array b in ascending order using sortArray(b,10) function.
5. Print array b with message “sorted array” using printArray
6. Pickup 10 numbers and store it in array c.
7. Find largest, smallest, and median values of array c and print them.
8. Print largest number of two arrays a,c and specify which array contains it.
9. Your program should ask for a number and print “YES” if the number is in array a, otherwise print “not found”
#include
#include
#include
#include
#include
void fillRandom(int * x , int n);
void printArray(int * x, int n, int k);
void doSort(int ** x, int n);
void copyArray (int * a,int * b,int n);
int main ()
{
int a[10];int i;int b[10];int c[10];int rn=0;
srand(time(NULL));

fillRandom(a,10);
printf("This is Array a:\n");
printArray(a,10,10);
printf("Array not sorted\n");
copyArray(a,b,10);
printf("\nThis is Array b(copy from Array a):\n");
printArray(b,10,10);
doSort(b,10);
printf("\nArray b is sorted now:\n");
for(i=0 ; i< 10 ; i++)
{ printf("%d ", b[i]);
}
fillRandom(c,10);
printf("\n\nThis is Array c:\n");
printArray(c,10,10);


doSort(c,10);

printf("\nThe Largest in Array c is: %d\n",c[0]);


printf("The smallest in Array c is: %d\n",c[9]);
printf("The middle values of Array c are %d and %d\n",c[5],c[4]);

doSort(a,10);

printf("\n%d is the largest in Array a\n",a[0]);
printf("%d is the largest in Array c\n",c[0]);


printf("\nenter a number to see if it is in Array a:");scanf("%d",&rn);
if ((rn==a[0])||(rn==a[1])||(rn==a[2])||(rn==a[3])||(rn==a[4])||(rn==a[5])||(rn==a[6])||(rn==a[7])||(rn==a[8])||(rn==a[9]))
printf("Yes!\n");
else
printf("Not found\n");
getch ();

return 0;
}
void fillRandom(int * x , int n)
{
int i=0;
for(i=0; i < n; i++)
x[i]= (rand()%(99-10+1) + 10);
}

void printArray(int * x, int n, int k)
{ int i;
for(i=0; i < n ; i++)
{ printf("%d ",x[i]);
if((i+1)%k==0)
printf("\n");
}
}

void copyArray (int * a,int * b, int n)
{ int i;
for (i=0; i