1

Edit:No Recursion as pointed out by Willis

Task is to first pass a 3-dim array to a function which calls another passing on the same array but 2 dimensions of it then just the one dimension to calculate minimum as well as maximum.

#include <iostream>
#include <fstream>
#include <random>
#include <cmath>

using namespace std;

double Mean, StdDev;
const int n1(2), n2(200), n3(50);

default_random_engine generator(random_device{}());
normal_distribution<double> NormalD(Mean, StdDev);
uniform_real_distribution<double> UniD(Mean-5*StdDev, Mean+5*StdDev);

void fill(double v[][n2][n3]) {
  int i=0;
  while(i==0){
    for(int j=0; j<n2; j++){
      for(int k=0; k<n3; k++){
        double Random_Number = NormalD(generator);
        v[i][j][k] = Random_Number;
      }
    }
    i++;
  }
  if(i==1){
    for(int j=0; j<n2; j++){
      for(int k=0; k<n3; k++){
        double Random_Number = UniD(generator);
        v[i][j][k] = Random_Number;
      }
    }
  }
}

void cnt_hist(double v[n3],double min_v,double max_v,double hist[20]) {
  for(int i=0; i<n3; i++){
    if(v[i]>max_v)max_v=v[i];
    if(v[i]<min_v)min_v=v[i];
  }
  double higherEnd = Mean+5*StdDev;
  double lowerEnd = Mean-5*StdDev; 
  double step = (fabs(higherEnd-lowerEnd))/20;
  double g0 = lowerEnd;
  for(int i=0; i<20; i++){
    double g1 = g0 + step;
    for(int j=0;j<n3; j++){
      if(v[j]>=g0 && v[j]<g1){
        hist[i]++;
      }
    }
    g0 = g1;
  }
  for(int i=0; i<20; i++){
    hist[i]/=100;
  }
}

void cnt_hist(double v[][n3], double min_v, double max_v, double hist[20]) {
  for (int i=0; i<n2; i++) {
    cnt_hist(v[i], min_v, max_v, hist);
  }
}

void cnt_hist(double v[][n2][n3], double min_v[n1], double max_v[n1], double hist[][20]) {
  for (int i=0; i<n1; i++) {
    cnt_hist(v[i], min_v[i], max_v[i], hist[i]);
  }
}

void print_hist(double min_v[n1], double max_v[n1], double hist[][20]){
  for(int i=0; i<n1; i++){
    cout << "Minimum: " << min_v[i] << endl;
    cout << "Maximum: " << max_v[i] << endl;
    int k = -10;
    for(int j=0; j<20; j++){
      double s = 0;
      cout << k << " - " << k+1 << " :";
      while(s<hist[i][j]){
        cout << "*";
        s++;
      }
      cout << endl;
      k++;
    }
  }
}

int main(int c, char* s[]){
  Mean = stod(s[1]);
  StdDev = stod(s[2]);
  double v[n1][n2][n3];
  double min[n1], max[n1], hist[n1][20];
  fill(v);
  cnt_hist(v,min,max,hist);
  print_hist(min,max,hist);
  return 0;
}

Eventually it should display the distributions in 2 histograms like this

./a.out 0 2
Minimum: -7.51451
Maximum: 7.09445
-10 - -9 :
-9 - -8 :
-8 - -7 :
-7 - -6 :
-6 - -5 :
-5 - -4 : *
-4 - -3 : ****
-3 - -2 : *********
-2 - -1 : **************
-1 - 0 : *******************
0 - 1 : ******************
1 - 2 : ***************
2 - 3 : *********
3 - 4 : ****
4 - 5 : *
5 - 6 :
6 - 7 :
7 - 8 :
8 - 9 :
9 - 10 :
Minimum: -9.9992
Maximum: 9.99874
-10 - -9 : ****
-9 - -8 : *****
-8 - -7 : *****
-7 - -6 : *****
-6 - -5 : ****
-5 - -4 : *****
-4 - -3 : *****
-3 - -2 : *****
-2 - -1 : ****
-1 - 0 : *****
0 - 1 : ****
1 - 2 : ****
2 - 3 : ****
3 - 4 : ****
4 - 5 : *****
5 - 6 : ****
6 - 7 : ****
7 - 8 : *****
8 - 9 : *****
9 - 10 : ****

I doubt that the arrays are actually being passed correctly. Thanks for any advice.

  • You could probably do this with templates and vectors. A function that takes a `vector` wouldn't care how many "dimensions" it is, and you could keep passing indexes of the vector back into the function itself until you do something different when you only have a one dimensional vector (which you could check with something like https://stackoverflow.com/a/28796458/2602718) – scohe001 Nov 25 '19 at 17:12

2 Answers2

0

One likely issue with your program is that along the way, you shift from passing min_v and max_v by reference to passing them by value.

Consider the cnt_hist that appears first in the code, the one that looks like this:

void cnt_hist(double v[n3],double min_v,double max_v,double hist[20])

In the function, you update both min_v and max_v. But those values are just local doubles, and once the function exits, the updates are lost.

You should pass them by reference instead:

void cnt_hist(double v[n3],double &min_v,double &max_v,double hist[20])

The parameters that are declared as arrays are fine because arrays are always passed as pointers.

Your handling of the variable i in the fill function is... unusual. Both the while and the if will execute once; what's the point in having the variable i at all?

I should also note that there are no recursive function calls in this program. While there are three functions called cnt_hist, they're different functions. Recursion happens when a function calls itself, either directly or indirectly, not another function with the same name. If your assignment is to generate this histogram with a recursive function, you haven't done it here.

Willis Blackburn
  • 8,068
  • 19
  • 36
  • I assumed it was a recursive call. Thanks for the clarification. As for the variable i, i changed it to a for-loop, i just wanted some way to fill one half of the array with a particular set of numbers then the other. The reference calls also helped. – Very_much_Confused Nov 25 '19 at 17:40
  • You can just directly index with zero: `v[0][j][k] = Random_Number;` – Willis Blackburn Nov 25 '19 at 17:51
0

With arrays (size must be passed, because type decays from array to pointer upon function call).

// requires C++14
template <class T>
void process(T *arr, int size, double &min, double &max) {
    if constexpr (std::is_array<T>::value)
        for (int i = 0; i < size; ++i)
            process(arr[i], sizeof(*arr)/sizeof(**arr), min, max);
    else
        for (int i = 0; i < size; ++i) {
            max = std::max(max, arr[i]);
            min = std::min(min, arr[i]);
        }
}
Kostas
  • 4,061
  • 1
  • 14
  • 32