Home Classroom C++ C++ with Barney! Class 9

C++ with Barney! Class 9


Arrays – Part 2

[You're Ted and you decide to learn some C++ from your friend Barney. This is the ninth day of the class]

You: Hi, Barney!

Barney: Hi, Ted!

You: So what are we going to do today?

Barney: Last time we talked about arrays, right?

You: Yeah, arrays.

Barney: And we didn’t talk about multidimensional arrays?

You: Mmm, nope.

Barney: Well, then, what do you think multidimensional arrays are?

You: Arrays with more than one dimension?

Barney: Exactly. The arrays we talked about last time had only one dimension. That is, they go only in one direction. But we can have arrays with several dimensions. We have to use two dimensional arrays in most of the programs. A two dimensional array is just like a table, it has rows and columns.

Suppose we want to store prices and taxes of several items in our program:

Price Tax
Item 1 120 12.5
Item 2 200 17.5
Item3 60 17.5
Item 4 150 7.5

How would you store these if you use the arrays like we did last time?

You: Mm, we can make one array with prices and another array with taxes.

Barney: Right. If there were more columns in the table, we’ll have to create more arrays. This is where 2D arrays come to help. Let’s see how we can declare a 2D array in the above scenario.


double data[4][2];


Here, data is the name of the array. The next two arguments tell that it has 4 rows and 2 columns.

You: Ah, I get it. But how can we assign the values to the array?

Barney: It’s like this. You assign the values row by row. We have 4 rows here. We consider them as 4 separate arrays, and we join them to build a 2D array like this:


double data[4][2]={{120,12.5}, {200,17.5}, {60,17.5}, {150,7.5}};


You: I see. How do you access them afterwards?

Barney: It’s simple. If you want to access the tax of item 3, that is third row’s second value, we can use:




You: 2 and 1? Not 3 and 2?

Barney: Remember, arrays, as we talked last week, are zero-based. The first element is zero, the second one, and so on.

You: Ah, yes, I forgot. I understand now. So if you want the first row’s first column you may enter data[0][0]?

Barney: Exactly. Let’s write a small program to calculate the final price of each item:

Can you understand what the program does?

You: Mmm, it first declares the 2D array and assigns values. Then there’s a loop which will run 4 times from 0 to 3. In each turn, it computes the final price from the data in the array and display them. Is that it?

Barney: Exactly. Let’s run and see the output.

You: I see.

Barney: You can use for loops to process the elements in arrays. When we use loops to manipulate 2D arrays, we need to put one loop inside another, like this:


for (int i=0; i<5; i++) { for (int j=0; j<12; j++) { // //Do sth with array[i][j] // } }


We call a loop that lies inside another a loop, a subordinate loop. With such loop structures, manipulation of large arrays become real easy.

You: Hmm. So we can make arrays with more than two dimensions?

Barney: Definitely. We come upon such arrays from time to time. But 2D arrays are the most popular.

You: Okay.

Barney: Now let’s write a program that actually uses arrays. Suppose you want to let the user input several integers and find its maximum. We write a program like this:

Now, Ted, try to explain the program.

You: Okay. First we declare an array called marks with length 5. Then we run a loop to let the user enter values to that loop one by one. Then..mm..

Barney: Yes, we declare a variable called max and assign the first value in the array to it. What the next loop does is it iterates over the other elements of the array to find values greater than the current value in max. When it lands upon such values, it assigns that new value to max so that it can check for higher values.

You: I see, I see. At the end of the loop, the variable max should contain the maximum value in the list. Fantastic!

Barney: Yes, let’s run the program and see.

You: Yeah, works the way we expected.

Barney: In fact, to get the maximum use of any programming language, one has to master the concept of arrays. And the best way to do that is to write many programs by yourself using arrays. So I think that’s it for today. See you next time!

You: Okay, I’ll try to write more. Thanks, Barney! Bye!

Barney: Bye, Ted!



Engineer. Loves tic-tac.


Leave a Reply