Hello there everyone. I am having a bit of trouble with my C programming. I do not know anything about C programming and this is my first semester and already my professor is putting pressure on me. Anyone that can help would be appreciated.

So I am supposed to create a program that reads from a file with the numbers:
98 96 14 95 88 64 37 72 78 120 86 73 84 73 104 66 92 19 63 45

After that I am supposed to calculate all the grades for each one like A, A+, A-, B, B+, B- C, C+, C-, D, D+, D-, F, all invalid grades less than 21 and more than 100 are considered invalid

So at the end of my code it should print out:
The number of B grades is: 3
The numerical average for a B grade is : 86
The number of B+ grades is: 2
The number of B- grades is: 1

So I am having trouble on how to read all the A grades and do average, all the B grades and do average, and so on. My professor didn't teach us that yet, so I am very confused. Its easy with scanf, but from the file, I don't know. If anyone could help would be so glad. Thanks in advance.

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

int main()
{
	int countA = 0;
	int gradeA = 0;
	int gradeAplus = 0;
	int gradeAminus = 0;

	int countB = 0;
	int gradeB = 0;
	int gradeBplus = 0;
	int gradeBminus = 0;

	int countC = 0;
	int gradeC = 0;
	int gradeCplus = 0;
	int gradeCminus = 0;

	int countD = 0;
	int gradeD = 0;
	int gradeDplus = 0;
	int gradeDminus = 0;

	int countF = 0;

	int invalidcount = 0;
	
	int numA = 0;

	FILE* infile;

	infile = fopen("lab2_data.txt", "r");

	while(!feof (infile) )
	{
		fscanf(infile, "%d", &numA);
		{
			if (numA > 100)
			{
			printf("The number grade %d is invalid\n", numA);
			invalidcount = invalidcount + 1;
			}
			
			else if (numA >= 96)
			{
			printf("The number grade %d is a A\n", numA);
			countA = countA + 1;
			gradeAplus = gradeAplus + 1;

			}

			else if (numA >= 91)
			{
			printf("The number grade %d is a A\n", numA);
			countA = countA + 1;
			gradeAminus = gradeAminus + 1;
			}

			else if (numA >= 86)
			{
			printf("The number grade %d is a B\n", numA);
			countB = countB + 1;
			gradeBplus = gradeBplus + 1;
			}

			else if (numA >= 81)
			{
			printf("The number grade %d is a B\n", numA);
			countB = countB + 1;
			gradeBminus = gradeBminus + 1;
			}

			else if (numA >= 76)
			{
			printf("The number grade %d is a C\n", numA);
			countC = countC + 1;
			gradeCplus = gradeCplus + 1;
			}

			else if (numA >= 71)
			{
			printf("The number grade %d is a C\n", numA);
			countC = countC + 1;
			gradeCminus = gradeCminus + 1;
			}

			else if (numA >= 66)
			{
			printf("The number grade %d is a D\n", numA);
			countD = countD + 1;
			gradeDplus = gradeDplus + 1;
			}

			else if (numA >= 61)
			{
			printf("The number grade %d is a D\n", numA);
			countD = countD + 1;
			gradeDminus = gradeDminus + 1;
			}

			else if (numA >= 21)
			{
			printf("The number grade %d is a F\n", numA);
			countF = countF + 1;
			}

			else if (numA < 21)
			{
			printf("The number grade %d is invalid\n", numA);
			invalidcount = invalidcount + 1;
			}
		}
	}

	printf("\nThe number of A grades is: %d\n", countA); 
	printf("The number of A+ grades is: %d\n", gradeAplus); 
	printf("The number of A- grades is: %d\n", gradeAminus); 

	printf("\nThe number of B grades is: %d\n", countB); 
	printf("The number of B+ grades is: %d\n", gradeBplus); 
	printf("The number of B- grades is: %d\n", gradeBminus); 

	printf("\nThe number of C grades is: %d\n", countC); 
	printf("The number of C+ grades is: %d\n", gradeCplus); 
	printf("The number of C- grades is: %d\n", gradeCminus); 

	printf("\nThe number of D grades is: %d\n", countD); 
	printf("The number of D+ grades is: %d\n", gradeDplus); 
	printf("The number of D- grades is: %d\n", gradeDminus); 

	printf("\nThe number of F grades is: %d\n", countF); 

	printf("\nThe number of invalid grades is: %d\n", invalidcount); 
	fclose(infile);
	
	return(0);
}

You just missed the code-tag on your post :)

OK, what you could do is add another variable to each of the grade groups that you have (A, B, C etc) that would store the average as you go along. So every time you add one to count, update the average as well.

A normal way to handle this kind of thing would be with structures and/or arrays, but you might not have come to those in your course yet.

Thanks for the reply. But I am still confused. I know that I have to have an int average, but like lets say the two numbers 88 and 86 are being read from the file, how do I tell the program that to save these numbers and that they are letter B grades. Could you like explain the concept to me on how this works.

So do I have like

averageA = averageA + numA
averageB = averageB + numA
averageC = averageC + numA

Before you concern yourself with averages I would get your while loop working, as it is you read the last grade twice.

Try changing your loop condition to something like...

while( fscanf(infile, "%d", &numA) != EOF)

Well, you already have a bunch of if... else statements that count up the number of each grade. If you take one of them, say

else if (numA >= 91){
    printf("The number grade %d is a A\n", numA);
    countA = countA + 1;
    gradeAminus = gradeAminus + 1;
}

and change it to

else if (numA >= 91){
    printf("The number grade %d is a A\n", numA);
    countA = countA + 1;
    gradeAminus = gradeAminus + 1;
    averageA += numA;    // update average here
}

Then after you've processed all the grades, you just need something like

averageA /= countA;

and you're done. Does that make sense?

Thank you very much everyone especiall ravenous. I was doing
averageA = averageA / numA. I dont know what I was thinking. I felt soo stupid. But ravenous, after you showed me that averageA = averageA / countA; I was like dang, that was so silly of me. But I am very thankful. Here is my final code

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

int main()
{
	int countA = 0;
	int gradeA = 0;
	int gradeAplus = 0;
	int gradeAminus = 0;
	int totalA = 0;
	int averageA = 0;

	int countB = 0;
	int gradeB = 0;
	int gradeBplus = 0;
	int gradeBminus = 0;
	int totalB = 0;
	int averageB = 0;

	int countC = 0;
	int gradeC = 0;
	int gradeCplus = 0;
	int gradeCminus = 0;
	int totalC = 0;
	int averageC = 0;

	int countD = 0;
	int gradeD = 0;
	int gradeDplus = 0;
	int gradeDminus = 0;
	int totalD = 0;
	int averageD = 0;

	int countF = 0;
	int totalF = 0;
	int averageF = 0;

	int invalidcount = 0;
	
	int numA = 0;

	FILE* infile;

	infile = fopen("lab2_data.txt", "r");

	while(!feof (infile) )
	{
		fscanf(infile, "%d", &numA);
		{
			if (numA > 100)
			{
			printf("The number grade %d is invalid\n", numA);
			invalidcount = invalidcount + 1;
			}

			else if (numA >= 96)
			{
			printf("The number grade %d is a A\n", numA);
			countA = countA + 1;
			gradeAplus = gradeAplus + 1;
			totalA = totalA + numA;
			}

			else if (numA >= 91)
			{
			printf("The number grade %d is a A\n", numA);
			countA = countA + 1;
			gradeAminus = gradeAminus + 1;
			totalA = totalA + numA;
			}

			else if (numA >= 86)
			{
			printf("The number grade %d is a B\n", numA);
			countB = countB + 1;
			gradeBplus = gradeBplus + 1;
			totalB = totalB + numA;
			}

			else if (numA >= 81)
			{
			printf("The number grade %d is a B\n", numA);
			countB = countB + 1;
			gradeBminus = gradeBminus + 1;
			totalB = totalB + numA;
			}

			else if (numA >= 76)
			{
			printf("The number grade %d is a C\n", numA);
			countC = countC + 1;
			gradeCplus = gradeCplus + 1;
			totalC = totalC + numA;
			}

			else if (numA >= 71)
			{
			printf("The number grade %d is a C\n", numA);
			countC = countC + 1;
			gradeCminus = gradeCminus + 1;
			totalC = totalC + numA;
			}

			else if (numA >= 66)
			{
			printf("The number grade %d is a D\n", numA);
			countD = countD + 1;
			gradeDplus = gradeDplus + 1;
			totalD = totalD + numA;
			}

			else if (numA >= 61)
			{
			printf("The number grade %d is a D\n", numA);
			countD = countD + 1;
			gradeDminus = gradeDminus + 1;
			totalD = totalD + numA;
			}

			else if (numA >= 21)
			{
			printf("The number grade %d is a F\n", numA);
			countF = countF + 1;
			totalF = totalF + numA;
			}

			else if (numA < 21)
			{
			printf("The number grade %d is invalid\n", numA);
			invalidcount = invalidcount + 1;
			}
		}
	}

	averageA = totalA / countA;
	averageB = totalB / countB;
	averageC = totalC / countC;
	averageD = totalD / countD;
	averageF = totalF / countF;

	printf("\nThe number of A grades is: %d\n", countA); 
	printf("The numerical average for a A grade is: %d\n", averageA); 
	printf("The number of A+ grades is: %d\n", gradeAplus); 
	printf("The number of A- grades is: %d\n", gradeAminus); 

	printf("\nThe number of B grades is: %d\n", countB); 
	printf("The numerical average for a B grade is: %d\n", averageB); 
	printf("The number of B+ grades is: %d\n", gradeBplus); 
	printf("The number of B- grades is: %d\n", gradeBminus); 

	printf("\nThe number of C grades is: %d\n", countC); 
	printf("The numerical average for a C grade is: %d\n", averageC); 
	printf("The number of C+ grades is: %d\n", gradeCplus); 
	printf("The number of C- grades is: %d\n", gradeCminus); 

	printf("\nThe number of D grades is: %d\n", countD); 
	printf("The numerical average for a D grade is: %d\n", averageD); 
	printf("The number of D+ grades is: %d\n", gradeDplus); 
	printf("The number of D- grades is: %d\n", gradeDminus); 

	printf("\nThe number of F grades is: %d\n", countF); 
	printf("The numerical average for a F grade is: %d\n", averageF); 

	printf("\nThe number of invalid grades is: %d\n", invalidcount); 
	fclose(infile);
	
	return(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.