1

I just made a function that needs to find the lowest amount of spectators at soccer games. It works for all values but one, team[10] where it seems as if it is pointing to a random location in memory and I cannot seem to find the reason. It seems random.

Output:
FCN, 37.250000
SDR, 40.408000
FCM, 66.279000
VFF, 42.273000
OB, 65.967000
HOB, 22.352000
AGF, 75.126000
BIF, 124.341000
AAB, 66.410000
EFB, 53.708000
FCK, 128.433000 // This value changes randomly for each time i compile
RFC, 48.314000
Team: HOB, Spectators:22.352000


void solve_task_four(ROUND *round, TEAM *team) {
  int j = 0;
  int i = 0;
  int k = 0;

  for(i=0; i<33; i++) { 
    for(j=0; j<6; j++) {
      if(round[i].match[j].year == 2015) {
        for(k=0; k<12; k++) {
          if(strcmp(round[i].match[j].home_team,team[k].name)==0) {
            team[k].spectators_home_last_year += round[i].match[j].spectators;
          }
        }
      }
    }
  }

  for(k=0; k<12; k++) {
    printf("%s, %lf\n", team[k].name, team[k].spectators_home_last_year);
  }
}

I have a structs of matches called round. A struct match where data about each match is stored. And then I have a struct team. I have made an array of teams where I am storing the data about the teams spectator numbers and names.

EDIT: It actually prints the correct value half the time. Other half it prints for example:

FCK, 11619132426987445786417210392049029283197879336796768202803907011075546173646479715331152202283963354268608554533948573765823213199739004181127023348833923632578496785562152249963391728687857881966870769941957750816768.000000

EDIT: The struct:

typedef struct {
  char *name;
  int points, matches_played, 
      matches_won, matches_draw, matches_lost, 
      matches_won_home, matches_won_away,
      goals_for, goals_against, goal_difference;
  double spectators_home_last_year;
} TEAM;

Filling the struct - an array of teams. (Where number of teams is 12)

TEAM team[NUMBER_OF_TEAMS];
asdasd
  • 59
  • 6
  • 1
    The error does not seem to be in the code you posted, can you show the code that fills the structs? Did you make sure that `team[10].spectators_home_last_year` is initialized to zero before calling `solve_task_four`? – Karsten Koop Nov 29 '16 at 11:58
  • 1
    Just added the structs themselves and the filling of the array of team. I havent initialized the variables to zero before calling tasks four. Trying to test if that is the cause now :-). – asdasd Nov 29 '16 at 12:09
  • The initialization of the teams seems to have fixed the issue. Thank you! – asdasd Nov 29 '16 at 12:24

0 Answers0