c++ - 如何计算数字范围内的数组项

标签 c++

我的任务是尝试根据用户提供的一组样本值创建直方图。我已经创建了从样本值输入创建数组的程序部分,但现在我必须为直方图获取用户输入。它们给出最小值、最大值和箱数。所以,我假设 bins 的数量指定了直方图数组的大小。但我对如何转到其他数组并计算特定 bin 的指定范围内有多少值感到困惑。我希望这是有道理的。到目前为止,这是我的程序代码:

#include <iostream>
using namespace std;


#define MAX_SAMPLES 100
#define MAX_BINS 20
#define DOUBLE_TOLERANCE 0.0000005
#define EXIT_VALUE -999
int promptUserAndGetChoice(); //function prototype for the menu

//for information describing sample set of data and functions that operated on //those data
class SamplingClass 
   {  
      private:
         char charID; //the user enters an id for his sample set
         int numbOfValues; // the number of good values the user enters
         double sampleValues[MAX_SAMPLES]; //for the set of sample values. 
                                           //max is 100
      public:
         bool readFromKeyboard(); //prototype function 
         bool printToScreen();//protype function

      SamplingClass(); //constructor

   };

      SamplingClass::SamplingClass() //initializing charID
       {
        charID = 0;
       }

bool SamplingClass::readFromKeyboard()
   {
      int i = 0;
      cout << "Enter character identifier for this sample:";
      cin >> charID;
      cout << "you entered " <<charID << "\n";
      cout << "Enter all samples, then enter -999 to end:\n";

     while (i < MAX_SAMPLES)
      {
        cin >> sampleValues[i];     
   if 
      (sampleValues[i] < EXIT_VALUE + DOUBLE_TOLERANCE && sampleValues[i] > EXIT_VALUE - DOUBLE_TOLERANCE)
      {
         break;

      }//End if/else

        i++;

       }//End while

   numbOfValues = i;    
    return true;

   }

//this function checks whether charID is empty and then performs accordingly
bool SamplingClass::printToScreen() 
   {
   if (numbOfValues == 0) ///either make a test for existance first or charID

      {
         cout << "ERROR: Can not print uninitialized sampling!\n";
         return false;

      }  

   else 

      { 
         cout << "Data stored for sampling with identifier " << charID << ":\n"; 
         cout << "Total samples:" << numbOfValues << "\n";
         cout << "Samples (5 samples per line):\n";
         int i;

         for(i=0; i<numbOfValues;i++)
         {
         cout << sampleValues[i] << " ";

         if (((i+1) % 5) == 0)
          {  
             cout << endl;
          }

         }
         cout << endl;
        return true;

      }
    } 

class HistogramClass 
   {  
      private:
         double minBinValue; //specified by user
         double maxBinValue; // specified by user
         int numbBins; //specified by user, max of 10
         int histoBinCounts[MAX_BINS];

      public:
         bool setupHistogram(); //prototype function 
         bool addDataToHistogram(SamplingClass &sampling);//protype function
         bool printHistogramCounts();
         bool displayHistogram(); 
   };

bool HistogramClass::setupHistogram()
   {
      cout << "Enter minimum value:";
      cin >> minBinValue;
      cout << "Enter maximum value:";
      cin >> maxBinValue;
      cout << "Enter number of bins:";
      cin >> numbBins;
      cout << "\n";
      if (numbBins <= MAX_BINS) 
         {cin >> numbBins;}
      else
        cout << "Sorry, the maximum amount of bins allowed is 20. Try again!\n";

    }



//function for the menu options that display to user  
int promptUserAndGetChoice()

   {
     cout << "1. Enter a sample set of data values\n";
     cout << "2. Print the contents of the current sample set\n";
     cout << "3. Reset / Provide values for setting up a histogram\n";
     cout << "4. Add the contents of current sample set to histogram\n";
     cout << "5. Print bin counts contained in histogram\n";
     cout << "6. View the histogram in graphical form\n";
     cout << "0: Exit the program\n\n";
   }

int main()

{  
   const int enter_option = 1;
   const int printContents_option = 2;
   const int reset_option = 3;
   const int add_option = 4;
   const int printBin_option = 5;
   const int viewHist_option = 6;
   const int exit_option = 7;
   int menuChoice;
   SamplingClass sampleSet;
   HistogramClass histoSet;

   do 
      {
        promptUserAndGetChoice();
        cout << "Your Choice: ";
        cin >> menuChoice;

   if (menuChoice == 1)
      {
       sampleSet.readFromKeyboard();
       cout << "Last Operation Successful: YES\n\n";
      }
   else if (menuChoice == 2)

      {
      sampleSet.printToScreen();
      }

   else if (menuChoice == 3)
      {
      histoSet.setupHistogram();
      }

    } 
     while (menuChoice != 7);
     return 0;
}

最佳答案

直方图中的每个 bin 通常大小相同。因此,当用户为您提供最小值、最大值和 bin 数量时,您可以计算每个 bin 的大小,从而计算范围。每个 bin 的大小都是

bin_size = (max-min)/#_of_bins.

现在要找出一个值进入哪个 bin,计算

bin = ceil(value/bin_size)

(如果您从 0 开始对垃圾箱进行编号,请发言)。并增加这个 bin 中的计数。对所有值执行此操作后,您可以打印出每个 bin 中的计数,这就是您的直方图。

更新:如果 min != 0,则公式为:

bin = (int) (value-min)/bin_size

在这里使用强制转换 b/c codemesserupper 不能使用库。这里的 bin 将是 0 索引的。

关于c++ - 如何计算数字范围内的数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4929000/

相关文章:

C++ 适当的指针成员初始化

c++ - 第二次检查后 flock lock 丢失

在带有 VS2012/w Update 3 的 Windows 7 上构建的 C++ 程序无法在 WinXP SP3 上运行

c++ std::set插入导致段错误

C++:继续创建新变量可以吗?

c++ - 数组成员指针大小的模板推导

c++ - 多线程我的程序的负加速

c++ - 将 Crashpad 与 MacOS Qt 应用程序集成

c++ - 为什么在段错误中通过 B 类中的方法初始化 A 类的指针?

c++ - 设置随机获取项目的机会(C++)?