c语言在3行4列的二维数组储存学生名字,学号,分数,怎样在数组添加第四行学生的信息
时间: 2024-03-24 21:41:17 浏览: 31
假设我们已经定义了一个3行4列的二维数组`studentInfo`来存储学生的信息,其中每一行分别表示学生的姓名、学号、分数,可以通过以下代码向数组中添加第四行学生的信息:
```c
#include <stdio.h>
#include <string.h>
int main() {
char studentInfo[4][20]; // 声明一个3行4列的二维数组
// 初始化前三行
strcpy(studentInfo[0], "张三");
strcpy(studentInfo[1], "1001");
strcpy(studentInfo[2], "85.5");
// 添加第四行学生的信息
strcpy(studentInfo[3], "李四");
strcpy(studentInfo[4], "1002");
strcpy(studentInfo[5], "90.0");
return 0;
}
```
在这个代码中,我们首先声明了一个3行4列的二维字符数组`studentInfo`,然后通过`strcpy`函数向前三行分别添加了一个学生的姓名、学号、分数。最后,我们通过`strcpy`函数向第四行分别添加了另一个学生的姓名、学号、分数。这样,我们就成功地向二维数组中添加了第四行学生的信息。当然,如果我们要添加更多的学生信息,就需要逐行使用`strcpy`函数进行添加。
相关问题
c语言在3行4列的二维数组储存学生名字,学号,分数,怎样在数组添加新学生的信息,数组还不知道要多少行
如果数组还不知道要多少行,则可以使用动态内存分配来解决。下面是一个示例代码,用来向二维数组中添加新学生:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 3
#define COLS 4
int main() {
// 动态分配二维数组内存
char **studentInfo = (char **)malloc(sizeof(char *) * ROWS);
for (int i = 0; i < ROWS; i++) {
studentInfo[i] = (char *)malloc(sizeof(char) * 20);
}
// 向数组中添加学生信息
int curRow = 0;
char name[20], id[10], score[10];
while (1) {
printf("请输入第%d个学生的信息(姓名 学号 分数):", curRow+1);
scanf("%s %s %s", name, id, score);
if (strcmp(name, "exit") == 0) { // 如果输入exit,则结束输入
break;
}
// 如果行数已经达到上限,则重新分配内存
if (curRow == ROWS) {
ROWS += 1;
studentInfo = (char **)realloc(studentInfo, sizeof(char *) * ROWS);
studentInfo[ROWS-1] = (char *)malloc(sizeof(char) * 20);
}
// 添加学生信息到数组中
strcpy(studentInfo[curRow][0], name);
strcpy(studentInfo[curRow][1], id);
strcpy(studentInfo[curRow][2], score);
curRow++;
}
// 打印输出学生信息
printf("学生信息如下:\n");
for (int i = 0; i < curRow; i++) {
printf("%s %s %s\n", studentInfo[i][0], studentInfo[i][1], studentInfo[i][2]);
}
// 释放内存
for (int i = 0; i < ROWS; i++) {
free(studentInfo[i]);
}
free(studentInfo);
return 0;
}
```
该代码使用了动态内存分配来动态地创建二维字符数组`studentInfo`,并通过循环向数组中添加学生信息。在循环过程中,如果行数已经达到上限,则使用`realloc`函数重新分配内存,以扩充数组的行数。最后,我们通过循环打印输出学生信息,并在程序结束前释放内存。
c语言用二维数组在一行中储存 名字,学号,分数,储存3个人的,代码实现,名字等内容要等用户输入
好的,以下是代码实现:
```c
#include <stdio.h>
int main() {
char student_info[3][3][20]; // 定义一个3行3列的二维数组,每个元素可以储存20个字符
// 循环输入3个人的名字、学号、分数
for (int i = 0; i < 3; i++) {
printf("请输入第%d个学生的名字:", i+1);
scanf("%s", student_info[i][0]); // 第i个学生的名字储存在第i行第0列
printf("请输入第%d个学生的学号:", i+1);
scanf("%s", student_info[i][1]); // 第i个学生的学号储存在第i行第1列
printf("请输入第%d个学生的分数:", i+1);
scanf("%s", student_info[i][2]); // 第i个学生的分数储存在第i行第2列
}
// 输出每个学生的名字、学号、分数
printf("姓名\t学号\t分数\n"); // 格式化输出表头
for (int i = 0; i < 3; i++) {
printf("%s\t%s\t%s\n", student_info[i][0], student_info[i][1], student_info[i][2]); // 格式化输出第i个学生的信息
}
return 0;
}
```
以上代码使用了一个3行3列的二维数组,每行分别储存一个学生的名字、学号、分数。在循环中,先询问用户输入每个学生的信息,然后储存在相应的元素中。最后,使用格式化输出的方法输出每个学生的信息。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)