473,734 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 4939
sicarie
4,677 Recognized Expert Moderator Specialist
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

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

Similar topics

8
14110
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find the median of the another, though I'm not sure how. I've been able to add the "Median" function (from the Microsoft Access Help Archive), but I can't figure out how to incorporate that into the Totals. Do I need to use the "expression" and...
2
12985
by: Drebin | last post by:
I'm looking for mean, median, mode and standard deviation - maybe a couple methods that take in an array of floats or something?? Thanks much!
4
8175
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
5
9429
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new query it without making a table out of query 1. I can't find a median function in the "Total" field, so is there so way to make an expression to calculate the median of the orignial data from query 1 in my new query? Also, what does name by...
5
3590
by: Yannick Tremblay | last post by:
Hi, I have a std::map<id, timestampcollection. The id is the normal reference so the key-value ordering is correct. However, once in a blue moon, I want to prune this collection. So that it doesn't happens to often, I prune the 50% oldest entries and go back to normal usage of the collection. In order to do so, I need to find the median timestamp then delete all entries older than the
3
4671
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
7
36030
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
5
4901
by: zfareed | last post by:
I created a program to determine the medians of arrays, be it of type int or float. I have used a template function and the problem I'm having is returning the float median for even arrays. Can anyone help? include <iostream> using namespace std; template <class T> T getMedian(T* array, int size) {
3
5153
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include <vector> double* median(double a, int a_size) // Function Median
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.