1、第九次课上课程序及习题参考程序1、一维数组的下标法访问:int main(void) int a10; int i; for(i=0; i10; i+) scanf(%d, &ai); for(i=0; i10; i+) printf(%d, ai);return 0;2、一维数组的指针法访问:int main(void) int a10; int *p; for(p=a;p(a+10);p+) scanf(%d, p); for(p=a;p(a+10);p+) printf(%d , *p); return 0;3、冒泡排序中指针做形参#include void bubble_sort(in
2、t arr, int n);int main(void)int arr10, i;printf(Please input 10 numbers:n);for ( i = 0; i 10; i+)scanf(%d, &arri);bubble_sort(arr, 10);printf(The sorted numbers:n);for ( i = 0; i 10; i+)printf(%4d, arri);printf(n);return 0;void bubble_sort(int *p, int n)int i, j, temp;for (i = 0; i n - 1; i+)for ( j
3、 = 0; j pj+1)temp = pj;pj = pj+1;pj+1 = temp;4、有序表的有序插入#include void insert(int a, int n, int num);int main(void)int a6, num, i;printf(Input array(5):n);for (i = 0; i 5; i+)scanf(%d, &ai);printf(Input the insert num:);scanf(%d, &num);insert(a, 6, num);printf(Output:n);for (i = 0; i ai)/*寻找位置*/i+;for
4、 (j=n-1; ji; j-) /*移动*/aj = aj-1;ai = num;/*插入*/从后向前找void insert(int a, int n, int num)int i;i = n-1;while (i 0 & num ai-1)/*寻找和移动*/ai = ai-1;i-;ai = num;使用指针:#include void insert(int a, int n, int num);int main(void)int a6, num, i;printf(Input array(5):n);for (i = 0; i 5; i+)scanf(%d, &ai);printf(I
5、nput the insert num:);scanf(%d, &num);insert(a, 6, num);printf(Output:n);for (i = 0; i 0 & num pi-1)pi = pi-1;i-;pi = num;5、题库59题,编写一程序P2-217.C实现以下功能求任意的一个mm矩阵的对角线上元素之和#include int main(void)int arr2020;int i, j, m, sum;printf(Please input m: );scanf(%d, &m);printf(nPlease input array:n);for (i = 0;
6、 i m; i+)for (j = 0; j m; j+)scanf(%d, &arrij);sum = 0;for (i = 0; i m; i+)for (j = 0; j m; j+)if (i = j | i + j = m - 1)sum += arrij;printf(nsum=%dn, sum);return 0;使用行指针和列指针:#include int main(void)int arr2020, *p1, (*p2)20;int i, j, m, sum;printf(Please input m: );scanf(%d, &m);printf(nPlease input
7、 array:n);p1 = *arr;for (i = 0; i m; i+)for (j = 0; j m; j+)scanf(%d, p1+i*20+j);p2 = arr;sum = 0;for (i = 0; i m; i+)for (j = 0; j m; j+)if (i = j | i + j = m - 1)sum += *(*(p2 + i) + j);printf(nsum=%dn, sum);return 0;6、输入5个字符串,按升序排序后输出,用字符数组处理#include #include void bubble_sort(char str10, int n);i
8、nt main(void) int i; char str510; for (i=0; i5; i+) gets(stri); bubble_sort(str, 5);for (i=0; i5; i+) puts(stri); return 0;void bubble_sort(char str10, int n)int i, j;char temp10;for (i=0; in-1; i+)for (j=0; j 0)strcpy(temp, strj);strcpy(strj, strj+1);strcpy(strj+1, temp);7、输入5个字符串,按升序排序后输出,用指针数组完成#
9、include #include void bubble_sort(char *ps, int n);int main(void)int i;char str581, *ptr5;for (i = 0; i 5; i+)ptri = stri;printf(Input 5 strings:n);for (i = 0; i 5; i+)gets(ptri);bubble_sort(ptr, 5);printf(-n);for (i = 0; i 5; i+)puts(ptri);return 0;void bubble_sort(char *ps, int n)int i, j;char *te
10、mp;for (i=0; in-1; i+)for (j=0; j 0)temp = psj;psj = psj + 1;psj + 1 = temp;8、带参数的main()函数#include int main(int argc, char *argv)int i;printf(The program name is:%sn, argv0);if (argc 1)printf(The other arguments are following:n);for (i = 1; iargc; i+) printf(%sn, argvi);return 0;习题9-7参考程序题库104题,输入5个
11、字符串,排序,不能使用strcpy()和strcmp()函数p7-781.c只使用字符数组#include void bubble_sort(char s81, int n);int str_compare(char s1, char s2);void str_copy(char s1, char s2);int main(void)int i;char str581;printf(Input 5 strings:n);for (i = 0; i 5; i+)gets(stri);bubble_sort(str, 5);printf(-n);for (i = 0; i 5; i+)puts(s
12、tri);return 0;void bubble_sort(char s81, int n)int i, j;char temp81;for (i=0; in-1; i+)for (j=0; j 0)str_copy(temp, sj);str_copy(sj, sj+1);str_copy(sj+1, temp);int str_compare(char s1, char s2)int i;i = 0;while ( s1i = s2i)i+;return (s1i - s2i);void str_copy(char s1, char s2)int i;for (i = 0; s2i !=
13、 0; i+)s1i = s2i;s1i = 0;使用指针数组#include void bubble_sort(char *ps, int n);int str_compare(char *p1, char *p2);int main(void)int i;char str581, *ptr5;for (i = 0; i 5; i+)ptri = stri;printf(Input 5 strings:n);for (i = 0; i 5; i+)gets(ptri);bubble_sort(ptr, 5);printf(-n);for (i = 0; i 5; i+)puts(ptri);return 0;void bubble_sort(char *ps, int n)int i, j;char *temp;for (i=0; in-1; i+)for (j=0; j 0)temp = psj;psj = psj + 1;psj + 1 = temp;int str_compare(char *p1, char *p2)while ( (*p1) = (*p2)p1+;p2+;return (*p1) - (*p2);17