Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I store information into an array using file input?(In C) For example: I

ID: 3864369 • Letter: H

Question

How do I store information into an array using file input?(In C)

For example:

I have this set of numbers:

10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165 6
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48


The first set of numbers need to be stored in a variable called "id". The second set of character need to be stored in a variable called "category". The next 5 numbers need to be stored together in a variable called combat_scores.

This is my code:

    //Read in number of sets
    fscanf(ifp,"%d", &n);

        //Read ID and category
        for (i=0; i<n; i++) {
        fscanf(ifp, "%d %c", &id[i], &category[i]);

       //Read 5 combat_scores
            for (j=0; j<5; j++) {
            fscanf(ifp, "%d", &combat_scores[j]);

            printf("%d %c %d", id[i], category[i], combat_scores[j]);
          
            }

        }

Explanation / Answer

CODE:

//Read in number of sets
fscanf(ifp,"%d", &n);
//Read ID and category
for (i=0; i<n; i++)
       {
       for(j=0;j<2;j++)
fscanf(ifp, "%d %c", &id[i], &category[i]);
       }
//Read 5 combat_scores
   for(i=0;i<n;i++)
   {
for (j=2; j<7; j++)
           {
fscanf(ifp, "%d", &combat_scores[j]);
printf("%d %c %d", id[i], category[i], combat_scores[j]);
  
}
       }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote