2d array histogram

This is a discussion on 2d array histogram within the C++ Programming forums, part of the General Programming Boards category; Hi, I've put together this code that processes a 2d array: Code: #include <iostream.h> #include <stdlib.h> #include <string> #include <fstream> ...

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

    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

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    > 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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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

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

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

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    Cool Cheers

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    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
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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