使用c++创建一个对象数组,数组的元素是学生,学生的信息包括学号、姓名和成绩,在main函数之中将数组中大于80分的学生信息显示出来
时间: 2023-04-07 07:05:06 浏览: 339
好的,这个问题可以回答。以下是使用C语言创建学生对象数组并显示大于80分的学生信息的代码示例:
```c
#include <stdio.h>
typedef struct {
int id;
char name[20];
int score;
} Student;
int main() {
Student students[5] = {
{1, "Tom", 78},
{2, "Jerry", 85},
{3, "Alice", 92},
{4, "Bob", 76},
{5, "Lisa", 88}
};
printf("大于80分的学生信息:\n");
for (int i = 0; i < 5; i++) {
if (students[i].score > 80) {
printf("学号:%d,姓名:%s,成绩:%d\n", students[i].id, students[i].name, students[i].score);
}
}
return 0;
}
```
输出结果为:
```
大于80分的学生信息:
学号:2,姓名:Jerry,成绩:85
学号:3,姓名:Alice,成绩:92
学号:5,姓名:Lisa,成绩:88
```
希望这个回答能够帮到你!
相关问题
c++创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
#include <iostream>
using namespace std;
class Student {
public:
int id;
string name;
float score;
};
int main() {
Student students[5] = {
{101, "张三", 78},
{102, "李四", 85},
{103, "王五", 92},
{104, "赵六", 74},
{105, "钱七", 80}
};
for (int i = 0; i < 5; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
c++创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int num;
string name;
int score;
Student(int num, string name, int score) {
this->num = num;
this->name = name;
this->score = score;
}
};
int main() {
Student students[] = {
Student(1, "Tom", 85),
Student(2, "Jerry", 75),
Student(3, "Mary", 90),
Student(4, "John", 80),
Student(5, "Lisa", 95)
};
int len = sizeof(students) / sizeof(Student);
for (int i = 0; i < len; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].num << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
阅读全文