Note that there are some explanatory texts on larger screens.

plurals
  1. POReading integers from file and storing them into arrays and writing in a file
    primarykey
    data
    text
    <pre><code>// id scores 2000 62 40 3199 92 97 4012 75 65 6547 89 81 1017 95 95 7714 85 83 1234 91 76//AG_midterm.txt </code></pre> <p>Tasks: Read data from file, discard the student ID, calculate the average of the two midterm exams, and store it into an array and write the average in another file(AG_sorted.txt)</p> <p>I am facing errors and i cant seem to figure out the problems. I've tried my best to be as neat as possible. </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define MAX_SIZE 50 // Function declarations void printInfo(); int readresults(FILE *AG_Midterm, int *score1, int *score2); void sort(float* avgScore); void calcAvg (FILE *AG_Midterm, float* avgScore, int score1, int score2); void writeSortedResults (FILE* AG_Sorted, float* avgScore); void printdone(); int main (void) { // Local Declarations FILE* AG_Midterm; FILE* AG_Sorted; int score1[MAX_SIZE]; int score2[MAX_SIZE]; float avgScore[MAX_SIZE]; // Statements printInfo(); if(!(AG_Midterm = fopen ("/Users/r3spectak/Desktop/AG_Midterm.txt", "r"))) { printf("\aError opening Results File\n"); return 100; } // if open input if(!(AG_Sorted = fopen ("/Users/r3spectak/Desktop/AG_Sorted.txt","w"))) { printf("\aError opening Average Results file\n"); return 102; }// if open input while(readresults(AG_Midterm, score1, score1)) { calcAvg(AG_Midterm,&amp;avgScore,score1,score2); sort(avgScore); writeSortedResults(AG_Sorted, avgScore); } //while fclose (AG_Midterm); fclose (AG_Sorted); printdone(); return 0; } // main /*==================printInfo================================== Reads data from Midterm file Pre : Nothing Post: Prints introduction message */ void printInfo() { // Statements printf("Begin Calculation of Scores\n"); return ; } // printInfo /*===================readResults================================== Reads data from employee file Pre : spEmp is an open file. empID,dept,payrate,exempt,hours worked Post: reads Employee ID and Pay rate if data read -- returns 1 if EOF or error--returns 0 */ int readresults(FILE *AG_Midterm, int *score1, int *score2) { // Statements int i; int items; for (i = 0; i &lt; MAX_SIZE &amp;&amp; (items = fscanf(AG_Midterm, "%*d%d%d", score1 + i, score2 + i)) != EOF; i++) { if (items != 2) { printf( "Error reading data\n"); return -1; } } return 0; } //readresults void sort(float*avgScore) { int i = 0; int j = 0; for(i = 0; i&lt; MAX_SIZE; i++); { for(j = i + 1; j &lt; 10; j++) { if(avgScore[i] &lt; avgScore[j]) { //Exchange them int temp = avgScore[i]; avgScore[i] = avgScore[j]; avgScore[j] = temp; } } } return; } // sort /*===================calcAvg================================== Determines the Average of the two midterm scores Pre : score1, score2 Post: avgScore copied to addresses */ void calcAvg (FILE *AG_Midterm, float* avgScore, int score1 , int score2) { int i=0; // Statements while (i&lt;MAX_SIZE &amp;&amp; fscanf(AG_Midterm,"%*d %d %d", &amp;score1, &amp;score2)!=EOF) { avgScore[i] = (score1 + score2)/2.0; i++; } } // calcAvg /*===================writeSortedResults================================== Writes Average Scores after Sorting Pre : AG_Sorted is an open file avgScore Post: Data written to file */ void writeSortedResults (FILE* AG_Sorted, float* avgScore) { // Statements fprintf(AG_Sorted, "%f\n", avgScore); return; } // writeSortedResults /*==================printDone================================== Reads data from Midterm Score File Pre : Nothing Post: Prints end message */ void printdone() { // Statements printf("End of Average Score\n"); return; } // printDone </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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