Thread: Sorting from a txt file

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    6

    Sorting from a txt file

    Hello programmers.

    I am actually coding a software that have to sort data from a text file. the data is results from soccer tournement. I've sorted the matches with more than 7 goals, now I want to sort out the match that the total goal count is the highest
    So the algorithm I have in mind is this

    Code:
    while (fscanf(blalba) !=EOF) 
        { 
          for(i = 0; i >= total_goals; i++)
              if( i >=total_goals)
                  printf(The results);
        }
    Now this doesn't seem to really work, any help? I would really appreciate it.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    That's not a sorting algorithm?

    In fact, unless total_goals is less then or equal to 0, it doesn't even do anything, let alone sort. Perhaps you wanted <= (less than or equal to) not >= greater than or equal to?

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Quote Originally Posted by nonpuz View Post
    That's not a sorting algorithm?

    In fact, unless total_goals is less then or equal to 0, it doesn't even do anything, let alone sort. Perhaps you wanted <= (less than or equal to) not >= greater than or equal to?
    Thank you for your respons
    Well could you give me an idea of how I could solve this problem?

    Well since total_goals is running into a while loop, the highest value is 8. I could sort out the match manually by using if statement and setting it to equal or greater than 8, but I wanted to make a generel solution for the problem.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The problem statement could use some clarification before worrying about code or algorithms. What is the problem you're trying to solve exactly. Also, give at least one or two examples of what the input file looks like and what should the output be in each case of input.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Quote Originally Posted by c99tutorial View Post
    The problem statement could use some clarification before worrying about code or algorithms. What is the problem you're trying to solve exactly. Also, give at least one or two examples of what the input file looks like and what should the output be in each case of input.
    Thank you for your reply and I am sorry about the poor explaination.

    Okay - The problem I want to solve is following:
    I have a list of soccer tournement with the results. I have to write a program that does following:

    a. Sort the matches where the total goals is 7 or more.(DONE)
    b. Sort out the rounds where the goal values are highest.

    I will show a little of the txt file since is it large I want don't to annoy anyone.
    Round Date Time Hometeam - AwayTeam Homescore awayscore and the spectators
    1 13.07.2012 kl. 18.30 AGF - AAB 1 - 1 9.728
    1 14.07.2012 kl. 17.00 SDR - RFC 6 - 1 2.995
    1 15.07.2012 kl. 14.00 EFB - SIF 2 - 3 6.226
    1 15.07.2012 kl. 17.00 BIF - OB 0 - 1 9.102
    1 15.07.2012 kl. 19.00 FCK - FCM 4 - 2 10.806
    1 16.07.2012 kl. 19.00 ACH - FCN 0 - 4 3.180
    2 20.07.2012 kl. 18.30 SIF - ACH 0 - 2 3.166
    2 21.07.2012 kl. 17.00 FCM - FCN 3 - 1 4.315
    2 22.07.2012 kl. 14.00 OB - RFC 0 - 1 7.123
    2 22.07.2012 kl. 17.00 SDR - FCK 1 - 1 6.575
    2 22.07.2012 kl. 19.00 AAB - BIF 2 - 1 7.895
    2 23.07.2012 kl. 19.00 AGF - EFB 0 - 0 7.323
    3 27.07.2012 kl. 18.30 ACH - FCM 2 - 2 3.579
    3 28.07.2012 kl. 15.00 FCK - AAB 3 - 0 9.571
    3 28.07.2012 kl. 17.00 EFB - SDR 1 - 2 5.345
    3 29.07.2012 kl. 17.00 RFC - AGF 2 - 1 6.595
    3 29.07.2012 kl. 19.00 FCN - OB 1 - 1 5.736
    3 30.07.2012 kl. 19.00 BIF - SIF 2 - 1 9.586
    4 03.08.2012 kl. 18.30 SIF - OB 0 - 1 3.025
    Notice this is only a little part of the file.

    Code:
    void greater_than_7(void)
    {
      int round, day, month, year, home_goal, away_goal, spectator1, spectator2;
      double time;
      char home_team[TEAM_LEN], away_team[TEAM_LEN];
      int total_goals;
      int i;
      FILE *inp;
    
    
      if(inp != NULL)
          {
    
    
        inp = fopen("superliga-2012-2013", "r");
    
    
        while(fscanf(inp, "%d %d.%d.%d kl. %lf %s - %s %d - %d %d.%d", &round, &day, &month, &year, &time, home_team, away_team, &home_goal,
                                                                     &away_goal, &spectator1 ,&spectator2) !=EOF)
        {
              total_goals = home_goal + away_goal;
    
    
              if(total_goals >= 7)
               {
                printf("%d %d.%d.%d kl. %.2lf %s - %s %d - %d %d.%d\n", round, day, month, year, time, home_team, away_team, home_goal,                                   \
    
    
                     away_goal, spectator1 ,spectator2);
                }
          } }
    
    
      else
        printf("The file does not exist");
    }
    This is actually how I solved the first problem

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by AverageJoe55 View Post
    Thank you for your reply and I am sorry about the poor explaination.

    Okay - The problem I want to solve is following:
    I have a list of soccer tournement with the results. I have to write a program that does following:

    a. Sort the matches where the total goals is 7 or more.(DONE)
    b. Sort out the rounds where the goal values are highest.
    Be careful with the word sort because it normally implied you want to create a total ordering.

    To solve your second problem, think how you would solve it for a simple input scheme. Suppose you are reading just a sequence of integers. How would you determine which one is the maximum? You can apply the same principle solution to your larger problem.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Oh my bad I apologize for the mistunderstanding
    It is easier to imagine with a simple input scheme, then a simple if loop would do the job. I am kind of lost here, any hints? :-)

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This would be easier with structs, but...

    You'll need a function like the one you have posted already, but you need to add in the maxGoals, maxRound, maxHome_goal, maxAway_goal, etc., variables.

    Two variables in your program are reserved words in C, and should be changed: round and time. You can use myRound and myTime or whatever you like, (here, round1 and time1), but not "round" or "time".

    Then you need to assign maxGoals to zero, before the loop that reads the file.

    Inside the loop that reads the file, you need something like this pseudo code:
    Code:
          if (maxGoals less than or equal to total_goals) {
             maxRound=round1;
             maxHome_goal=home_goal;
             maxAway_goal=away_goal;
             maxGoals = total_goals;
             //etc. Add other variable assignments as needed
          }
    and you're set. The equal to part ensures that matches that end in 0 - 0, maybe early in the season, are possible answers to this question.

    This won't find ties however. The last high match will be kept, only.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Quote Originally Posted by Adak View Post
    This would be easier with structs, but...

    You'll need a function like the one you have posted already, but you need to add in the maxGoals, maxRound, maxHome_goal, maxAway_goal, etc., variables.

    Two variables in your program are reserved words in C, and should be changed: round and time. You can use myRound and myTime or whatever you like, (here, round1 and time1), but not "round" or "time".

    Then you need to assign maxGoals to zero, before the loop that reads the file.

    Inside the loop that reads the file, you need something like this pseudo code:
    Code:
          if (maxGoals less than or equal to total_goals) {
             maxRound=round1;
             maxHome_goal=home_goal;
             maxAway_goal=away_goal;
             maxGoals = total_goals;
             //etc. Add other variable assignments as needed
          }
    and you're set. The equal to part ensures that matches that end in 0 - 0, maybe early in the season, are possible answers to this question.

    This won't find ties however. The last high match will be kept, only.
    Thank you for your reply.
    Well I will try to rewrite the program using structs when I've solved this problem.

    I have been thinking about it, and the solution must be following : I have to check every round and since every round has 6 games and then find the highest value of total_goals.

    Could you help me to make this into a C code :
    there is always 6 matches per same round.

    if(round is the same number e.g 1 or 2 og 3 )
    add the goals to an int.

    then print only the rounds with the highest goals and print how many goals that would be.

    it is so easy when I think about it, but when I try to put into codes it's just not working. I really hope you will help me out.

    And thank you for telling me about the reserved words. - I have fixed them

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is your program, with just a bit of code that I posted for you, before, included - perhaps a bit more.

    The data is from your example.

    Code:
    #include <stdio.h>
    
    #define TEAM_LEN 4              //3+end of string 0
    
    void greatest(void);
    
    int main()
    {
    
       greatest();
    
       return 0;
    }
    
    void greatest(void)
    {
       int round1, day, month, year, home_goal, away_goal, spectator1, spectator2;
       int maxRound, maxHome_goal, maxAway_goal, maxGoals = 0;  //etc.
       double time1;
       char home_team[TEAM_LEN], away_team[TEAM_LEN];
       int total_goals;
       int i;
       FILE *inp;
       char row1[100];
    
    
    
       inp = fopen("superliga-2012-2013", "r");
    
    
       if (inp == NULL) {
          printf("Error, file not found.\n");
          return;
       }
    
       fgets(row1, 100, inp);       /* get past the first row of info, the header */
       printf("%s\n\n",row1);
    
       while (fscanf(inp, "%d %d.%d.%d kl. %lf %s - %s %d - %d %d.%d", &round1, &day, &month, &year, &time1, home_team, away_team, &home_goal, &away_goal, &spectator1, &spectator2) != EOF) {
          total_goals = home_goal + away_goal;
          
          //if (total_goals >= 7) {
          //   printf("%2d %2d.%2d.%4d kl. %.2lf %4s - %4s %2d - %2d %d.%d\n", round1, day, month, year, time1, home_team, away_team, home_goal, away_goal, spectator1, spectator2);
          //}
          printf("Total goals: %d    home_goal: %d  away_goal: %d \n",total_goals,home_goal,away_goal);
          
          
          if (maxGoals <= total_goals) {
             maxRound=round1;
             maxHome_goal=home_goal;
             maxAway_goal=away_goal;
             maxGoals = total_goals;
             //etc.
          }
       }
       fclose(inp);
       printf("\nmaxRound: %d  maxHome_goal: %d  maxAway_goal: %d  maxGoals: %d \n",
          maxRound, maxHome_goal, maxAway_goal, maxGoals);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2011, 12:16 AM
  2. File I/O - Sorting
    By Fekore in forum C Programming
    Replies: 10
    Last Post: 04-05-2011, 09:33 PM
  3. sorting in a file!!
    By Leojeen in forum C Programming
    Replies: 14
    Last Post: 09-25-2008, 12:35 AM
  4. Sorting a file with a lot of words.
    By samus250 in forum C Programming
    Replies: 28
    Last Post: 04-27-2008, 01:36 PM
  5. File Access and Sorting
    By anewhope in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:42 AM