Skip to content

Instantly share code, notes, and snippets.

@Xe
Created April 28, 2012 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xe/2516229 to your computer and use it in GitHub Desktop.
Save Xe/2516229 to your computer and use it in GitHub Desktop.
#include "grades.h"
/* a small main you will need to add to. You can't change my function calls */
int main()
{
FILE * fin = NULL;
int * quizzes, * assignments, * exams, * finals;
int quizTotal, assignTotal, examTotal, finalTotal;
char name[MAX];
do
{
fin = openFile();
fgets(name, MAX, fin);
assignments = makeAndFillArray(fin, &assignTotal);
quizzes = makeAndFillArray(fin, &quizTotal);
exams = makeAndFillArray(fin, &examTotal);
finals = makeAndFillArray(fin, &finalTotal);
double assgnAvg = average(assignTotal, assignments);
double quizAvg = average(quizTotal, quizzes);
double examAvg = average(examTotal, exams);
double finalAvg = average(finalTotal, finals);
//derp
printf("name: %s\n", name);
printf("Assignment Avg:\t\t%f\n", assgnAvg);
printf("Quiz Avg:\t\t%f\nExam Avg:\t\t%f\nFinal Avg:\t\t%f\n", quizAvg, examAvg, finalAvg);
double rawScore = weightedGrade(.4, assignTotal, assignments, 100) + weightedGrade(.1, quizTotal, quizzes, 25) + weightedGrade(.25, examTotal, exams, 100) + weightedGrade(.25, finalTotal, finals, 200);
printf("Raw Score:\t\t%f\n", rawScore);
printf("GPA:\t\t%3.2f\n", gpa(rawScore));
free(assignments);
free(quizzes);
free(exams);
free(finals);
}while(goAgain());
return 0;
}// end main
sam@rarity:~/Code/C_Unix/c_stuff/lab5$ ./lab5
fname> hugh.txt
name: Hugh Jelly
Assignment Avg: 73.428571
Quiz Avg: 23.750000
Exam Avg: 72.500000
Final Avg: 200.000000
Raw Score: 0.819964
GPA: 2.70
Again? (y/n): n
sam@rarity:~/Code/C_Unix/c_stuff/lab5$ ./lab5
fname> scores1.txt
name: Stu Steiner
Assignment Avg: 100.000000
Quiz Avg: 25.000000
Exam Avg: 100.000000
Final Avg: 200.000000
Raw Score: 1.000000
GPA: 4.00
Again? (y/n): n
sam@rarity:~/Code/C_Unix/c_stuff/lab5$ ./lab5
fname> rarity.txt
name: Rarity Belle
Assignment Avg: 49.142857
Quiz Avg: 23.750000
Exam Avg: 69.000000
Final Avg: 125.000000
Raw Score: 0.620321
GPA: 0.70
Again? (y/n): n
In file included from cscd240_s12_lab5.c:1:0:
grades.h:7:24: error: unknown type name ‘FILE’
grades.h:8:1: error: unknown type name ‘FILE’
cscd240_s12_lab5.c: In function ‘main’:
cscd240_s12_lab5.c:7:2: error: unknown type name ‘FILE’
cscd240_s12_lab5.c:7:15: error: ‘NULL’ undeclared (first use in this function)
cscd240_s12_lab5.c:7:15: note: each undeclared identifier is reported only once for each function it appears in
cscd240_s12_lab5.c:16:3: warning: incompatible implicit declaration of built-in function ‘fscanf’ [enabled by default]
cscd240_s12_lab5.c:18:15: warning: assignment makes pointer from integer without a cast [enabled by default]
cscd240_s12_lab5.c:19:11: warning: assignment makes pointer from integer without a cast [enabled by default]
cscd240_s12_lab5.c:20:9: warning: assignment makes pointer from integer without a cast [enabled by default]
cscd240_s12_lab5.c:21:10: warning: assignment makes pointer from integer without a cast [enabled by default]
In file included from grades.c:1:0:
grades.h:7:24: error: unknown type name ‘FILE’
grades.h:8:1: error: unknown type name ‘FILE’
grades.c:3:24: error: unknown type name ‘FILE’
grades.c:18:1: error: unknown type name ‘FILE’
grades.c: In function ‘openFile’:
grades.c:19:2: error: unknown type name ‘FILE’
grades.c:19:15: error: ‘NULL’ undeclared (first use in this function)
grades.c:19:15: note: each undeclared identifier is reported only once for each function it appears in
grades.c:23:3: warning: incompatible implicit declaration of built-in function ‘fprintf’ [enabled by default]
grades.c:23:11: error: ‘stderr’ undeclared (first use in this function)
grades.c:24:3: warning: incompatible implicit declaration of built-in function ‘fscanf’ [enabled by default]
grades.c:24:10: error: ‘stdin’ undeclared (first use in this function)
grades.c:25:7: warning: assignment makes pointer from integer without a cast [enabled by default]
grades.c: In function ‘goAgain’:
grades.c:38:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
grades.c:39:2: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
make: *** [all] Error 1
#include "grades.h"
int * makeAndFillArray(FILE * fin, int * count) {
int total;
fscanf(fin, "%i", &total);
*count = total;
//printf("%d\n", total);
int * ara = (int *) malloc(*count * sizeof(int));
//printf("%s\n", ara == NULL ? "YES" : "NO");
int i, * ptr = ara;
for(i = 0; i < *count; i++) {
fscanf(fin, "%d", ptr);
ptr++;
//printf("%i\n", ara[i]);
}
//ara -= count-1;
return ara;
}
FILE * openFile() {
FILE * fin = NULL;
do {
char fname[MAX];
fprintf(stderr, "fname> ");
fscanf(stdin, "%s", fname);
fin = fopen(fname, "r");
if(fin == NULL) {
fprintf(stderr, "name %s rejected, check permissions and retry\n", fname);
}
} while (fin == NULL);
return fin;
}
int goAgain() {
char userInput;
getchar();
//fflush(stdin);
printf("Again? (y/n): ");
scanf("%c", &userInput);
return userInput == 'y';
}
double average(int count, int * array) {
double val = 0.0;
int i = 0, * ptr = array;
for (i = 0; i < count; i++) {
val = val + *ptr;
ptr++;
}
val = val / count;
return val;
}
double weightedGrade(double weight, int count, int * ara, int max) {
int i, r = 0, * ptr = ara;
for (i = 0; i < count; i++) {
r += *ptr;
ptr++;
}
//typecast here just to make sure the types are correct
return ((double)r / (count * max)) * weight;
}
double gpa(double perc) {
if(perc > .949) {
return 4.0;
} else if (perc < .60) {
return 0.0;
} else {
return 3.9 - 10*(.94 - perc);
}
}
#ifndef GRADES_H
#define GRADES_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int * makeAndFillArray(FILE *, int *);
FILE * openFile();
int goAgain();
double average(int, int *);
double weightedGrade(double, int, int *, int);
double gpa(double);
#endif
Hugh Jelly
7
40
80
80
35
90
99
90
8
24
25
25
17
26
25
23
25
2
60
85
1
200
scores1.txt is a 4.0
hugh.txt is a 2.7
rarity.txt is a 0.7
all:
gcc -o lab5 *.c -g
Rarity Belle
7
40
0
80
35
0
99
90
8
24
25
25
17
26
25
23
25
2
60
78
1
125
Stu Steiner
3
100
100
100
4
25
25
25
25
3
100
100
100
1
200
Stu Steiner
3
100
100
100
4
25
25
25
25
3
100
100
100
2
100
100
sam@rarity:~/Code/C_Unix/c_stuff/lab5$ valgrind ./lab5
==28892== Memcheck, a memory error detector
==28892== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==28892== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==28892== Command: ./lab5
==28892==
fname> scores2.txt
name: Stu Steiner
Assignment Avg: 100.000000
Quiz Avg: 25.000000
Exam Avg: 100.000000
Final Avg: 100.000000
Raw Score: 0.875000
GPA: 3.25
Again? (y/n): n
==28892==
==28892== HEAP SUMMARY:
==28892== in use at exit: 568 bytes in 1 blocks
==28892== total heap usage: 5 allocs, 4 frees, 616 bytes allocated
==28892==
==28892== LEAK SUMMARY:
==28892== definitely lost: 0 bytes in 0 blocks
==28892== indirectly lost: 0 bytes in 0 blocks
==28892== possibly lost: 0 bytes in 0 blocks
==28892== still reachable: 568 bytes in 1 blocks
==28892== suppressed: 0 bytes in 0 blocks
==28892== Rerun with --leak-check=full to see details of leaked memory
==28892==
==28892== For counts of detected and suppressed errors, rerun with: -v
==28892== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment