Solved

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

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

Expert Comment

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

Author Comment

by:m0neyman
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Accepted Solution

by:
grg99 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ah! I finally got it!!
Thanks for all you help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now