Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-01
7
Medium Priority
?
532 Views
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:
Scores
  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 ) {
      score_count++;
      print_scores( score );
      total_score_calc( score );
   }
   cout << endl << endl << "Descriptive Statistics" << endl;
   mean_calc();
   print_stats();

} //end main

void print_scores( int xscore )
{
  cout << setw( 2 ) << xscore << " ";
  print_score_count++;
  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;
  }
  else
     zero_count++;
}

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;
}

0
Comment
Question by:m0neyman
7 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:m0neyman
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.
0
 
LVL 12

Expert Comment

by:Salte
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.

Alf
0
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.

 
LVL 22

Accepted Solution

by:
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.

0
 

Author Comment

by:m0neyman
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.
0
 

Author Comment

by:m0neyman
ID: 9666350
Ok, I am making progress.

I was able to write the following code:
int main(){
   cout << "Scores" << endl << endl;
   while ( cin >> score ) {
      score_count++;
      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?
0
 

Author Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

885 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