# arduino 2 dimensional array

So in the last tutorial we have learned about the arrays for a single dimensional arrays, an error is nothing but the collection of similar elements, and in the last tutorial we had an example of marks. We were storing the mark of a student in the array and what, if we want to store the marks of two students at that time, you know what we can do is we can create two arrayed separately and use them or we can create one multi dimensional Array, so a multi dimensional array is nothing but an array and say it another array, for example, if you guys want to store the marks of another student we’re, going to create another array here and we’re, going to call this array as the marks and as you Guys can see here this looks like a table, so we have two rows and six columns, so the first row is going to contain the marks of the first student and the second row is going to contain the marks of the second student. So this is a two dimensional array to declare this we’re going to use marks, then the square bracket and another square bracket. So in this first square bracket we can specify the rows let’s say X and in this second square bracket we can now specify the columns Y. So now, if we see this and compare it to the table here, we have X rows and y columns and in C we can create two dimensional three dimensional.

You know. Similarly, we can create en dimensional of arrays. So this two dimensional array is where it’s easy to use, comparing to the three dimensional or the four dimensional arrays. All right now see an example. So in this tutorial, just for the demonstration purpose, we can store the marks of two students in a two dimensional array and we can create that. Are they right now so it’s going to be int, which is the type of data stored in our array and then the RA name I’m going to call it as marks? And then we need to use two square brackets and in the first square bracket we’re going to specify how many rows we want, or you know how many number of students data or the marks we can a store is – and this is going to be two and Next, how many elements will be present in that each individual array? So here we have two arrays one for the first student and another one is for the second student, and here we can specify how many elements we want in each of that arrays. We want to store six values, you know the marks of six subjects that’s, why it’s going to be six here and then a semicolon. So this is the declaration of a two dimensional array. Now, if you guys want to initialize this two dimensional array right here, you know when you declare it, then you guys need to write equal to the sum int operator.

Then the curly braces I’m going to align it such so that you know it will be very easy to understand, and here we need to specify each array, elements separately, so I’m going to use another pair of curly braces and add a comma and another pair. I think we got more real, all right now, its proper. So here we are initializing the values for our multi dimensional or the two dimensional array marks. So in this first pair of curly braces, we can write the values of the first student, so let’s say: 35. 45, 85, 95, 75 and 25, and in the next or the second pair of curly braces, we can write the marks of the second student let’s, say: 78. 98. 6241. 3664. So now we have a two dimensional area marks, which is containing the marks of two students. So the next thing is how to access the elements store in this two dimensional RF, it’s, very similar to accessing the values of a single dimensional array. So, first we need to write the RA name, it’s going to be marks and then the index, whether we want to access the marks of the first student or whether we want to access the values of the first array or whether we want to access the value Of this second array here we want to access the value of the first RA, and all of you know that the first item will be present in the 0th index that’s.

Why I’m going to write zero here and again, the next square bracket, which is used to access which element you want to access in that array? Here I want to access this third element and this third element is present in the second index: zero one, two that’s: why I’m going to write 2 here and let’s? Add the semicolon that’s it. Let me see whether I can draw this all right now. These are the two arrays that we have. You know the first one and the second one. The first error is containing the marks of the first student, and the second error is containing the marks of the second student. So the first array is present at the position. 0 of this multi dimensional of the two dimensional array marks and the second array is present at the position 1 or the index 1. So these are the marks or the elements stored in this first array. So here the first element is present in the 0th index of the first array and similarly the last element is present at the 5th index and 4 that’s secondary. Also. The first element, 78 is present in 0th index and the last element is present at the 15 index. If I wanted to access the mark of this first student and third subject this one and this first students marks is present in the 0th index that’s. Why? First, I need to write the our a name here marks and which array first array, because I want to access the marks of the first student.

The index is 0 and then which element I want or which subjects Mottola I want. I want the third subjects mark which is present in the second index: 0. 1. 2 that’s. It all right now, we’re going to use a see out here and just print it out I’m going to build and run this. So it is, can see we got 85 and the next thing is, while initializing the multi dimensional array in C. You should specify these dimensions here and if you want to initialize this arrays, not while defining it to do that, you need to write the marks. Then the which array, the first one and then you know the dimensions which we used just like here and as I meant operator and the value, let us say: 0 amp, 2, all right I’m going to save it, build and run 10. So this is it guys. This is about some information on the multi dimensional or the two dimensional arrays in C. Thank you for watching and don’t forget to my channel, like my facebook page at facebook.

# arduino 2 dimensional array Social

Originally posted 2016-08-07 09:23:33.

(Visited 276 times, 1 visits today)

## Comment (26)

1. Rajeev Mehta says:

what if i want to print both the array ?

2. The Tarboz says:

very nice

1. LearningLad says:

Thank you 🙂

3. Paul Smith says:

Thank you so much Learning Lad!!

Also, I did this…please tell me if it is better doing this way or any other advise

#include

using namespace std;

int main()

{
int marks  = {
{25,53,35,4,54,45},
{15,96,45,65,42,51}

};

cout < < "-> marks” < < marks; cout << "-> marks” < < marks; cout << "-> marks” < < marks; return 0; } Results: -> marks42-> marks4-> marks65YTHENs-iMac:~ YTHEN\$

1. LearningLad says:

good. experiment with your code 🙂

4. Romel Durrani says:

Anil thank you bro

5. Kunal Nalawade says:

How to initialize an array if it has a large number of elements

6. Pranit k says:

what if we dont use = after 

7. network 99 says:

You mentioned how the first bracket determined the number of arrays, how could we label each array?
Array  = “Chlorine levels”;
Array = “Cyanauric acid levels”;
Would this work?

8. noornisha maheen says:

how are u getting that pencil pls tell me

9. 3mroo gamer says:

The code turned ‘ too many initializers’ error msg

1. LearningLad says:

checkout your code with the source code of this tutorial which is available in my website.

10. Youssef El-Shabasy says:

ty

11. Shahezad Ali says:

Why you not make videos on hacking

12. Dipti Kulkarni says:

Is there any way for the user to select which element he/she wants to view?

1. Bhoomi Ki Pathshala says:

yes cout << arr; or whatever you want to view

13. Yash says:

Array = Arre
N= yenn
Nice pronunciation btw.

14. Rekha Jain says:

sir I have one doubt you don’t use for loop in array but we are using for loop in array

15. Rekha Jain says:

Can we take value of array to be saved through if yes then how?

16. Utkarsh Dewan says:

How to initialize thr array using for loop??

1. Bhoomi Ki Pathshala says:

for( int i =0; i< n;i++) { cin>> arr[i];
}

17. END1254 says:

Hey if I wanted to multiply a 2 dimensional array by a value like 10 that has 3 rows and 4 columns with initial values present in each location, why would I set the “setw(5)” with the 5 argument instead of a (4) argument in this manipulator function.

18. AKSHAY DESHMUKH says:

How to store user input data into 2D array?

19. Muhammad Ali says:

Thanks sir

1. LearningLad says:

my pleasure 🙂

20. Naira singh says:

#include