Part 1
1.1
#includeconst int N=5;int main() { int a[N] = { 1, 2, 3, 4, 5}; int i; for(i=0; i
1.2
#includeconst int N=5;int main() { char a[5] = { 'h','e','l','l','o'}; int i; for(i=0; i
1.3
#includeconst int N=5;int main() { double a[5] = { 1.0,2.0,3.0,4.0,5.0}; int i; for(i=0; i
Part 2
2.1
#includeint main() { int a[5]; a[0] = 1; a[1] = 9; a[2] = 8; a[3] = 6; a[4] = 0; printf("a[0] = %d\n", a[0]); printf("a[1] = %d\n", a[1]); printf("a[2] = %d\n", a[2]); printf("a[3] = %d\n", a[3]); printf("a[4] = %d\n", a[4]); return 0;}
2.2
#includeint main() { int a[5] = { 1, 9, 8, 6, 0}; int i; for(i=0; i<5; i++) printf("a[%d] = %d\n", i, a[i]); return 0;}
2.3
#includeint main() { int a[] = { 1, 9, 8, 6, 0}; int i,n; n = sizeof(a) / sizeof(a[0]); for(i=0; i
2.4
#includeint main() { int a[5] = { 1, 9}; int i; for(i=0; i<5; i++) printf("a[%d] = %d\n", i, a[i]); return 0;}
Part 3
3.1
#includeconst int N=5;int main() { int score[N] = { 99, 82, 88, 97, 85}; int i; for(i=0; i
3.2
#includeconst int N=5;void print(int x);int main() { int score[N] = { 99, 82, 88, 97, 85}; int i; for(i=0; i
3.3
#includeconst int N=5; void init(int a[],int n, int value); int main() { int b[N],i; init(b,N,-1); for(i=0;i
Part 4
#includeconst int N=5;void bubblesort(int [],int);int main(){ int i,a[N]; printf("请输入%d个整型数据: \n",N); for(i=0;i x[j+1]){ t=x[j]; x[j]=x[j+1]; x[j+1]=t; } } }}
Part 1-4总结
1.下标必须为整型值,表示了数组元素的位置
2.函数声明和函数定义中,函数名init后面括号里出现的是形式参数,数组名后面要加[ ]
Part 5
5.1
#includeint findMax(int a[], int n); const int N=5;int main() { int a[N]; int max, i; printf("输入%d个整数: \n", N); for(i=0;i max) max=a[j]; } return max;}
5.2
#includeconst int N=4;void output(char x[], int n); // 函数声明 // 排序函数声明void function(char a[],int n); int main() { char string[N] = { '2','0','1','9'}; int i; printf("排序前: \n"); output(string, N); // 调用排序函数对字符数组中的字符由大到小排序 function(string,N); printf("\n排序后: \n"); output(string, N); printf("\n"); return 0; } // 函数定义// 函数功能描述:输出包含有n个元素的字符数组元素// 形参:字符数组,以及字符数组元素个数void output(char x[], int n) { int i; for(i=0; i a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } }}
最后一个题不知道为什么一直报错,求大佬帮忙