0

So what I have is two arrays each with different temperature readings. I have a low array that includes the low temperatures of the month and a high array that includes the high temperatures of the month. I got these variables by reading them in from a text file. My code for that looks like:

while ( !file.eof() )
{
    file >> high[i] >> low[i];
    i++;
}

I know the size of the array since it is the second line in the text file show below.

April 6, 2010
24
88   72
52   36
73   48
89   72
97   84
61   41
48   37
68   45
88   63
79   52
48   21
55   46
54   41
97   81
55   37
79   70
72   43
68   45
102  80
57   39
37   32
45   28
66   45
59   36

Now I have to get the counts from the temperatures and create a histogram that looks like the sample below. So if there are for example 2 temperatures greater or less than equal to 49 I would print two stars after the interval label.

 < 0|
<= 9|
<=19|
<=29|*
<=39|
<=49|**
<=59|* 
<=69|
<=79|*
<=89|
<=99|
 >99|

I was thinking of combining the two arrays into one and looping through them but I the only way I currently know how to do this is by using a switch case or a giant if else block. Even if the there are no temperatures between 0 and 9 I would still have to print "<= 9|". I'm not sure how to create the count function without actually using a bunch of if-else's. Any ideas would be greatly appreciated.

I have this code for my count function right now that combines the two arrays but that is about it:

void count(float lowArr[], float highArr[], int num){
    int size = num * 2, i = 0, j =0;
    float *arr;
    arr = new float[size];

    for (i ; i < num; ++i)
    {
        arr[i] = lowArr[i];
    }
    for (j; j < num; ++j, ++i)
    {
        arr[i] = highArr[j];
    }
}
binaryhex
  • 113
  • 2
  • 12
  • Use a lookup table for the limits of the histogram rows. This allows you to change the row limits without having to change the lookup function or histogram drawing function. – Thomas Matthews Sep 10 '15 at 23:51
  • I am curious why <= 9, <= 19, etc. rather than < 10, < 20, etc. Equality with floating point usually doesn't work very well due to rounding errors and floating point representation. – Richard Chambers Sep 11 '15 at 00:43
  • Don't use `eof` in your loop condition (see [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)). If the intervals are broken into static ranges of ten values, you can directly map the values into those ranges. – Jason Sep 11 '15 at 01:47
  • @Jason, thanks for that. I fixed it to while(file >> low[i] >> high[i]) instead. – binaryhex Sep 11 '15 at 01:56
  • @ThomasMatthews, do you a good resource to learning how to use a lookup table. I have never used one before and would like to learn how. – binaryhex Sep 11 '15 at 01:57
  • @KrustyCheerio: Simple. Define an array that has the "less than" values. Iterate over the array. – Thomas Matthews Sep 11 '15 at 15:08
  • You could always search the internet for "C++ lookup table" or "C++ look up table", sometimes `std::map` would work too. – Thomas Matthews Sep 11 '15 at 15:09

1 Answers1

0

He is one way to do this:

  1. Create an array with the correct number of entries for your histogram
  2. Fill in the array with the counts for each entry
  3. print out the array.

For #2, you can use some maths to work out what index to use into your array. You might still need a couple of if statements, but not as many as you are currently thinking:

  • Test for < 0 - increment the count in array[0]
  • Test for > 99 - increment the count in array[11]
  • Otherwise work out which entry to fill in by using (temp / 10) + 1. The +1 is to skip over the first array entry (which you are using for < 0) the temp / 10 will use integer maths (assuming temp is an int) to give you a number between 0 and 9 indicating the "tens" part of the temp.
The Dark
  • 8,213
  • 1
  • 14
  • 19