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

C++ with Barney! Class 8

1201

Arrays

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

You: Hi, Barney!

Barney: Hi, Ted! I was waiting for you.

You: Great! What shall we be doing today?

Barney: Now that we’ve almost done with functions, let’s turn to arrays today. Suppose you have to store the marks of hundred student in your program. What would you do?

You: Mmm, let’s see. Okay, I have to store them in variables.

Barney: And how much variable do you need?

You: Oh, I need hundred, don’t I?

Barney: Exactly. But is that method fine?

You: Oh, no! Declaring hundred variables is tragedy!

Barney: Certainly, and that’s when arrays come to the rescue. In short, you can store many values inside a single variable by making the variable an array. Suppose we define an array called marks with a length of 100. Then this array can handle 100 values, and we call these marks[0], marks[1], up to marks[99].

You: And what about marks[100]?

Barney: This array has 100 elements and it starts from 0. So it should end at 99. In other words, every array starts with 0 so if its length is n then its last element should be n-1.

You: Ah, okay, I get it. So this array can hold hundred values of any type?

Barney: No, when we declare an array we must specify the data type its going to hold too. Let’s see this in a small program.

You: Hmm, defining an array seems to be so complex!

Barney: No, of course not! Actually we have done several things in one line, that’s why it may seem complex. To define the array with five elements, we’d have simply used

int marks[5];

But here, after defined that we have also assigned five values to the elements of the array. We include the values to be assigned to the array inside curly brackets, and these elements should be separated by commas.

You: Ah, right, now it’s clear.

Barney: Okay, so what do you think the program would output?

You: Mmm, marks[1] should mean the second value in the array, so it should print 56.

Barney: Yes, let’s see.

Good! Let’s expand our program to see the real benefits of using arrays. Suppose we want to take the sum and the average of the marks. How are we to do that?

You: We should add the numbers and divide by 5.

Barney: Yes, but this becomes quite easy when using arrays. We can use loops to manipulate them, you see?

You: How?

Barney: Let’s write a program and find out:

You: Ah, yes, I see. You can address the whole array in a single loop.

Barney: Exactly. When array operations are combined with loops the whole thing becomes quite efficient. Now let’s see what the output is.

You: Oops! The average is not right, is it?

Barney: Yeah, and why do you think it has gone wrong?

You: I have no idea.

Barney: Well, consider the statement avg=sum/5; Here, the variable avg is a double but sum is an integer. So when the program is run, the statement sum/5 evaluates to an integer. This can be corrected by casting the variable sum to a double. We will learn more about casting some other day. Until then, just keep this in mind. We have to change that line to:

The variable sum is casted into a double by adding (double) before it. Now let’s see the output:

You: Ah, now it’s okay.

Barney: Yes. I’ll teach you about casting another day.

You: Okay, and now I have to be going. Have to pick my sister from a class. Thanks Barney!

Barney: No problem, Ted. Bye!

You: Bye, Barney!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply