配套代码笔记仓库。目录目录数组一维数组练习部分斐波那契数列前10项数据排序冒泡选择法进制转换删除法求质数二维数组练习部分行列互换求最大值及其所在位置求各行与各列的和矩阵乘积字符数组常用函数练习部分单词计数多维数组数组构造类型之一,连续存放。[!warning]时间关系,大量的练习题,没有做笔记,只记录了题目。自己思考加看课程足矣。一维数组定义【存储类型】 数据类型 标识符 【下标】初始化不初始化全部初始化部分初始化static元素引用数组名【下标】数组名数组名是表示地址的常量,也是数组的起始位置。数组越界练习部分斐波那契数列前10项static void fibonacci(void) { int fib[10] = {1, 1}; for (int i = 2; i < sizeof(fib) / sizeof(fib[0]); i++) { fib[i] = fib[i - 2] + fib[i - 1]; } for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) { printf("fib[%d] = %d\n", i, fib[i]); } printf("\n"); int i = 0; int j = sizeof(fib) / sizeof(fib[0]) - 1; int tmp; while (i < j) { tmp = fib[i]; fib[i] = fib[j]; fib[j] = tmp; i++; j--; } for (int i = 0; i < sizeof(fib) / sizeof(fib[0]); i++) { printf("fib[%d] = %d\n", i, fib[i]); } }数据排序冒泡#define N 10 static void sort1(void) { int a[N] = {12, 8, 45, 30, 98, 67, 2, 7, 68, 11}; int tmp; for (int i = 0; i < N; i++) { printf("%d ", a[i]); } printf("\n"); for (int i = 0; i < (N - 1); i++) { for (int j = 0; j < N - 1 - i; j++) { if (a[j] > a[j + 1]) { tmp = a[j]; a[j] = a[j + 1]; a[j + 1] = tmp; } } } for (int i = 0; i < N; i++) { printf("%d ", a[i]); } printf("\n"); }选择法static void sort2(void) { int a[N] = {23, 45, 90, 76, 13, 55, 76, 45, 3, 8}; int i, j, k, tmp; for (int i = 0; i < N; i++) { printf("%d ", a[i]); } printf("\n"); for (i = 0; i < N - 1; i++) { k = i; for (j = i + 1; j < N; j++) { if (a[j] < a[k]) k = j; } if (i != k) { tmp = a[i]; a[i] = a[k]; a[k] = tmp; } } for (int i = 0; i < N; i++) { printf("%d ", a[i]); } printf("\n"); }进制转换static void base_convert(void) { int num, base; int n[128]; int i = 0; printf("Please enter the converted num:\n"); scanf("%d", &num); printf("Please enter the base:\n"); scanf("%d", &base); do { n[i] = num % base; num = num / base; i++; } while (num != 0); for (i--; i >= 0; i--) { if (n[i] >= 10) printf("%c", n[i] - 10 + 'A'); else printf("%d", n[i]); } printf("\n"); }删除法求质数static void primer(void) { char primer[1001] = {0}; int i, j; for (i = 2; i < 1001; i++) { for (j = i * 2; j < 1001; j += i) primer[j] = -1; } for (i = 2; i < 1001; i++) { if (primer[i] == 0) printf("%d is a primer.\n", i); } }二维数组定义,初始化【存储类型】 数据类型 标识符 【行下标】 【列下标】[!note]初始化时,行号可省,列号不可省元素引用数组名 【行标】【列标】存储形式顺序存储,按行存储对于二维数组的深入理解int a[M][N] = {1, 2, 3, 4, 5}; int i, j; printf("a = %p\n", a); printf("a+1 = %p\n", a + 1); for (i = 0; i < M; i++) { for (j = 0; j < N; j++) { printf("%p --> %d\n", &a[i][j], a[i][j]); } printf("\n"); }输出:a = 0x7ffcc2376f80 a+1 = 0x7ffcc2376f8c 0x7ffcc2376f80 --> 1 0x7ffcc2376f84 --> 2 0x7ffcc2376f88 --> 3 0x7ffcc2376f8c --> 4 0x7ffcc2376f90 --> 5 0x7ffcc2376f94 --> 0a+1跨越了行而不是单个元素。练习部分行列互换求最大值及其所在位置求各行与各列的和矩阵乘积字符数组定义,初始化,存储特点【存储类型】 数据类型 标识符 【下标】...单个字符初始化用字符串常量初始化输入输出常用函数常用函数strlen和sizeofsize_t strlen(const char *s); // strlen函数可以后去字符串的长度,不包括尾'\0' // 以'\0'作为结束,所以对于"hello\0abc",abc就不计入 // sizeof是完整的识别。 char str[] = "hello\0abc"; printf("%d\n", strlen(str)); // out: 5 printf("%d\n", sizeof(str)); // out: 6strcpy与strncpychar *strcpy(char *dest, const char *src); // 把src内容拷贝到dest,以'\0'结束,返回dest的地址 // 当src大于dest,造成越界 char *strncpy(char *dest, const char *src, size_t n); // 把src内容拷贝n个字节到dest strcpy(str, "abcde"); puts(str); // out: abcde strncpy(str, "abcde", STR_SIZE); puts(str); // out: abcdestrcat与strncatchar *strcat(char *dest, const char *src); // 把src补到dest后面 // 需要保证dest空间足够 char *strncat(char *dest, const char *src, size_t n); // 把src拿最多n个字符到dest strcat(str1, " "); strcat(str1, "world"); // out:hello world strncat(str, " ", STR_SIZE); strncat(str, "world", STR_SIZE); // out:hello worldstrcmp与strncmpint strcmp(const char *s1, const char *s2); // 返回两个字符串,ascii码的差值 int strncmp(const char (s1, const char *s2, size_t n); // 指定比较前5个字符练习部分单词计数多维数组int a[2][3][4];