Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will make the assumption that you only care about which round had the most goals in it and that you don't need to store your text file in memory like @Ben suggested.</p> <p>If that's the case the you can do something like this:</p> <pre><code>int i, maxGoals = 0, roundWithMostGoals = 0; for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &amp;runde, &amp;dag, &amp;month, &amp;year, &amp;clock, team1, team2, &amp;goal1, &amp;goal2, &amp;attendance) == 10; ++i) { if (maxGoals &lt; goal1 + goal2) { roundWithMostGoals = runde; maxGoals = goal1 + goal2; } } // Edit: printf("The largest number of goals was %d, scored in round %d", maxGoals, roundWithMostGoals); </code></pre> <p><strong>This code does have a problem</strong>. If there are two rounds with the largest number of goals only the first one will be printed.</p> <p>To avoid this we would need to loop through twice which is not ideal and I would recommend going with one of the other proposed approaches that loads all of this data into memory.</p> <p>However, here is a modified solution like the one above even though I don't think it is optimal:</p> <pre><code>int i, maxGoals = 0, roundWithMostGoals = 0; // Find the maximum number of goals that was scored in any one round. for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &amp;runde, &amp;dag, &amp;month, &amp;year, &amp;clock, team1, team2, &amp;goal1, &amp;goal2, &amp;attendance) == 10; ++i) { if (maxGoals &lt; goal1 + goal2) { maxGoals = goal1 + goal2; } } printf("The largest number of goals scored was %d.\n", maxGoals); printf("The largest number of goals was scored in\n"); // TODO: Reposition the file stream back to the beginning or close it and then reopen it again. // XXX Code Here XXX // Loop through again getting all the rounds with the maximum number of goals. for (i = 0; fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &amp;runde, &amp;dag, &amp;month, &amp;year, &amp;clock, team1, team2, &amp;goal1, &amp;goal2, &amp;attendance) == 10; ++i) { if (maxGoals == goal1 + goal2) { printf("\tRound %d\n", runde); } } </code></pre> <p>But this now loops through twice and is definitely not the best solution to your problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload