-1

I am trying to print a histogram using for loops, but, for some reason my second loop which is supposed to run based on if conditions, is not working, and I end up having only 1 "#" for each counter.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main () {

char hash = '#';                                
double list[10];                                
int size = sizeof(list) / sizeof(list[0]);      

for(int i = 0; i < size; i++) {                 
  list[i] = ((rand() % 10000) / 10000.0);       

  for(int j = 0; j < 10000; j++) {              
    if
    (      
        (list[i] >= 0 && list[i] <= .1) ||
        (list[i] >= .1 && list[i] <= .2) || 
        (list[i] >= .2 && list[i] < .3) ||
        (list[i] >= .3 && list[i] < .4) ||
        (list[i] >= .4 && list[i] < .5) ||
        (list[i] >= .5 && list[i] < .6) ||
        (list[i] >= .6 && list[i] < .7) ||
        (list[i] >= .7 && list[i] < .8) ||
        (list[i] >= .8 && list[i] < .9) ||
        (list[i] >= .9 && list[i] < 1.0)
    ) 
      {
      list[i] += 1;             
    }
  }
}

cout << "HISTOGRAM OF 10 COUNTERS " << endl;
for(int i = 0; i < size; i++) {

  cout << "[" << i << "] \t" << list[i] << "\t" << string(list[i], hash) << endl;
}

  return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Lima
  • 27
  • 2
  • The for loop tests the same value 10000 times.You probably want to use `j` somewhere in it. That said, stop writing code. Think this out with pen-on-paper and then return to code with a plan. – user4581301 Oct 17 '19 at 06:25
  • 3
    Your multiline `if` condition is the same as testing for `list[i]` being between 0 and 1, and that is obviously not what you intended. Relook at your logic. – acraig5075 Oct 17 '19 at 06:35

2 Answers2

0

There are multiple things wrong with your code, but since this looks like homework that you should do on your own, I will just explain the observed behaviour:

In each iteration of the outer for loop list[i] is initially set to a value between 0 and 1. Your if condition can be shortened to list[i] >= 0. && list[i] < 1.. It will always match on the first iteration of the loop. But then you add 1 to that value. As a result, it won't match any of the subsequent evaluations of that condition, because then list[i] is 1 + original value of list[i], which is not between 0 and 1.

Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
0
  1. You are using list[] for random numbers and for histogram

    That is wrong! The random values should be stored in temp variable and generated in main loop (j<10000) instead.

  2. the for loops should not be nested

    The first one (i<10) should just clear your histogram to zero. The second should generate your random value, compute histogramn bin index from it and increment the bin count by one.

  3. The big if condition

    is not complete and it should be (size=10) individual or if/else conditions that compute i index. Also it should use the temp random value instead of histogram value:

    random_value=((rand()%10000)/10000.0);
    i=-1;if (random_value<0.0);
    else if (random_value<0.1) i=0;
    else if (random_value<0.2) i=1;
    else if (random_value<0.3) i=2;
    ...
    else if (random_value<1.0) i=9;
    if (i>=0) list[i]++;
    

    However your random value is in range <0.0,1.0) so ifs can be removed completely by:

    random_value=((rand()%10000)/10000.0);
    i=floor(random_value*double(size));
    list[i]++;
    

For more info see

You can find there C++ histogram example ..

Spektre
  • 49,595
  • 11
  • 110
  • 380