-
2d array histogram
Hi,
I've put together this code that processes a 2d array:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string fileName; // create a filename storage for a string of characters
const int MAXSIZE=100; // absolute max size of the array
double readings[MAXSIZE]; // array declaration
int hist [MAXSIZE];
int noOfReadings; // actual number of readings
int noOfIntervals; // actual number of intervals
int carryon = 1; // initialise flag to continue calculations
while (carryon)
{
cout << "Enter actual number of readings (0<n<="
<< MAXSIZE << "): ";
cin >> noOfReadings; cout << endl;
// enter name of file with input data
cout << "Enter the name of the file with readings data: ";
cin >> fileName;
ifstream myDataFile;
myDataFile.open(fileName.c_str());
// input actual readings from file
for (int i=0; i< noOfReadings; i++)
{
myDataFile >> readings[i];
}
// find max and min
// initialise current max and min
double maxRead = readings[0];
double minRead = readings[0];
for (int i = 1; i < noOfReadings; i++)
{
if (maxRead < readings[i])
{
maxRead = readings[i];
}
if (minRead > readings[i])
{
minRead = readings[i];
}
}
cout << "Enter actual number of intervals for a histogram (0<k<="
<< noOfReadings << "): ";
cin >> noOfIntervals; cout << endl;
// compute the histogram
// size of interval
double d = (maxRead - minRead)/noOfIntervals;
cout << "d=" << d << endl;
// initialise this histogram first
for (int i = 0; i < noOfIntervals; i++)
{
hist[i] = 0;
}
// look through each reading and increment the appropriate interval count
int offset;
for (int i = 0; i < noOfReadings; i++)
{
offset = int((readings[i]-minRead)/d);
if (offset < noOfIntervals)
{
hist[offset]=hist[offset]++;
}
else
{
hist[offset-1]=hist[offset-1]++;
}
cout << "offset=" << offset << endl;
}
// output the results
cout << "The largest reading was " << maxRead << endl;
cout << "The smallest reading was " << minRead << endl;
cout << "Now the numbers of readings in each interval between max and min: "
<< endl;
for(int i=0; i < noOfIntervals; i++)
{
cout << "Interval(" << i+1 << ")= " << hist[i] << endl;
}
cout << endl << "Do you want to continue? (type 1 if yes; 0 if not) ";
cin >> carryon;
}
cout << "End of Readings" << endl;
return 0;
} // end main()
Does any body know how I can adapt it to do the same for a 2d array? Also; the histogram must create a 2d output, so that if numbers in the first and second columns in the same line fit within certain bounds, a counter is incremented. e.g.
For the raw data that follows:
1 1
1 2
1 3
There are conditions like:
if arrayName[i]=1 || arrayName[j]=1
then hist offset++
Thanks,
Will
-
> Does any body know how I can adapt it to do the same for a 2d array?
Do what? Read from the file into a 2d array? Make hist a 2d array?
Meanwhile a few pointers ...
You don't need to cycle through hist to set all its elements to 0 before using it. If on hist definition you do this
Code:
int hist [MAXSIZE] = {0}; // all elements will be initialized to 0.
If you intend to protect from user mistakes, you may want to make sure calls such as
cin >> noOfReadings; have some form of error check. What happens if the user enters "hello" instead of "67"? The same is true for other input operation in your code.
-
I think it's better to use
Code:
int hist [MAXSIZE] = {};
and have all elements initialized in the same way, since someone who starts using {0} might think that they could initialize all elements to 1 with
Code:
int hist [MAXSIZE] = {1};
instead of just the first element.
-
Hi,
If I explain the function maybe that would help. The program is to make a wind rose from raw wind data that consists of a direction and speed. This is read in from a text file into a 2d array.
Wind speed and direction are then categoriesed (bounds establised) and the program checks which category the line of data falls into.
So, wind speed 12 direction 48 would mean that the value of the count in the box shown in red would increase by one.
Code:
Speed 0-10 10-20 30-40 Total
Direction
0-90 10 0 0 10
90-180 40 3 0 70
180-270 0 10 10 20
Total: 50 40 10 100
All lines of data are then checked to make complete table.
Cheers,
Will
-
Hi,
I think I have to do a nested for loop. However, I'm not sure how to get the program to apply one set of boudaries to the speed column and another to the direction colum and then use the output from both applications in a third.
So, for example if wind speed is between 0 and ten and direction is 30 then increment the count for the first boundry. But if wind speed is between 0 and ten and direction is 110 then increment the count for the second boundry.
Thanks any help is greatly appreciated.
Will
-
Ok. I'm really tired today, Will, for some reason. No reason to, so I hope i'm not egging something. The season is on.
Don't take a lack of replies badly. I'll get to this tomorrow if someone doesn't help in the meantime. For now, anything more complex than adding seems to be taxing my brain.
-
-
Ok... now... hmmm...
Well, you seem to need two 2d arrays. One will store the raw data.
int raw_data[MAXSIZE][2];
The histogram on the other hand is the archetypal matrix. It can be defined as:
int hist[ROWS][COLUMNS]
ROWS and COLUMNS represent each the intervals you are going to use for direction and speed, respectively.
The actual sizes of these arrays depend on user input. You should define them dynamically with the operators new/delete combination. But if you still want to have them fixed like you have now and simply ignore the remaining elements, that's fine. For some reason, I think that is not the purpose of this exercise.
In order to fill the histogram it is then very easy... You only need one For statement. Something like:
Code:
for (i = raw_data[begin][] to raw_data[end][]) { // for each "row" in raw_data
dir_interval = calculate_interval_for_direction;
spd_interval = calculate_interval_for_speed;
hist[dir_interval][spd_interval] += 1; // incremeant count by 1
}