使用c++建立一个对象数组,内放5(至少5个)个学生的数据(学号、成绩),输出所有信息,统计出学生的个数,计算出平均成绩。
时间: 2024-06-10 09:09:48 浏览: 27
```c
#include <stdio.h>
#define MAX_STUDENTS 5
struct Student {
int id;
int score;
};
int main() {
struct Student students[MAX_STUDENTS] = {
{1001, 85},
{1002, 92},
{1003, 77},
{1004, 64},
{1005, 90},
};
int num_students = sizeof(students) / sizeof(struct Student);
int total_score = 0;
for (int i = 0; i < num_students; i++) {
printf("学生%d的学号是%d,成绩是%d\n", i + 1, students[i].id, students[i].score);
total_score += students[i].score;
}
printf("学生的个数是%d\n", num_students);
printf("平均成绩是%.2f\n", (float)total_score / num_students);
return 0;
}
```
相关问题
建立一个对象数组,内放5个学生的学号和成绩,用指针指向数组首元素输出全部学生的数据
可以使用以下代码建立对象数组并输出全部学生的数据:
```c++
#include <iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
int main() {
Student students[5] = {{1, 90}, {2, 85}, {3, 92}, {4, 88}, {5, 95}};
Student* p = students;
for (int i = 0; i < 5; i++) {
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
p++;
}
return 0;
}
```
输出结果为:
```
学号:1,成绩:90
学号:2,成绩:85
学号:3,成绩:92
学号:4,成绩:88
学号:5,成绩:95
```
用c++建立一个对象数组,内放5个学生的数据,用指针指向数组首元素,输出1.3.5学生的数据
好的,以下是代码实现:
```c
#include <stdio.h>
struct Student {
char name[20];
int age;
float score;
};
int main() {
struct Student students[5] = {
{"Tom", 18, 89.5},
{"Jerry", 19, 92.0},
{"Alice", 20, 85.5},
{"Bob", 19, 90.0},
{"Lisa", 18, 87.5}
};
struct Student *p = students;
printf("1st student: %s, %d, %.1f\n", p->name, p->age, p->score);
p += 2;
printf("3rd student: %s, %d, %.1f\n", p->name, p->age, p->score);
p += 2;
printf("5th student: %s, %d, %.1f\n", p->name, p->age, p->score);
return 0;
}
```
输出结果为:
```
1st student: Tom, 18, 89.5
3rd student: Alice, 20, 85.5
5th student: Lisa, 18, 87.5
```
希望这个回答能够帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)