Help with calculation

So I am writing a code that will take someone's test, quiz and assignment scores and calculate each percentage. My first calculation for the assignments seems to work but then the quizzes and exams follow but display some large number. Not sure where I have gone wrong and have ben investing a lot of time. If anyone can take a look and see anything. It would be much appreciated. I cannot change the main part of the function. Everything works through separate functions.

Thanks Michael

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <stdlib.h>

/* NOTE you can't change my main */

const int MAX = 100;
const int ASSIGN_TOTAL = 7, QUIZ_TOTAL = 3, EXAMS_TOTAL = 2;
const int ASSIGN_MAX = 100, QUIZ_MAX = 25, EXAMS_MAX = 100, FINAL_MAX = 100;

// Prototypes

int main()
{
    char name[MAX];
    int assigns[ASSIGN_TOTAL], quizzes[QUIZ_TOTAL], exams[EXAMS_TOTAL], finalExam;
    double assignsMedian, assignsMean, assignsPerc;
    double quizzesMedian, quizzesMean, quizzesPerc;
    double examsMedian, examsMean, examsPerc, finalPerc, classPerc, classGrade;

    do
    {
        readName(name);

        fillArray(assigns, ASSIGN_TOTAL, "assignments");
        selectionSort(assigns, ASSIGN_TOTAL);
        assignsMedian = findMedian(assigns, ASSIGN_TOTAL);
        assignsMean = findMean(assigns, ASSIGN_TOTAL);
        assignsPerc = calcPercentage(assigns, ASSIGN_TOTAL, ASSIGN_MAX);


        fillArray(quizzes, QUIZ_TOTAL, "quizzes");
        selectionSort(quizzes, QUIZ_TOTAL);
        quizzesMedian = findMedian(quizzes, QUIZ_TOTAL);
        quizzesMean = findMean(quizzes, QUIZ_TOTAL);
        quizzesPerc = calcPercentage(quizzes, QUIZ_TOTAL, QUIZ_MAX);


        fillArray(exams, EXAMS_TOTAL, "exams");
        selectionSort(exams, EXAMS_TOTAL);
        examsMedian = findMedian(exams, EXAMS_TOTAL);
        examsMean = findMean(exams, EXAMS_TOTAL);
        examsPerc = calcPercentage(exams, EXAMS_TOTAL, EXAMS_MAX);


        //finalExam = readFinalScore();
        //finalPerc = calcFinalPercentage(finalExam, FINAL_MAX);

        classPerc = calcClassPerc(assignsPerc, quizzesPerc, examsPerc, finalPerc);
        //classGrade = calcGrade(assignsPerc, quizzesPerc, examsPerc, finalPerc);

        //displayResults(name, classPerc, classGrade,
                       //assignsPerc, quizzesPerc, examsPerc, finalPerc,
                       //assignsMean, quizzesMean, examsMean,
                       //assignsMedian, quizzesMedian, examsMedian);

    }while(goAgain());

    return 0;
}
//**************************************************************************************//

int readName(int s)

{
    printf("\nName: ");
    scanf(" %s", s);
}

//**************************************************************************************//

int fillArray(int x[], int num, int k)

{
    int i;

    for(i = 1; i <= num; i++)
    {
        printf("\nScore for %s %d: ", k, i);
        scanf(" %d", &x[i]);
    }
}

//**************************************************************************************//

int goAgain()

{
    char answer;

    printf("\nWould you like to calculate another set of scores (Y/N) ");
    scanf(" %c", &answer);

    if(answer != 'n' && answer != 'N')
       {
           return answer;
       }
    else
        {
            return 0;
        }
}

//**************************************************************************************//

int selectionSort(int myScore[], int total)

{  int  lim,
        hole;

   for ( lim = 1 ; lim < total ; lim++ )      //Credit Tim Rolfe
   {  int save = myScore[lim];              //Lecture 02-12-2015 Chapter 23 Sorting Arrays

      for ( hole = lim ;

            hole > 0 && save < myScore[hole-1] ;
            hole-- )
      {  myScore[hole] = myScore[hole-1];  }

      myScore[hole] = save;
   }
}

//**************************************************************************************//

int findMedian(int myPapery[], int num)

{
    int middle1, middle2, i;
    float median, median2;

    middle1 = num / 2;
    middle2 = (num/2)-1;

    if ((num % 2) == 0)
    {
        median = (myPapery[middle1] + myPapery[(middle2)]);
        median = median /2;
    }
    else
    {
        median = myPapery[middle1 + 0] / 1;
    }
return median;
}

//**************************************************************************************//

int findMean(int myPaperx[], int num)

{
    int j, sum, sum1;

    for(sum = j = 0; j < num; j++)
    {
        sum += myPaperx[j];
        sum1 = sum / num;
    }

    return sum1;
}

//**************************************************************************************//

int calcPercentage(int papers[], int total, int max)

{
    int y, sum = 0, sum1 = 0, perc = 0;

    sum1 = (total * max);
    printf("TOTAL: %d\n", sum1);
    for(sum = y = 0; y <= total; y++)
    {
        sum += papers[y];
    }
    perc = ((float)sum/sum1)*100;
    printf(" SUM: %d\n", sum);
    printf(" PERC: %d\n", perc);

    return perc;

}

//int readFinalScore()

//{
    //int x;

    //printf("\nScore for Final Exam: ");
    //scanf(" %d\n", &x);

//}


int calcClassPerc(int assignsPerc, int quizzesPerc, int examsPerc, int finalPerc)

{
    printf("\nPercentage per category:");

    printf("\n\nAssignments: %.2f", assignsPerc);

    printf("\n\nQuizzes: %.2f", quizzesPerc);

    printf("\n\nExams: %.2f", examsPerc);

    printf("\n\nFinal Exam: %.0f\n", finalPerc);

}
Last edited on
closed account (D80DSL3A)
Boy, that's a bit of code to sift through.
I see one problem on line 76.
Array index out of range:
for(i = 1; i <= num; i++)
That should probably be:
for(i = 0; i < num; i++)

See if that maybe fixes the problem.
Actually if I change that then it will only ask for 6 assignments not 7. I think my problem is somewhere in here. Then when this gets called to the top it should got to the int calcClassPerc function but doesn't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int calcPercentage(int papers[], int total, int max)

{
    int y, sum = 0, sum1 = 0, perc = 0;

    sum1 = (total * max);
    printf("TOTAL: %d\n", sum1);
    for(sum = y = 0; y <= total; y++)
    {
        sum += papers[y];
    }
    perc = ((float)sum/sum1)*100;
    printf(" SUM: %d\n", sum);
    printf(" PERC: %d\n", perc);

    return perc;

}
Last edited on
Sorry I read your edit wrong. That fixed my percentage, thank you very much. Now I just need to figure out why my percent's are pulling into my int calcClassPerc function.
closed account (D80DSL3A)
I see you've gone one past the array bounds in calcPercentage.
Line 8 above for(sum = y = 0; y <= total; y++)
should be for(sum = y = 0; y < total; y++)

The last parameter passed to calcClassPerc (line 48 your 1st post) is finalPerc, which isn't assigned a value anywhere (not even initially), though I see commented out code on line 46 which would assign a value.
Topic archived. No new replies allowed.