# Counting incidence of compliance with logic statement within file

• 11-23-2006
will15
Counting incidence of compliance with logic statement within file
Hallo,

I've been trying to come up with the best approach to generate a wind rose from wind data. A wind rose categorises wind speed and direction and counts the number of times there is a wind within speed and direction bounds.

This information is eventually expressed as a probability table. e.g.

.......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

At the moment the data is kept in .txt files in this kind of format:

Speed Direction
12.0 10
15.3 90
20.4 180

I've thought of reading files into arrays, using vectors and all sorts of things but at getting nowhere. Some expert advice as to where to start would be most appreciated.

Thanks,

Will
• 11-23-2006
anon
It looks like a 2-d array (or a vector of vectors) would be needed.

wind[4][3], where the first dimension represents the 4 directions (you are missing 270-360), and the second dimension the 3 speed ranges. (Basically the data would lay out like in the table.)

To add a "wind" find out the direction index i (0-90 => 0, 90-180 => 1 etc) and the speed index j (0-10 => 0, 10-20 => 1 etc) and increment wind[i][j]++;
• 11-23-2006
will15
Hi,

So, if I write the .txt file to a 2d array using fstream, do you think I could increment a value in a secondary array representing the results table?

Will
• 11-23-2006
anon
The 2-d array would be an array of counters. At first it needs to be zero-initialized. Each time you read a new line of input from the file, the program will determine which speed range (0-2) and direction (0-3) it falls into. Then you increment the respective array element.

In the end you output the array formating it the way you want, print the table captions, align columns etc (I hate this stuff :))

That is, if you only need to count the winds from particular directions, you don't need to store the wind data for each item anywhere. If you want to do anything else with the file data besides that, yes, you might store the data, for example in a vector of class/struct Wind:
Code:

```struct Wind {     unsigned speed;     unsigned direction; };```
But you don't need this just to count different types of winds. You can discard the data as soon as you know which array element to increment, and take another input from the file.
• 11-24-2006
will15
Do I have to get my .txt file into an array before I process it?
• 11-24-2006
will15
If so I wrote the following. Which does not seem to work:
Code:

```#include <iostream.h> #include <stdlib.h> #include <fstream> int main() { char fileName; int noOfReadings; cout<<"How many lines of data are there?"<<endl; cin>> noOfReadings; double wind[noOfReadings][noOfReadings]; cout<<"Enter the name of the input file: " <<endl; cin>> fileName; cin.ignore(80,'n'); ifstream myDataFile; myDataFile.open(fileName); myDataFile>>wind; for(int i=0;i<noOfReadings;i++)  {   myDataFile>>wind(i);  }       system("PAUSE");       return 0; }```
Any idea why not?
Cheerz Will
• 11-24-2006
Salem
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```
You want several different types of lines