How would I code this in C (NOT C++) I need to read a file of data that looks li
ID: 3692679 • Letter: H
Question
How would I code this in C (NOT C++)
I need to read a file of data that looks like this:
And store the data in an array of structures for Name, ID(500000-899999), score1, score2, score3, score4, score5, grade BUT the program also needs to take the five scores and average them and store them in the same structure as a float. The part where I have to take the scores and average them and store them in the same structure is what has me stuck.
This is my code for reading the data into the array but i dont know how to add the part that averages the scores and stores the new numbers into the array:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define LINE_MAX 200
#define FILE_MAX 100
#define ARRAY_MAX 45
#define NAME_LEN 25
#define ID_LEN 6
#define TEST_LEN 3
#define TEST2_LEN 3
#define PROJECT_LEN 3
#define PROJECT2_LEN 3
#define PROJECT3_LEN 3
#define AVERAGE_LEN 6
#define GRADE_LEN 2
typedef struct
{
char NAME[NAME_LEN+1];
int ID;
int TEST;
int TEST2;
int PROJECT;
int PROJECT2;
int PROJECT3;
float AVERAGE;
char GRADE[GRADE_LEN+1];
} STUREC;
int readRECS(STUREC records[], int maxRECS, FILE* fp);
int main(void)
{
char file_name[FILE_MAX];
FILE* fp;
int count=0;
int i;
int rc;
STUREC records[ARRAY_MAX];
printf(" COP 2220-51091 Project 5: Brent Reber ");
printf("Enter the file name: ");
rc = scanf("%s", file_name);
fp = fopen(file_name, "r");
if (fp == 0)
{
printf(" Error: Could not open file %s for read. ", file_name);
exit(1);
}
if (fp == NULL)
{
printf(" Error: No file name entered. ");
exit(2);
}
count = readRECS(records, ARRAY_MAX, fp);
if( count == 0)
{
printf(" There were no valid records found in file %s ", file_name);
exit(3);
}
return(0);
}
int readRECS(STUREC records[], int maxRECS, FILE* fp)
{
char line[LINE_MAX];
int rc;
int count=0;
while(fgets(line, LINE_MAX, fp) != NULL)
{
rc = sscanf(line,"%25[^:]%*c%6d*[^:]%3d*[^:]%3d*[^:]%3d*[^:]%3d*[^:]%3d*[^:]%2[^:]%*c",
records[count].NAME, &records[count].ID, &records[count].TEST,
&records[count].TEST2, &records[count].PROJECT, &records[count].PROJECT2,
&records[count].PROJECT3, records[count].GRADE);
if ((rc != 8) || records[count].ID < 500000 || records[count].ID > 899999 ||
records[count].TEST < 0 || records[count].TEST > 110 || records[count].TEST2 < 0 ||
records[count].TEST2 > 110 || records[count].PROJECT < 0 ||
records[count].PROJECT > 110 || records[count].PROJECT2 < 0 ||
records[count].PROJECT2 > 110 || records[count].PROJECT3 < 0 ||
records[count].PROJECT3 >110)
{
printf(" Invalid Record: %s", line);
}
else
{
count++;
if(count == ARRAY_MAX)break;
}
}
}
Explanation / Answer
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.