结构体数组

阅读量: 179 编辑

一、结构体数组

方式一

struct student {
    int no;             // 学号
    char name[20];      // 姓名
    double score;       // 学分
} s1[6]; //定义结构体数组


int main() {
    cout << s1[0].no << endl;
    return 0;
}

方式二

struct student {
    int no;             // 学号
    char name[20];      // 姓名
    double score;       // 学分
};

int main() {
    student s2[6];		//数组
    cout << s2[0].no << endl;
    return 0;
}

二、结构体数组:计算平均分

#include <iostream>
using namespace std;

struct student {
    int no;             // 学号
    char name[20];      // 姓名
    double score;       // 学分
};

int main() {
    student arr[3] = {
        {1001,"张三",100},
        {1002,"李四",97},
        {1003,"王五",90},
    };
    
    //求平均分
    double totalScore = 0;
    for(int i = 0; i < 3; i++){
        totalScore += arr[i].score;
    }
    
    cout << totalScore/3.0 << endl;
    return 0;
}
爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司