Link to home
Start Free TrialLog in
Avatar of quco74
quco74

asked on

Problem: I have 2 independed for-loops conflicting!!

Hello my friends,

Here I am again... with another question...
The input for this program is a set of integer scores for a class of students. My program is supposed to read integer scores from a file redirected as input to the program and then display the scores, and the ranking of each student compared to others in the class.

The following file is being used as input to test the program. The output for each set of data is shown below the listing of the file.
This are the numbers in the file:
> cat scores2
100 43 88 83 78 55 78 100
93 82 78 82 72 81
>

The program should produce the following output if executed as shown below.

> gcc p5.c
> a.out < scores2

student score ranking

   1     100     1    
   2      43    14    
   3      88     4    
   4      83     5    
   5      78     9    
   6      55    13    
   7      78     9    
   8     100     1    
   9      93     3    
  10      82     6    
  11      78     9    
  12      82     6    
  13      72    12    
  14      81     8    

Class Average: 79.50

(The symbol > is the Unix prompt.)

I assume there will be no more than 50 scores to be processed as input by the program and that the scores are in the range [0, 100]

The ranking of each score is one more than a count of the number of students who scored HIGHER than that score.  For example: In the score2 file there were two scores of 100.  No student scored higher than 100.  Therefore, the 100 score ranking is 0 + 1 = 1. In the score2 file there were two scores higher than 93. Therefore, the ranking of the 93 score is 2 + 1 = 3.

P R O B L E M :

THE FOLLOWING CODE SHOW WHAT I HAVE DONE AT THIS POINT, THE PROGRAM IS NOT READY, BUT YOU CAN SEE WHAT KIND OF OUTPUT IS GETTING! I'M KIND OF FRUSTRATED BECAUSE I DON'T KNOW WHAT IS NOT WORKING! I SUSPECT THE PROBLEM HAS TO DO WITH THE 2 FOR-LOOPS DEFINED BELOW. PLEASE HELP!

Sincerely,

Eugenio Palomo
Gainesville, FL

=====================================================================
=====================================================================

#include <stdio.h>
#define N 50

main()
{
                           
system("clear");            

// VARIABLE DECLARATIONS HERE
int data, scores[N]={0}, ranks[N]={0};
int i, j, k, sum, cnt=0;
float class_average;

// WHILE LOOP OPENS TO RETRIVE DATA INTO SCORES[] ARRAY
while (scanf("%d", &data) == 1) {
   scores[cnt] = data;
   cnt++;
}

// FOR-LOOP TO CALCULATE THE AVERAGE OF THE NUMBER OF SCORES RETRIVED
for (i=0; i<=49; i++)
{
   sum += scores[i];
   class_average = (float) sum/cnt;
}

// FOR-LOOP TO CALCULATE THE RANKING AND ASSIGN IT INTO THE SECOND ARRAY
for (j=0; j<cnt; j++)
{
  for (k=0; k<cnt; k++)
  {
    if (scores[k] > scores[j])
      ranks[j]++;
  }
}

   
// THE FOLLOWING LINES DISPLAY SOME VARIABLES AS A TEST, AS MY PROGRAMMING PROGRESSES.....
printf("%d %d and %d standing for scores[0]=100, scores[1]=43 and scores[6]=78 respectively\n", scores[0], scores[1], scores[6]);
printf("This is the size of the array: %d\n", sizeof(scores));
printf("This is the number of students: %d\n", cnt);
printf("This is the sum: %d\n", sum);
printf("This is the class average: %.2f\n", class_average);
printf("%d %d %d\n", ranks[0]+1, ranks[1]+1, ranks[2]+1);

return 0;
}




NOW, HERE'S WHEN I RUN THIS PROGRAM TO SEE WHAT KIND OF OUTPUTS I HAVE:

> gcc p5.c
> a.out < scores2

100 43 and 78 standing for scores[0]=100, scores[1]=43 and scores[6]=78 respectively
This is the size of the array: 200
This is the number of students: 14
This is the sum: 2136
This is the class average: 152.57
1 14 4

==========================================================================
==========================================================================
ASKER CERTIFIED SOLUTION
Avatar of TascoDLX
TascoDLX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of quco74
quco74

ASKER

Thank you so much!
I can't believe the solution was so simple!
How come I didn't see it? I don't know, but I least I have learn from you. Thanks again!