任务1
#include <stdio.h> #define N 4 int main() { int a[N] = {2, 0, 2, 3}; char b[N] = {'2', '0', '2', '3'}; int i; printf("sizeof(int) = %d\n", sizeof(int)); printf("sizeof(char) = %d\n", sizeof(char)); printf("\n"); // 输出int型数组a中每个元素的地址、值 for (i = 0; i < N; ++i) printf("%p: %d\n", &a[i], a[i]); printf("\n"); // 输出char型数组b中每个元素的地址、值 for (i = 0; i < N; ++i) printf("%p: %c\n", &b[i], b[i]); printf("\n"); // 输出数组名a和b对应的值 printf("a = %p\n", a); printf("b = %p\n", b); return 0; }
1.int型数组a在内存中是连续存放的,每个元素占用4个字节
2.char型数组在内存中也是连续存放的,每个元素占用1个字节
3.数组名a对应的值和&a[0]一样,数组名b对应的值和&b[0]一样
任务2
(1)
#include <stdio.h> #define N 2 #define M 3 int main() { int a[N][M] = {{1, 2, 3}, {4, 5, 6}}; char b[N][M] = {{'1', '2', '3'}, {'4', '5', '6'}}; int i, j; // 输出int型二维数组a中每个元素的地址、值 for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %d\n", &a[i][j], a[i][j]); printf("\n"); // 输出int型二维数组名a, 以及,a[0], a[1]的值 printf("a = %p\n", a); printf("a[0] = %p\n", a[0]); printf("a[1] = %p\n", a[1]); printf("\n"); // 输出char型二维数组b中每个元素的地址、值 for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %c\n", &b[i][j], b[i][j]); printf("\n"); // 输出char型二维数组名b, 以及,b[0], b[1]的值 printf("b = %p\n", b); printf("b[0] = %p\n", b[0]); printf("b[1] = %p\n", b[1]); printf("\n"); system("pause"); return 0; }
(2)
#include<stdio.h> int func(int,int); int main(){ int k=4,m=1,p1,p2; p1=func(k,m); p2=func(k,m); printf("%d,%d\n",p1,p2); return 0; } int func(int a ,int b){ static int m=0,i=2; i+=m+1; m=i+a+b; return m; }
static特性:只读取一次数据,数据在程序运行过程中不断被刷新
任务3
(1)
#include <stdio.h> long long func(int n); int main() { int n; long long f; while (scanf("%d", &n) != EOF) { f = func(n); printf("n = %d, f = %lld\n", n, f); } return 0; } long long func (int n){ int f; if(n==1) f=1; else f=(func(n-1)+1)*2-1; return f; }
(2)
#include<stdio.h> #include<stdlib.h> #define N 1000 int main(){ char line[N]; int word_len; int max_len; int end; int i; while(gets(line) != NULL){ word_len = 0; max_len = 0; end = 0; i =0; while(1){ while(line[i] == ' '){ word_len = 0; i++; } while(line[i] != '\0' && line[i] != ' '){ word_len++; i++; } if(max_len < word_len){ max_len = word_len; end = i; } if(line[i] == '\0') break; } printf("最长单词:"); for(i = end - max_len; i < end; ++i) printf("%c", line[i]); printf("\n\n"); } system("pause"); return 0; }
任务4
#include<stdio.h> #include<stdlib.h> #define N 5 void input(int x[], int n); void output(int x[], int n); double average(int x[], int n); void bubble_sort(int x[], int n); int main(){ int scores[N]; double ave; printf("录入%d个分数:\n", N); input(scores, N); printf("\n输出课程分数:\n", N); output(scores, N); printf("\n课程分数处理:计算均分,排序...\n"); ave = average(scores, N); bubble_sort(scores, N); printf("\n输出课程平均分:%.2f\n", ave); printf("\n输出课程分数(高->低):\n"); output(scores, N); system("pause"); return 0; } void input(int x[], int n){ int i; for(i = 0; i < n; ++i) scanf("%d", &x[i]); } void output(int x[], int n){ int i; for(i = 0; i < n; ++i) printf("%d ", x[i]); printf("\n"); } double average(int x[], int n){ int i; double s = 0; for(i = 0; i < n; ++i) s = s + x[i]; return s / n; } void bubble_sort(int x[], int n){ int i, j, t; for(i = 0; i < n-1; ++i) for(j = 0; j < n-i-1; ++j) if(x[j] > x[j+1]){ t = x[j]; x[j] = x[j+1]; x[j+1] = t; } }
任务5
标签:return,int,len,char,++,实验,printf From: https://www.cnblogs.com/chz-0225/p/17323954.html