//======================================================
//this is the grading sorting program for part one
//======================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10

int main()
{
    FILE *ifp; // file pointer, points to the file
	char filename[32]; // store file name

	int ID [SIZE],grade[SIZE];

	int i;

	int sum_grades = 0;

	 //opening file by asking the user for the file name

    /*
     *printf("Enter the name of the file containing the grades\n");
	 *scanf("%s",filename);
	 *printf("%s", filename);
     */

	ifp = fopen("grades.txt", "r");

    //========================================
    // checkes to see if the file is valid
    //========================================
	if (ifp == NULL)
	{
	    printf("unable to locate file... FILE NOT FOUND\n");
	}

    //=============================================================
	//read in data into the arrays
	//this prints out the grades the way they appear in the program
	//=============================================================

    printf("=====================\n");
    printf("ID   Grade\n");
    printf("=====================\n");
    for(i = 0; i < SIZE; i++)
        {
            fscanf(ifp,"%d", &ID[i]);
            fscanf(ifp,"%d", &grade[i]);

            printf("%d %d\n", ID[i], grade[i]);

            if(ID[i]==0 && grade[i]==0)
            {
                break;
            }
        }

    //===============================================================
    //finding the highest grade and the corresponding ID in the class
    //===============================================================
    int highest = grade[0];

    for(i = 1; i< SIZE; i++)
    {
        if(grade[i]>highest)
        {
            highest = grade[i];
        }
    }

         printf("Top grade: %d by student with ID 3050\n", highest);


    //================================================================
    // calculating the mean grade
    //================================================================
    int mean = grade[0];
    for( i = 0; i < SIZE; i++)
    {
        if(ID[i]==0 && grade[i]==0)
            sum_grades += grade[i];
    }
    printf("sum is %d\n", sum_grades);

    fclose(ifp);
	return 0;
}

its a program designed to print out the student's ID's and grades as they appear in the file, print the highest grade, the mean value and print the student's ID's in increasing order

INPUT FILE
------------
3050 76
2030 60
1600 70
2222 50
2430 60
2800 50
0 0
Ball Adam 70
Arnold Joshua 67
Scot Michael 80
Cook Bret 55
Davis Ronald 62
Fox Brian 87
Singh Jaadu 91
Hudson Jeff 45
xxx xxx 0
Fox Brian
Jessi Mala
-------------------
1st part of the program its supposed to stop at the value ("0 0"). so it should print
3050 76
2030 60
1600 70
2222 50
2430 60
2800 50
but ("0 0") is being included, which affects my mean value as well. also how how do u implement the sorting algorithm for printing the ID's in increasing order which correspond to the grades

help would be appreciated

Lines 66 and 81, you're examining the WHOLE array, not the number of valid entries entered.

If you had a separate variable which you increment ONLY when the data is valid, you would know when to stop.

Lines 66 and 81, you're examining the WHOLE array, not the number of valid entries entered.

If you had a separate variable which you increment ONLY when the data is valid, you would know when to stop.

explain im not following what u are saying

for ( i = 0 ; i < N ; i++ ) {
  // read data
  // if condition, break
}

If N is 10, and you exit after 2 entries, how much data should you read?

for ( i = 0 ; i < N ; i++ )
// or
for ( i = 0 ; i < 2 ; i++ )

When you understand that the 2nd answer is right, replace the 2 with a variable.

This article has been dead for over six months. Start a new discussion instead.