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

• Comp Sci
• RJLiberator
In summary: But it depends on what you're expected to learn from this exercise.In summary, the conversation was about creating a 10 x 10 matrix with elements 0, 1, 2, 3, 4, 5,... and displaying it properly. The expert provided guidance on how to fix the offset in the first row of the matrix and suggested using the printf() function. The expert also hinted at how to approach creating a 2d-histogram out of the matrix. The conversation ended with the poster sharing their progress on the histogram and expressing curiosity about the instructor's response.
RJLiberator
Gold Member

## 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
20.1 KB · Views: 636
Last edited:
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.

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

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
19.2 KB · Views: 518
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.

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.

RJLiberator
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!

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

RJLiberator
This is what I currently have.

#### Attachments

• hist.jpg
24.1 KB · Views: 537
Thanks.
Not at ALL what I expected!
I would be very curious as to the instructor's response. Hopefully I too can learn something.

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

RJLiberator

## 1. How do I form a matrix in C++?

To form a matrix in C++, you can use a nested for loop to iterate through rows and columns and assign values to each element of the matrix. Alternatively, you can use the built-in array functions in the vector or array library to create a matrix.

## 2. How do I send a matrix to a 2D histogram in C++?

To send a matrix to a 2D histogram in C++, you can use the Fill function from the ROOT library. This function takes in the x and y coordinates of each element in the matrix and adds it to the histogram. You can also specify the number of bins and range for the histogram using the TH2F or TH2D classes.

## 3. Can I plot a 2D histogram from a matrix using ROOT in C++?

Yes, you can plot a 2D histogram from a matrix using ROOT in C++. After sending the matrix to the histogram, you can use the Draw function to plot the histogram and customize the appearance using various options such as color, title, and axis labels.

## 4. How do I access and manipulate a 2D histogram in C++?

To access and manipulate a 2D histogram in C++, you can use the GetBinContent and SetBinContent functions. These functions allow you to retrieve and modify the bin contents of the histogram at a specific x and y coordinate. You can also use other functions such as GetBinError and SetBinError to handle uncertainties in the histogram data.

## 5. Is there a limit to the size of the matrix or histogram in C++ using ROOT?

The size of the matrix or histogram in C++ using ROOT is limited by the available memory on your computer. However, you can optimize the size of the histogram by adjusting the number of bins and range to fit your data. Additionally, you can use the SetDirectory function to save the histogram to a file and free up memory for larger datasets.

• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Engineering and Comp Sci Homework Help
Replies
17
Views
1K
• Engineering and Comp Sci Homework Help
Replies
18
Views
2K
• Engineering and Comp Sci Homework Help
Replies
14
Views
3K
• Engineering and Comp Sci Homework Help
Replies
23
Views
7K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
9
Views
2K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
10
Views
2K
• Engineering and Comp Sci Homework Help
Replies
9
Views
4K