[SOURCE]
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX 100
static int Compare(const void *element_1, const void *element_2)
{
int i = *(int *)element_1,
j = *(int *)element_2;
if ( i > j ) return (-1);
if ( i < j ) return (1);
return (0);
}
main()
{
int scores[MAX], input, i, j, k, n, count[5];
int sum, high, low, varsum;
float average, var;
puts("TEST STATISTICS:\n");
for(;;)
{
puts("\nEnter the test scores (-9 after the last score entered)");
i = 0;
do
{
if (i > MAX - 1)
{
puts("Array is full!\n");
break;
}
printf("Score #%i -> ", i+1);
scanf("%d", &input);
while(getchar() != '\n');
if(input != -9) scores = input;
if((input < 0 || input > 101) && (input != -9))
{
puts("Error: Data Must Be In Range of 0 to 100");
--i;
}
else if(input/ 101 != 0)
{
puts("Error: Data Must Be In Range And Not Decimal");
--i;
}
if(input == -9 && i == 0) break;
++i;
}
while(input != -9);
if(i > 0)
{
if(i< MAX -1) i = i - 2;
else --i;
for(j = 0; j <= 4; ++j) count[j] = 0;
sum = 0;
high = scores[0];
low = scores[0];
for(j = 0; j <= i; ++j)
{
sum = sum + scores[j];
if(scores[j] < low) low = scores[j];
if(scores[j] > high) high = scores[j];
if(scores[j] > 89) ++count[0];
else if(scores[j] > 79) ++count[1];
else if(scores[j] > 69) ++count[2];
else if(scores[j] > 59) ++count[3];
else ++count[4];
}
average = (float)sum/(i+1);
varsum = 0;
for(j = 0; j <= i; ++j)
varsum = varsum + (scores[j] - average) * (scores[j] - average);
var = varsum/i;
printf(" Average ---- %5.2f\n"
" High ------- %3i\n"
" Low ------- %3i\n"
" Variance --- %5.2f\n"
" Std. Dev --- %5.2f\n"
" Distribution:\n"
" (90 - 100) --- %3i\n"
" (80 - 89) --- %3i\n"
" (70 - 79) --- %3i\n"
" (60 - 69) --- %3i\n"
" ( 0 - 59) --- %3i\n"
"\n\n Bar Graph\n\n"
" (90 - 100) ",
average, high, low, var, sqrt(var),
count[0], count[1], count[2],
count[3], count[4]);
for(k = 1; k <= count[0]; ++k) putchar('*');
fputs("\n (80 - 89) ", stdout);
for(k = 1; k <= count[1]; ++k) putchar('*');
fputs("\n (70 - 79) ", stdout);
for(k = 1; k <= count[2]; ++k) putchar('*');
fputs("\n (60 - 69) ", stdout);
for(k = 1; k <= count[3]; ++k) putchar('*');
fputs("\n ( 0 - 59) ", stdout);
for(k = 1; k <= count[4]; ++k) putchar('*');
/* puts("\n Scores in Descending Sequence:\n");
n = sizeof(scores) / sizeof (int);
qsort(scores, n, sizeof(int), Compare);
for(k = 0; k <= i; ++k)
printf(" %2i %3i\n", k + 1, scores[k]); */
//PROBLEM AREA!
printf("\n Total Number of Tests -> %i", i+1);
}
else break;
}
return(0);
}
[/SOURCE]
I need some homework help...
Okay, I'm in an intro to C class...and I can't get one thing on my homework to work right. It has to do with the qsort function. I'm making a test score analyzer based upon arrays, and everything in my program works fine except for sorting the arrays into ascending order. Here's my code:
I put the issues in comments so it's easy to see. If you could tell me what I'm doing wrong, that would be nice. Though...I gotta get to class now...lol
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement