-->
当前位置:首页 > 题库

PROGRAMMING:Analysis of array comprehensive application code

Luz5年前 (2021-05-10)题库443
Please analyze the following code to understand the comprehensive application of array. If you want to pass this question, please submit the last code directly.
```
int a[201],size=0; // Global variables
#include
#include
#include
int menu(); // Function declaration
int print();
int init();
void sort();
int max();
void min();
int sum();
int average();
int append();
int get_ index(int n);
int find();
void del();
Int main() {/ / main function
char c='#';
while(c!=' X'){
c=menu();
switch(c){
case 'P': print(); break;
case 'I': init(); break;
case 'S': sort(); break;
case 'D': del(); break;
case 'M': max(); break;
case 'N': min(); break;
case 'U': sum(); break;
case 'G': average(); break;
case 'A': append(); break;
case 'F': find(); break;
case 'X': break;
default:printf ("wrong command...);
}
if(c!=' X'){
Printf ("\ npress any key to continue...);
getch();
}
}
}
int menu(){
Printf ("\ n ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^;
Printf ("\ n * I. initialize U. all elements and *");
Printf ("\ n * P. output g. average value of all elements *");
Printf ("\ n * s. sort A. add element at the end *");
Printf ("\ n * M. max. F. find element *");
Printf ("\ n * n. minimum D. delete element *");
Printf ("\ n * X. exit program *");
printf("\n***");
Printf ("\ n please enter your choice?");
char c;
c=getche();
if(c>='a'&&c<='z')c=c-32;
return c;
}
int print(){
int i; Printf ("- array has% d elements in total:,, size));
for(i=0; i}
int init(){
Printf ("- the system initialization array is five random integers from 1 to 1000.");
int i;
size=5;
srand(time(NULL));
for(i=0; iprint();
}
void sort(){
int i,j,t;
for(i=0; ifor(j=i+1; jif(a[i]t=a[i]; a[i]=a[j]; a[j]=t;
}
Printf ("- sort complete...");
print();
}
int max(){
int m,i;
If (size = = 0) printf ("- no element!");
else{
m=a[0];
for(i=1; iPrintf ("\ nmaximum element% d", m));
}
}
void min(){
int m,i;
If (size = = 0) printf ("- no element!");
else{
m=a[0];
for(i=1; ia[i])m=a[i];
Printf ("\ nminimum element% d", m));
}
}
int sum(){
int s=0,i;
If (size = = 0) printf ("- no element!");
else{
for(i=0; iPrintf ("- sum of all elements: D", s));
}
}
int average(){
int s=0,i;
If (size = = 0) printf ("- no element!");
else{
for(i=0; iPrintf ("- average value of all elements: LF", (double) s / size));
}
}
int append(){
int i,p=0,n;
Printf ("- please enter the element to be added:);
scanf("%d",&n);
a[size++]=n;
print();
}
int get_ index(int n){
int i;
for(i=0; iif(a[i]==n) return i;
return -1;
}
int find(){
int i,n,index;
Printf ("- please enter the element you want to find:);
scanf("%d",&n);
index=get_ index(n);
if(index!=- 1)
Printf ("- found, index is% d.", index));
else
Printf ("\ nnot found.");
}
void del(){
int n,i,index;
Printf ("- please enter the element you want to find:);
scanf("%d",&n);
index=get_ index(n);
if(index==-1){
Printf ("element with value% d not found.", n));
return;
}
for(i=index; isize--;
print();
}
```
###Input example:
```in
```
###Output example:
```out
0
```
###Output the code of 0. Please submit the code directly:
```
#include
int main(){
printf("0");
return 0;
}
```







answer:If there is no answer, please comment