468,504 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

C++ Frequency, median, and mode without using arrays

hi I'm supposed to do a frequency array and find mode,median and count, but without storing the actual data in an array. I am able to build a frequency array but I can't return the mode since I am not supposed to store the actual value in an array. Please help me with this problem.
Thank you
Guinness
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. void open_input_file(ifstream &inf);
  9.  
  10. int main()
  11. {
  12.  ifstream from_file;
  13.  open_input_file(from_file);
  14.  
  15.  int i=0;
  16.  int j=0;
  17.  int f[30];
  18.  int a=0;
  19.  
  20.  for(j=0;j<=29; j++)
  21.  {
  22.   f[j]=0;
  23.  }
  24.  
  25.  from_file>>a;
  26.  int mode=f[0];
  27.  while(!from_file.eof())
  28.  {
  29.   i=a;
  30.  
  31.   f[i]=f[i]+1;
  32.   cout<<i<<"  "<<f[i]<<"       "<<mode<<endl;
  33.   from_file>>a;
  34.  }
  35.  
  36. void open_input_file(ifstream &inf)
  37. {
  38.  char input_file_name[80];
  39.  
  40.  do
  41.  { inf.clear();
  42.    cout<<"Enter input file name: ";
  43.    cin>>input_file_name;
  44.    inf.open(input_file_name);
  45.  } while (inf.fail());
  46. }
Nov 14 '07 #1
1 4472
sicarie
4,677 Expert Mod 4TB
If you're not worried about efficiency, you can take each individual number, go through and count how many times it occurs, hold both of them aside, and then get the next, count how many times it occurs, compare it to the last. If it's larger, move the new into the old, and get the next, etc...
Nov 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by nick.vitone | last post: by
4 posts views Thread by uspensky | last post: by
5 posts views Thread by Yannick Tremblay | last post: by
3 posts views Thread by Scott | last post: by
7 posts views Thread by Bhadan | last post: by
5 posts views Thread by zfareed | last post: by
3 posts views Thread by mehwishobaid | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.