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

Click For Summary

Discussion Overview

The discussion revolves around forming a 10 x 10 matrix in C++ and converting it into a 2D histogram using the ROOT framework. Participants explore issues related to formatting the matrix display and seek clarification on the concept of creating a histogram from the matrix data.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes their task of creating a 10 x 10 matrix and expresses confusion about how to display it correctly, noting an offset in the first row.
  • Another participant suggests considering the spacing between printed numbers and the number of characters in each row to address the offset issue.
  • A participant proposes formatting the numbers to include leading zeros to align the matrix correctly, but is unsure how to implement this.
  • Hints are provided regarding the use of the printf() function to achieve the desired formatting.
  • Participants discuss the concept of histograms, with one expressing uncertainty about how to represent a 10x10 matrix in histogram form and what the axes would signify.
  • Some participants suggest consulting the instructor for clarification on the histogram task, indicating a lack of consensus on how to proceed.
  • One participant shares their progress on the histogram and expresses that it does not meet their expectations of being "cool," while others express curiosity about the instructor's feedback.

Areas of Agreement / Disagreement

Participants generally agree on the need for clarification regarding the histogram task, but multiple competing views on how to format the matrix and interpret the histogram remain unresolved.

Contextual Notes

There are unresolved questions about the mathematical representation of the histogram and how the matrix data translates into graphical form. Participants express varying levels of understanding regarding the histogram's axes and overall appearance.

RJLiberator
Gold Member
Messages
1,094
Reaction score
63

Homework Statement


[/B]
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.

Homework Equations



Here is my code
Code:
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;
}

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.
 

Attachments

  • matrix.jpg
    matrix.jpg
    20.1 KB · Views: 707
Last edited:
Physics news on Phys.org
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.
 
  • Like
Likes   Reactions: RJLiberator
1) How many spaces get printed between each number?
2) How many characters are in the numbers that are printed in the different rows?

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?
 
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.)
 
  • Like
Likes   Reactions: RJLiberator
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.)

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

Attachments

  • mat1.jpg
    mat1.jpg
    19.2 KB · Views: 590
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.
 
  • Like
Likes   Reactions: RJLiberator
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.

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.
 
RJLiberator said:
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 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.
RJLiberator said:
I can't even understand what the axis's would represent.
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.
 
  • Like
Likes   Reactions: RJLiberator
  • #10
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!
 
  • #11
If it's convenient, could you post an image of that histogram?
What I visualize doesn't seem very cool either.
 
  • Like
Likes   Reactions: RJLiberator
  • #12
This is what I currently have.
 

Attachments

  • hist.jpg
    hist.jpg
    24.1 KB · Views: 609
  • #13
Thanks.
Not at ALL what I expected!
I would be very curious as to the instructor's response. Hopefully I too can learn something.
 
  • Like
Likes   Reactions: RJLiberator
  • #14
Tom.G said:
Thanks.
Not at ALL what I expected!
I would be very curious as to the instructor's response. Hopefully I too can learn something.
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.
 
  • Like
Likes   Reactions: RJLiberator

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K