首页 文章

在数组类型struct中查找Max和Min [关闭]

提问于
浏览
-2

我试图找到数组类型结构的max,min,mean和average . 我有一个函数“bsort”,它使用struct团队中的冒泡排序对我的所有数据进行排序,我在这里称它为我的数据fisrt排序 . 这是我用来查找最大值的代码:

void bfind(struct annual_stats *stats, int y, int year, string field, string item){ 
    bsort(stats, y, year, field, "increasing"); 

    if (item == "max"){
        int max;
     for (int i = 0; i < NO_TEAMS; i++) {

    if (stats->teams->games[i] > max){
            max = stats->teams->games[i];
         }
    }
}

}

其中stats是一个包含两个元素的结构,一个整数和另一个结构:

struct annual_stats{
int year;
struct team_stats teams[ NO_TEAMS ];
};

然后我有这些声明:

#define NO_TEAMS      32 // Number of NFL teams
#define TEAM_NAME_LEN 25 // Maximum team name string length
#define TOP_LEN        6 // Maximum time of possession string length

我的结构团队如下:

struct team_stats{
char team_name[ TEAM_NAME_LEN ]; // Name of NFL team
int games; // Number of games played in the season
float pts_per_game; // Points per game
int total_points; // Total points
int scrimmage_plays; // Scrimmage plays
float yds_per_game; // Yards per game
float yds_per_play; // Yards per play
float first_per_game; // First downs per game
int third_md; // Third down conversions
int third_att; // Third down attempts
int third_pct; // Third down percentage
int fourth_md; // Fourth down conversions
int fourth_att; // Fourth down attempts
int fourth_pct; // Fourth down percentage
int penalties; // Number of penalties
int pen_yds; // Penalty yards
char top_per_game[ TOP_LEN ]; // Time of possession per game
int fum; // Number of fumbles
int lost; // Fumbles lost
int to; // Turnover ratio
};

然后,teams是一个数组类型结构,它包含int,float和char类型中的20个元素 . 我想在这里做的是我想在游戏中找到最大值,这是团队的int成员 . 当我运行这个程序时,我不断收到一个错误,无效的类型int [int]为数组下标 . 我知道如果我删除[],我的程序编译,但我认为我必须指定元素[i]?有人可以给我一些提示吗?

我的程序正在从文本文件中读取数据 .

顺便说一句,我不是C的专业人士,所以请以最简单的方式解释!谢谢 .

1 回答

  • 3
    void bfind(struct annual_stats *stats, int y, int year, string field, string item){ 
        bsort(stats, y, year, field, "increasing"); 
    
        if (item == "max"){
            int max;
         for (int i = 0; i < NO_TEAMS; i++) {
    
        if (stats->teams[i].games > max){
                max = stats->teams[i].games;
             }
        }
    }
    

    我认为应该 stats->teams[i].games

相关问题