Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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

==========================================================================
==========================================================================
0
quco74
Asked:
quco74
1 Solution
 
TascoDLXCommented:
You need to initialize your 'size' variable.

Try:

// FOR-LOOP TO CALCULATE THE AVERAGE OF THE NUMBER OF SCORES RETRIVED
sum = 0;
for (i=0; i<cnt; i++)
{
  sum += scores[i];
}
class_average = (float) sum/cnt;
0
 
quco74Author Commented:
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!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now