?
Solved

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

Posted on 2003-03-21
2
Medium Priority
?
209 Views
Last Modified: 2010-04-15
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
Comment
Question by:quco74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
TascoDLX earned 1000 total points
ID: 8185566
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
 

Author Comment

by:quco74
ID: 8187035
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question