高级语言程序设计课程第七次个人作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C/
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13304
学号:102300108
姓名:陈茜蕾
1
#include <stdio.h>
#include<string.h>
#include<math.h>
#pragma warning (disable:4996)
int main() {
int arr[4][4] = { 0 };
int* p = *arr;
//数组形式进行赋值和输出
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = j;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ",arr[i][j]);
if (j == 3) {
printf("\n");
}
}
}
//用指针进行赋值和输出
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
*p = j + 1;
p++;
}
}
int* fp = *arr;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", *(fp + i * 4 + j));
if (j == 3) {
printf("\n");
}
}
}
}
2
#include <stdio.h>
#include<string.h>
#include<math.h>
#pragma warning (disable:4996)
int main() {
int a = 0;
int condition = 1;
int arr[20][20] = { 0 };
scanf("%d", &a);
int directions[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
int r = 0, c = 0;
int dir = 0; //0:右,1:下,2:左,3:上
int num = 1;
while (num <= a * a) {
arr[r][c] = num;
//计算下一个位置
int nr = r + directions[dir][0];
int nc = c + directions[dir][1];
if (nr < 0 || nr >= a || nc < 0 || nc >= a || arr[nr][nc] != 0) {
dir = (dir + 1) % 4;
nr = r + directions[dir][0];
nc = c + directions[dir][1];
}
r = nr;
c = nc;
num++;
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
3
#include <stdio.h>
#include<string.h>
#include<math.h>
#pragma warning (disable:4996)
int main() {
int num = 1;
int a = 0;
int condition = 1;
int arr[3][8] = { 0 };
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 8; j++) {
scanf("%d", &arr[i][j]);
arr[i][0] += arr[i][j];
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
4
5
#include <stdio.h>
#include<string.h>
#include<math.h>
#pragma warning (disable:4996)
int main() {
int a[3][3];
int b[3][3];
//给a赋值
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = i + j;
}
}
//把元素复制到b数组
int* p = *a;
int* fp = *b;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
*(fp + i * 3 + j) = *(p + i * 3 + j);
}
}
printf("a数组:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", *(p + i * 3 + j));
}
printf("\n");
}
printf("b数组:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", *(fp + i * 3 + j));
}
printf("\n");
}
}
6
7
#include <stdio.h>
#include<math.h>
#pragma warning (disable:4996)
int main() {
int a[3][4] = { 0 };
//指的是数组的大小,int占4字节,有3*4个int类型的数据,故为48
printf("%d\n", sizeof(a));
//指的是a[0][0]这个数组元素的大小,为4字节
printf("%d\n", sizeof(a[0][0]));
//指的是a[0][]这一行数组元素的大小,为4*4=16个字节
printf("%d\n", sizeof(a[0]));
//指的是指向a[0][]的第二个元素的指针大小,8字节
printf("%d\n", sizeof(a[0] + 1));
//指的是a[0][1]的大小,4字节
printf("%d\n", sizeof(*(a[0] + 1)));
//指的是指向a数组的第一个元素的指针大小,8字节
printf("%d\n", sizeof(a + 1));
//指的是a数组第一行的大小,16字节
printf("%d\n", sizeof(*(a + 1)));
//指的是指向a[0][1]指针的大小,8字节
printf("%d\n", sizeof(&a[0] + 1));
//指的是a[1]这一行的大小,16字节
printf("%d\n", sizeof(*(&a[0] + 1)));
//指的是a数组第一行的大小,16字节
printf("%d\n", sizeof(*a));
//指的是a数组第三行的大小,16字节
printf("%d\n", sizeof(a[3]));
}
8
总结
通过这次作业我更好地理解了指针的概念和移动,以及如何良好地运用,还理清了一些混淆的概念,收获很多!