Percents are not showing. Help please.

So this is some of my program. The parts I am having trouble with. So I have a function below called int calcPercentage. It calculates a test score percentage and then is supposed to call it into another function called int displayResults. My percent's are not showing up though. Anyone see anything wrong?

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
208
209
210
211
#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 = 0; i < num; i++)
    {
        printf("\nScore for %s %d: ", k, i+1);
        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;
    float sum1;

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

    return sum1;
}

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

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

{
    int y, sum = 0;
    double perc;

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

//int readFinalScore()

//{
    //int x;

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

//}


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

{

    printf("\nPercentage per category:");
    printf("\n\nAssignments: %f", assignsPerc);
    printf("\n\nQuizzes: %.2f", quizzesPerc);
    printf("\n\nExams: %.2f", examsPerc);
    printf("\n\nFinal Exam: %.2f\n", finalPerc);
    printf("\n%s:\n\n", name);
    printf("Your weighted percentage is: %s ", );
    printf("Your Grade point is %d \n");
    printf("\nFor assignments the average score was %.0lf and the median score was %.0lf\n\n", assignsMean, assignsMedian);
    printf("For quizzes the average score was %.0f and the median score was %.0f\n", quizzesMean, quizzesMedian);
    printf("For exams the average score was %.0f and the median score was %.0f\n", examsMean, examsMedian);

}
Last edited on
It looks like you're either using the wrong type of variable in your displayResults() function, or you're using the wrong specifier for your printf() statements. For example should finalPerc be an int or a double?

Well I tried changing assignPerc to a double in my displayResults and it is still not showing up. I am pretty sure you use %f to display a double, am I wrong?
Honestly, I do not know how you are seeing any results from this code. There is not even a main function!
Your displayResults functions uses int instead of double.
I think that both of your functions outside of main should have a void return type, because they do not return anything. From about line 8 to line 51 you need to wrap that code inside an int main() { /*code goes here*/ }
Also you will need to declare function prototypes before main, or you can move both of your functions before main.
There may be other issues that I did not see after my quick glance. Let us know what other errors you encounter after fix those.
Its actually just bitsc and pieces of the whole program. I can post the whole thing if you would like. I just posted the stuff I was having problems with. I will change the functions to void to see if that helps. I'm trying to call the percents out of the percent function into the display function.
I have updated my code with the changes to void and doubles. But still uncertain why my percent's don't show.
I'd say fixing your compile errors and warnings would be a good place to start.

||=== c_homework, Debug ===|
main.c||In function ‘main’:|
main.c|14|warning: variable length array ‘name’ is used [-Wvla]|
main.c|15|warning: variable length array ‘assigns’ is used [-Wvla]|
main.c|15|warning: variable length array ‘quizzes’ is used [-Wvla]|
main.c|15|warning: variable length array ‘exams’ is used [-Wvla]|
main.c|22|error: implicit declaration of function ‘readName’ [-Wimplicit-function-declaration]|
main.c|24|error: implicit declaration of function ‘fillArray’ [-Wimplicit-function-declaration]|
main.c|25|error: implicit declaration of function ‘selectionSort’ [-Wimplicit-function-declaration]|
main.c|26|error: implicit declaration of function ‘findMedian’ [-Wimplicit-function-declaration]|
main.c|27|error: implicit declaration of function ‘findMean’ [-Wimplicit-function-declaration]|
main.c|28|error: implicit declaration of function ‘calcPercentage’ [-Wimplicit-function-declaration]|
main.c|51|error: implicit declaration of function ‘displayResults’ [-Wimplicit-function-declaration]|
main.c|56|error: implicit declaration of function ‘goAgain’ [-Wimplicit-function-declaration]|
main.c|15|warning: unused variable ‘finalExam’ [-Wunused-variable]|
main.c|63|warning: no previous declaration for ‘readName’ [-Wmissing-declarations]|
main.c||In function ‘readName’:|
main.c|67|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|72|warning: no previous declaration for ‘fillArray’ [-Wmissing-declarations]|
main.c||In function ‘fillArray’:|
main.c|79|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
main.c|86|warning: no previous declaration for ‘goAgain’ [-Wmissing-declarations]|
main.c|106|warning: no previous declaration for ‘selectionSort’ [-Wmissing-declarations]|
main.c|126|warning: no previous declaration for ‘findMedian’ [-Wmissing-declarations]|
main.c||In function ‘findMedian’:|
main.c|130|warning: unused variable ‘median2’ [-Wunused-variable]|
main.c|129|warning: unused variable ‘i’ [-Wunused-variable]|
main.c|149|warning: no previous declaration for ‘findMean’ [-Wmissing-declarations]|
main.c|166|warning: no previous declaration for ‘calcPercentage’ [-Wmissing-declarations]|
main.c|166|error: conflicting types for ‘calcPercentage’|
main.c|28|note: previous implicit declaration of ‘calcPercentage’ was here|
main.c|192|warning: no previous declaration for ‘displayResults’ [-Wmissing-declarations]|
main.c|192|error: conflicting types for ‘displayResults’|
main.c|51|note: previous implicit declaration of ‘displayResults’ was here|
main.c||In function ‘displayResults’:|
main.c|204|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘double’ [-Wformat=]|
main.c|205|error: expected expression before ‘)’ token|
main.c|206|warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]|
main.c|192|warning: unused parameter ‘classPerc’ [-Wunused-parameter]|
main.c|192|warning: unused parameter ‘classGrade’ [-Wunused-parameter]|
main.c||In function ‘readName’:|
main.c|68|warning: control reaches end of non-void function [-Wreturn-type]|
main.c||In function ‘fillArray’:|
main.c|82|warning: control reaches end of non-void function [-Wreturn-type]|
main.c||In function ‘selectionSort’:|
main.c|122|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 11 errors, 24 warnings (0 minutes, 1 seconds) ===|


Also please don't "update" your code, add a new post and paste your modification in that new paste. That way it'll be easier to see what changes you've made and it'll be easier to follow the topic.

Topic archived. No new replies allowed.