Note that there are some explanatory texts on larger screens.

plurals
  1. POarray of structs - size of array based on entries in file
    text
    copied!<p>Hmm not sure if the title of this makes any sense. I'm creating a program that reads information from the file marks.txt (This is homework by the way so please bear with me I'm still wandering in the dark a bit :) ) </p> <p>marks.txt:</p> <pre><code>U08006 3 30 40 30 12000001 55 42 60 12000002 37 45 40 12000003 58 0 24 12000004 74 67 80 12000005 61 50 38 12000006 70 45 58 99999999 </code></pre> <p>the first line is a module code followed by number of assignments and weighting of each assignment. The other lines are student numbers followed by marks for each assignment. And finally the end of the list is indicated by the student number 99999999.</p> <p><strong>EDIT: the program should eventually output the number of students and the average and standard deviation, for each assignment and overall module mark. Also I've been instructed to use an array of structures.</strong></p> <p>I have stored the module information in a struct (module), but when it comes to storing the student information I am really lost. I need an array of structs for the students but the problem is that I don't know how many students are in the file (well obviously I do right now but if I want my program to be able to read the file even though the number of students changes having a fixed size array isn't helpful).</p> <p>I was thinking maybe I could count the number of lines in the file between the first and the last. and then use malloc to allocate memory for the array? am I on the right track here. so something like student = malloc(sizeof(student???) * number of lines)</p> <p>this is really confusing me! dealing with pointers arrays structs and files all at once is really getting me all muddled up and this is due during my next class so can't ask my tutor for help. So any tips would be greatly appreciated.</p> <p>my code so far in all it's "work in progress"ness: <strong>EDIT: ok so I've pretty much finished the program i.e. it does what it is suppose to but I'm still struggling with the whole malloc thing. for my student struct I want an array of structs and I want to allocate the memory after I determine the number of students but I'm not sure how I'm suppose to do this?! this whole pointer to array of structs I'm not quite getting it so the following code doesn't work. (if I have an array of struct student of predetermined size everything is peachy)</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; int studentCount(); void getModuleDetails(FILE *fileptr); void getStudentDetails(FILE *fileptr, int classSize); void overallMarks(int classSize); void cleanUp(FILE *fileptr, int classSize); void printModuleStats(int classSize); struct ModuleSpec { char moduleCode[7]; int numOfAssign; /*number of assignments*/ int *assignWeighting; /*assignment weighting*/ } module; /*declare struct with variable name module*/ struct StudentMarks { int studentNumber; int *assignMark; //assignment mark int moduleMark; //overall mark for module }*student; /*******************************************************************************************/ int main() { int i; /*index*/ FILE *pFile; int studentCounter = studentCount(); //count number of students in class student = malloc(sizeof(student)*studentCounter); /*allocate memory for array of student structs*/ pFile = fopen("marks.txt", "r"); /*open file marks.txt for reading*/ if(pFile==NULL){ /*check if file was opened successfully*/ printf("File could not be opened!"); exit(EXIT_FAILURE); } getModuleDetails(pFile); //get module details from pFile and store in struct module getStudentDetails(pFile, studentCounter); //get student details from pFile and store in student array fo structs overallMarks(studentCounter); //calculate and print overall marks of students printModuleStats(studentCounter); cleanUp(pFile, studentCounter); return 0; } /*****************************************************************************************/ int studentCount(){ FILE *pFile; int temp; int studentCount = 0; pFile = fopen("marks.txt", "r"); /*open file marks.txt for reading*/ if(pFile==NULL){ /*if file can't be opened*/ printf("File could not be opened!"); exit(EXIT_FAILURE); } do{ temp = fgetc(pFile); if(temp == '\n'){ studentCount++; } }while(temp != EOF); studentCount -=2; /*subtract first and last lines to count only students*/ //printf("The number of students on this module is: %d\n", studentCount); fclose(pFile); return studentCount; } /*******************************************************************************************/ void getModuleDetails(FILE *fileptr){ int i; int sumWeighting = 0; fscanf(fileptr, "%s %d", module.moduleCode, &amp;module.numOfAssign); printf("Module code: %s\n", module.moduleCode); printf("Number of assignments: %d\n", module.numOfAssign); module.assignWeighting = malloc(module.numOfAssign * sizeof(int)); /*Allocate memory to hold assignment weightings*/ if (module.assignWeighting == NULL) { /*check if memory allocation was successful*/ printf("Memory allocation failed."); exit(EXIT_FAILURE); } /*get weighting for each assignment and store in module.assignWeighting*/ for(i=0;i&lt;module.numOfAssign;i++){ fscanf(fileptr, "%d", &amp;module.assignWeighting[i]); //printf("module %d %d\n", i+1, module.assignWeighting[i]); sumWeighting += module.assignWeighting[i]; } /*check if sum of weighting equals 100*/ if(sumWeighting != 100){ printf("Error: Sum of weighting = %d\n Expected sum: 100\n", sumWeighting); } } /*********************************************************************************************/ void getStudentDetails(FILE *fileptr, int classSize){ int i; int j; for(i=0;i&lt;classSize;i++){ student[i].assignMark = (int *)malloc(sizeof(int) * module.numOfAssign); if(student[i].assignMark == NULL) { //check if memory allocation was successful printf("Memory allocation failed."); exit(EXIT_FAILURE); } fscanf(fileptr, "%d", &amp;student[i].studentNumber); /*get student assignment marks*/ for(j=0;j&lt;module.numOfAssign;j++){ fscanf(fileptr, "%d", &amp;student[i].assignMark[j]); //printf("mark for assignment %d: %d\n", j+1, student[i].assignMark[j] ); /*check if mark is within range 0 to 100*/ if(student[i].assignMark[j]&lt;0 || student[i].assignMark[j]&gt;100){ printf("Error: Assignment mark is not within the range 0 to 100"); } } } } /************************************************************************************************/ void overallMarks(int classSize){ int i; int j; float temp; for(i=0;i&lt;classSize;i++){ printf("Overall mark for student %d: \n", student[i].studentNumber); for(j=0;j&lt;module.numOfAssign;j++){ temp += (float)(module.assignWeighting[j] * student[i].assignMark[j]) / 100; } student[i].moduleMark = temp + 0.5; /*add 0.5 for rounding as converting float to int rounds down*/ printf("%d%%", student[i].moduleMark); if(student[i].moduleMark&lt;25){ printf(" is a FAIL\n"); } else if(student[i].moduleMark&lt;40){ printf(" is a RESIT\n"); } else if(student[i].moduleMark&lt;55){ printf(" is a PASS\n"); } else if(student[i].moduleMark&lt;70){ printf(" is a MERIT\n"); } else if(student[i].moduleMark&lt;100){ printf(" is a DISTINCTION\n"); } temp = 0; } } /***********************************************************************************************/ void printModuleStats(int classSize){ int i; int j; float averageDevMarks; float stdDevMarks; float averageDevModule; float stdDevModule; printf("\nModule Statistics for %s\n", module.moduleCode); printf("\nNumber of students: %d\n", classSize); /*assignments*/ for(i=0;i&lt;module.numOfAssign;i++){ printf("\nAssignment %d:\n", i+1); for(j=0;j&lt;classSize;j++){ averageDevMarks += student[j].assignMark[i]; } averageDevMarks /= classSize; printf("Average deviation: %f\n", averageDevMarks); for(j=0;j&lt;classSize;j++){ stdDevMarks += (student[j].assignMark[i] - averageDevMarks)*(student[j].assignMark[i] - averageDevMarks); } stdDevMarks = sqrt(stdDevMarks/classSize); printf("Standard deviation: %f\n", stdDevMarks); stdDevMarks = 0; averageDevMarks = 0; } /*modules*/ for(i=0;i&lt;classSize;i++){ averageDevModule += student[i].moduleMark; } averageDevModule /= classSize; printf("\nAverage deviation for module mark: %f\n", averageDevModule); for(i=0;i&lt;classSize;i++){ stdDevModule += (student[i].moduleMark - averageDevModule)*(student[i].moduleMark - averageDevModule); } stdDevModule = sqrt(stdDevModule/classSize); printf("Standard deviation for module mark: %f\n", stdDevModule); } /************************************************************************************************/ void cleanUp(FILE *fileptr, int classSize){ int i; fclose(fileptr); /*close file*/ /*free previously allocated memory*/ free(student); free(module.assignWeighting); for(i=0;i&lt;classSize;i++){ free(student[i].assignMark); } } </code></pre>
 

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