How to sort input from a file without using an array?

Posted on 2003-11-01
Medium Priority
Last Modified: 2010-04-02
Basically I need to write a program that reads in data from a file (ie. program.ccp < input_file) and do several things with the data without using an array for the scores specifically.

I need to take this data from external file:
28 80 81 35 47 60 82 77 58 36 37 62 70 68 86 79 63 64 54 57
69 54 65 74 82 68 56 89 0 26 60 76 47 61 52

And make the output look like the following:
  28  80  81  35  47  60  82  77  58  36  37  62  70  68  86
  79  63  64  54  57  69  54  65  74  82  68  56  89   0  26
  60  76  47  61  52

Descriptive Statistics
Number of non-zeroes =   34; Number of zeroes =    1        
Mean =  61.85;   Standard Deviation =  16.35

Score Distribution
      Score   Frequency   Cum Freq
         89         1                 1
         86         1                 2
         82         2                 4
         81         1                 5
  (and so on)

I am not allowed to use an array for the individual 'scores', however I can use an array for 'Frequency' and 'Cum Freq'.
int freq[MAX_SCORE], cum_freq[MAX_SCORE];

So I was able to take care of the output up until the score distribution. But now I am stuck as to how to manipulate the output at this point. The two allowable arrays seem easy enough, but how do I read in the input, sort (highest to lowest) and output in required columns? I thought 'bubble sort' required an array. How else can I sort an input variable without knowing the whole stream?

Here's my code so far, I appreciate any help.
int main(){
   cout << "Scores" << endl << endl;
   while ( cin >> score ) {
      print_scores( score );
      total_score_calc( score );
   cout << endl << endl << "Descriptive Statistics" << endl;

} //end main

void print_scores( int xscore )
  cout << setw( 2 ) << xscore << " ";
  if ( print_score_count >= VALUES_PER_LINE ){
     cout << endl;
     print_score_count = 0;

void total_score_calc( int xscore )
  int xscore_square = 0;
  if ( xscore > 0 ){
     total_score = total_score + xscore;
     xscore_square = xscore * xscore;
     square_total = square_total + xscore_square;

void mean_calc( void )
  int square_mean;
  double mean_square;
  double temp_mean_cal;

  // Calculate mean
  mean_num = static_cast< double >(total_score) / (score_count - zero_count);

  // Calculate standard deviation
  square_mean = ((square_total) * (score_count - zero_count)) / ((score_count) * (score_count - 1));
  mean_square = ((total_score) * (total_score)) / ((score_count) * (score_count - 1));
  temp_mean_cal = static_cast< double >(square_mean) - static_cast< double >(mean_square);
  std_dev_num = sqrt( temp_mean_cal );

void print_stats( void )
  cout << "Number of non-zeroes =   " << (score_count - zero_count) << "; Number of zeroes =  " << zero_count << endl;
  cout << "Mean = " << setprecision( 4 ) << mean_num << ";    Standard Deviation =  " << setprecision( 4 ) << std_dev_num << endl;

Question by:m0neyman
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
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9665299
I take it you haven't been taught linked lists yet?

Author Comment

ID: 9665312
No, I have covered linked lists or classes yet.

The requirement says specifically:
The scores should NOT be read into an array. The score values themselves should be used to update any frequency arrays, but should not be stored in an array. In other words, there should only be a simple variable, say score, which will hold in turn each score value as you read through the data.

Also, The program should not contain any literal numeric constants except for -1, 0, 1, and 2.
LVL 12

Expert Comment

ID: 9665499
Actually the problem can be solved without arrays, linked lists or any other collection.

However, this smell homework and I suggest you read again what you agreed to when you signed up to this site.

I can give you a few hints though.

1. To print out the scores you don't need an array, a single integer variable can be used to hold all the scores long enough that you can read it, use it to compute what you need to compute and print it out. Then you can read the next value into the same variable without storing in an array.

2. To compute averange or standard defviation you don't need all the values. To compute the mean you only need two variables and to compute the standard deviation you only need three variables.

Good luck with your homework.

Independent Software Vendors: 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!

LVL 22

Accepted Solution

grg99 earned 750 total points
ID: 9665734
You don't need to sort anything.

You only need to print out the scores in descending order.
But this is easy as you're allowed to have an array to hold the frequencies,
so you just index thru the array starting from the highest score down to the lowest.


Author Comment

ID: 9666115
I got the standard deviation part, that is done. Sorry, but I am still confused on the frequencey arrays.

The 'frequency' array is only allowed to hold the value of the number of occurrences of a particular number not the score value itself. To add confusion the 'cumulative frequency' array is the number of scores that were greater than or equal to the score value.

1. I still don't see how I can output the input in descending order without an array. Is the 'cumulative frequency' array the key?

2. How do I index through the frequency array to get the scores? The 'frequency' array holds the values of the count of occurrances (1 or 2). How can I get a score value from the 'frequency' array?

Yes this is a homework assignment. I'm not asking for code, just how this will work logically, that is my roadblock.

Author Comment

ID: 9666350
Ok, I am making progress.

I was able to write the following code:
int main(){
   cout << "Scores" << endl << endl;
   while ( cin >> score ) {
      freq_calc( score );
   return 0;
} //end main

void freq_calc( int xscore )
  if ( score != 0 ){
     ++freq[ xscore ];
     cum_freq_num = cum_freq_num + freq[ xscore ];
     cout << setw( 6 ) << xscore << setw( 17 ) << freq[ xscore ] << setw( 24 ) << cum_freq_num << endl;

This gives me the "Score Distribution" output I need, but not in descending order. Also, I need to add it into my original code and make sure it outputs last. Any suggestions?

Author Comment

ID: 9666999
Ah! I finally got it!!
Thanks for all you help.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 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