c语言,通过数组输入10个数据代码
时间: 2024-02-25 18:59:08 浏览: 31
下面是C语言的代码,用于通过数组输入10个数据:
```
#include <stdio.h>
int main() {
int arr[10];
printf("请输入10个整数:\n");
for(int i=0; i<10; i++) {
scanf("%d", &arr[i]);
}
printf("您输入的10个整数是:\n");
for(int i=0; i<10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这段代码首先定义了一个长度为10的整型数组`arr`,然后通过for循环逐个输入数组元素的值,最后再通过另一个for循环逐个输出数组元素的值。
相关问题
C语言结构体数组如何输入数据
要输入 C 语言结构体数组的数据,可以使用循环语句和 scanf 函数来实现。下面是一个简单的示例代码:
```c
#include <stdio.h>
// 定义结构体
struct student {
char name[20];
int age;
float score;
};
int main() {
// 定义结构体数组
struct student stu[3];
int i;
// 输入数据
for (i = 0; i < 3; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
printf("姓名:");
scanf("%s", stu[i].name);
printf("年龄:");
scanf("%d", &stu[i].age);
printf("成绩:");
scanf("%f", &stu[i].score);
}
// 输出数据
for (i = 0; i < 3; i++) {
printf("第%d个学生的信息:\n", i+1);
printf("姓名:%s\n", stu[i].name);
printf("年龄:%d\n", stu[i].age);
printf("成绩:%f\n", stu[i].score);
}
return 0;
}
```
在这个例子中,我们首先定义了一个名为 `student` 的结构体,包含了学生的姓名、年龄和成绩三个数据成员。然后在 `main` 函数中,定义了一个 `stu` 数组,用于存储三个学生的信息。接着使用 for 循环语句和 scanf 函数,依次输入三个学生的信息,最后再使用 for 循环语句和 printf 函数,依次输出三个学生的信息。
c语言结构体数组输入输出
C语言结构体数组的输入输出可以通过循环和scanf、printf函数来实现。下面是一个示例:
```c
#include <stdio.h>
struct student {
char name[10];
int age;
char sex[1];
};
int main() {
struct student xs[3]; // 定义一个包含3个元素的结构体数组
// 输入结构体数组的数据
for (int i = 0; i < 3; i++) {
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", xs[i].name);
printf("请输入第%d个学生的年龄:", i+1);
scanf("%d", &xs[i].age);
printf("请输入第%d个学生的性别:", i+1);
scanf("%s", xs[i].sex);
}
// 输出结构体数组的数据
for (int i = 0; i < 3; i++) {
printf("第%d个学生的姓名:%s\n", i+1, xs[i].name);
printf("第%d个学生的年龄:%d\n", i+1, xs[i].age);
printf("第%d个学生的性别:%s\n", i+1, xs[i].sex);
}
return 0;
}
```
这段代码定义了一个包含3个元素的结构体数组`xs`,通过循环和`scanf`函数分别输入每个学生的姓名、年龄和性别。然后,通过循环和`printf`函数分别输出每个学生的姓名、年龄和性别。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.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)