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

Posted on 2003-11-01
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
LVL 85

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.

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

LVL 22

Accepted Solution

grg99 earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now