Counting incidence of compliance with logic statement within file

This is a discussion on Counting incidence of compliance with logic statement within file within the C++ Programming forums, part of the General Programming Boards category; Hallo, I've been trying to come up with the best approach to generate a wind rose from wind data. A ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    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

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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]++;

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    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

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    Do I have to get my .txt file into an array before I process it?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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
    - column headings line
    - data lines
    - totals line
    It's only the middle set which seem to be usefully stored in an array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21