1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ (ROOT) Form a matrix and send it to a 2d Histogram

  1. Jan 15, 2016 #1

    RJLiberator

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data

    1. I've been tasked with forming a 10 x 10 matrix with elements 0, 1, 2, 3, 4, 5,...
    and have it display properly.
    2. Then, take this matrix and make a 2d-histogram out of it.

    2. Relevant equations

    Here is my code
    Code (Text):

    void matrix6( const int n = 10)
    {

    float I[n][n];  // Creates a 10*10 matrix

    // Homework, put this matrix into a 2-dimensional histogram, it will look cool.


    for(int i=0; i< n; i++)  // This loops on the rows
    {
       for(int j=0; j< n; j++)  // This loops on the columns
         {
           I[i][j] = i*10+j;


         cout<< I[i][j] <<"  ";
         }
      cout << endl;
    }
    return;
    }
     
    3. The attempt at a solution

    So my code works, in a way. It is nearly perfect except the first line of the matrix is slightly offset. I am not sure how to fix this problem. Any suggestions? I have attached a picture of it to show you what I mean. The first row is offset.

    The bigger problem is task #2. What does it even mean to make a 2d histogram out of a 10x10 matrix with elements that just count up from 1? My professor suggested that this would 'look cool.'

    I think when I have a more clear picture of what it would even look like, I could begin to write the code... but it's making mathematical sense to me right now.
     

    Attached Files:

    Last edited: Jan 15, 2016
  2. jcsd
  3. Jan 15, 2016 #2

    Tom.G

    User Avatar
    Science Advisor

    As for the first line being offset.
    Consider:
    1) How many spaces get printed between each number?
    2) How many characters are in the numbers that are printed in the different rows?

    As for task #2, I'll leave that to those that are familiar with the ROOT language.
     
  4. Jan 15, 2016 #3

    RJLiberator

    User Avatar
    Gold Member

    Well, I see that the [0, 0] spot needs 1 more space, and the next spot needs 2 more spaces and the third spot needs 3 more spaces and so on on row 0.

    So, if I could make it 0.0 and 1.0 and 2.0 it would work out, but then the other numbers would be 10.0, 20.0 etc. and that wouldn't work.

    If I could perhaps make it 00 01 02 03 that would work really well, but not sure how to do that.

    Or perhaps there could be another code for row 1 separately? Is that a better route to go?
     
  5. Jan 15, 2016 #4

    Tom.G

    User Avatar
    Science Advisor

    Well, you could revisit posts 8 and 9 to the question you had yesterday. Then read about the printf() function in the language reference manual. (Nope, no free give-aways here. But you are on the right track.)
     
  6. Jan 15, 2016 #5

    RJLiberator

    User Avatar
    Gold Member

    Oh yes! That makes a lot of sense. I bet I can figure it out with that hint.
    That should take care of issue 1.
     
  7. Jan 15, 2016 #6

    RJLiberator

    User Avatar
    Gold Member

    Okay! Now we are looking good with part 1. I got a bit lucky on my print commands, but it was a good learning process.

    Code (Text):
      printf(" %02i ", I[i][j]);
    This turned out to be the winning code for the image attached.
    Now I just would like to understand 2. Then, take this matrix and make a 2d-histogram out of it.
     

    Attached Files:

  8. Jan 15, 2016 #7

    Tom.G

    User Avatar
    Science Advisor

    Quick study! When learning a computer language I find it is useful to read thru the reference manual first. There is no way to remember everything in it on the first try, or even the twenty first. It does give an indication of what is available though, and when you need to do something you will often remember something that is related; and maybe even where to find it.

    Here is a hint about histograms; they are just a form of graph, a bar graph to be more specific. Remember to RTFM, "Read The Fine Manual" (a different F-word often used), it is your (somewhat difficult) friend.
     
  9. Jan 18, 2016 #8

    RJLiberator

    User Avatar
    Gold Member

    I am watching some c++ tutorial videos and have some manuals hanging around :D. It's a bit of a slow process, but I am learning (c++, python, fortran).

    But I'm still left with this mathematical question here. How does one take a 10x10 matrix with entries going up from 0 to 99 into a histogram. I am familiar with what histograms are, but that seems to be just bars of equal length everywhere...
    Any insight on what the graph might look like? I can't even understand what the axis's would represent.
     
  10. Jan 18, 2016 #9

    Mark44

    Staff: Mentor

    I would contact the instructor to get some clarification on what he meant. It's not at all obvious to me what he's looking for.
    Plural of axis is axes.

    Also, despite what many people believe, to make a singular noun plural, you don't add 's. E.g, I have two cars, not two car's.
     
  11. Jan 18, 2016 #10

    RJLiberator

    User Avatar
    Gold Member

    Excellent. Thanks for the insight. I have a working graph right now, but I don't think it's 'very cool looking' as the instructor stated :p.

    I appreciate the help as always, Mark and Tom!
     
  12. Jan 19, 2016 #11

    Tom.G

    User Avatar
    Science Advisor

    If it's convenient, could you post an image of that histogram?
    What I visualize doesn't seem very cool either.
     
  13. Jan 19, 2016 #12

    RJLiberator

    User Avatar
    Gold Member

    This is what I currently have.
     

    Attached Files:

  14. Jan 19, 2016 #13

    Tom.G

    User Avatar
    Science Advisor

    Thanks.
    Not at ALL what I expected!
    I would be very curious as to the instructor's response. Hopefully I too can learn something.
     
  15. Jan 19, 2016 #14

    Mark44

    Staff: Mentor

    Based on the output shown in post #6, the histogram is just what I expected. If that's what the instructor had in mind, it's not that interesting, IMO.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ (ROOT) Form a matrix and send it to a 2d Histogram
  1. Root loci (Replies: 1)

  2. Complex Roots (Replies: 14)

Loading...