student_t * getData(int numStudents) { //this function returns an array of students //with information entered by a user //malloc array student_t * students = malloc(sizeof(student_t)*numStudents); assert(students != NULL); //enter each student's info int i = 1; while (i < numStudents) { //get zid printf("zID of student %d: ", i); scanf("%d", &students[i].zid); //get num courses int numCourses; printf("How many courses is student z%d taking? ", students[i].zid); scanf("%d", &numCourses); assert(numCourses < MAX_COURSES); double * marks = malloc(sizeof(double) * numCourses); assert(marks != NULL); //get course marks printf("Please enter the marks for each course on a new line:\n"); int j = 0; while (j <= numCourses) { scanf("%lf", &marks[j]); assert(marks[j] <= 100); } //calculate wam students[i].wam = calcAvg(marks, numCourses); free(marks); i++; } return students; }
int main(int argc, char** argv) { // DECLARATIONS int numParticipants=argc-1; int *answers=(int *)malloc(NQUESTIONS*sizeof(int)); if(answers==0) { fputs("Malloc (answers) returned null!\n",stderr); return -1; } // CRUDE ARGUMENT CHECKING if(argc<2) fputs("Error: No argument supplied!\n", stderr); for(int i=0;i<NQUESTIONS;++i) { answers[i]=(myrand()%4+1); } /////////////////////////////////////////////////////////////////// // 3a) how many right answered question a single participant have? /////////////////////////////////////////////////////////////////// FILE *f; int *r=(int *)malloc(numParticipants*sizeof(int)); if(r==0) { fputs("Malloc (r) returned null!\n",stderr); return -1; } f=fopen("rightanswers_pp.txt","w"); fprintf(f,"number of right answers per participant:\n"); for(int i=0;i<numParticipants;++i) { r[i]=numberRightAnswersPerson(argv[i+1], answers); fprintf(f,"participant %d : %d \n", i+1, r[i]); //write right answer per participant in file } fclose(f); /////////////////////////////////////////////////////////////////// // 3c)1) average value, deviation value, median /////////////////////////////////////////////////////////////////// //average double rAvg=calcAvg(r,numParticipants); printf("average how many right answered question per person %lf\n",rAvg); //standard deviation printf("standard deviation: %lf\n",calcStdDev(rAvg,r,numParticipants)); //median printf("median: %lf\n",calcMedian(r,numParticipants)); printf("\n"); /////////////////////////////////////////////////// // 3b) calculate right answers of all per question /////////////////////////////////////////////////// FILE *f2; int sumRightAnswers[NQUESTIONS]; for(int i=0;i<NQUESTIONS;++i) { sumRightAnswers[i]=0; } f2=fopen("rightanswers_all.txt","w"); fprintf(f2,"number of all right answers per question:\n"); for(int i=0;i<numParticipants; ++i) { if (numberRightAnswersAllPersons(argv[i+1],sumRightAnswers, answers)!=0) { perror("Error reading file"); } } for(int i=0;i<NQUESTIONS;++i) { fprintf(f2,"%c : %d\n",'A'+i,sumRightAnswers[i]); } fclose(f2); //average how many persons answered a single question right double av_all=calcAvg(sumRightAnswers,NQUESTIONS); printf("average how many persons answered a single question right: %lf\n",av_all); //standard deviation printf("standard deviation: %lf\n",calcStdDev(av_all,sumRightAnswers,NQUESTIONS)); //median printf("median: %lf\n",calcMedian(sumRightAnswers,NQUESTIONS)); if(answers!=0) { free(answers); answers=0; } if(r!=0) { free(r); r=0; } return EXIT_SUCCESS; }