-
What's up, everyone? Welcome to
your—uh, it's like, 15th or somethin'—
-
tutorial on arrays. And the
last one was on arrays,
-
but I promised that I would build
an application to show you guys
-
why arrays are actually useful,
so that's what I'm going to do now.
-
So, all you have to do is follow along,
and open your mind, and... just learn.
-
The first thing that I'm gonna do
is build an array, just like last time.
-
Let's put five elements in it.
-
And let's go ahead and fill this
array with some kind of elements.
-
Let's say, the ages of...
my brothers and sisters.
-
And... I don't have five
brothers and sisters, so...
-
We'll go 21 for me, 18, uh... let's say,
pretend I had one that was 47.
-
Another one that was 21.
That's my twin, named Mucky.
-
And another baby sister named 4.
-
So that's one, two, three, four, five.
-
So remember, the position
number for each of these
-
is zero, one, two, three, four.
-
And you could have these any
numbers you want; it doesn't matter.
-
The next thing I'm gonna do is go
ahead and build a counting variable.
-
And in case you guys don't know—and
by "case," I mean you guys don't know,
-
because I didn't tell you yet.
-
This application is just gonna add
up all the numbers in the array for us.
-
So, we're gonna be building a
"for" loop later with a counting variable,
-
so that's why I need that.
-
And the last thing I need is a variable
to keep the sum of the total,
-
or, pretty much, the sum or
the total; however you say it.
-
So now that we got our
array in two variables,
-
one for the counting variable
and one to keep the sum,
-
let's go ahead and build a for loop.
-
Now, our first condition is,
I need to start at zero.
-
And this is because the
first position number of this,
-
is gonna be zero,
and it's gonna go to four.
-
So let's go ahead and add our
next condition, the maximum.
-
"i" is less than five.
-
And we have less than five
and not equal to five because
-
it's always gonna be
less than five, which is four.
-
If we put less than or equal to,
it's gonna try to get that fifth number,
-
and zero, one, two, three, four—
-
there is no five; it always
goes from zero to four.
-
Not always, but in this case.
-
And now, let's go ahead.
"i++."
-
Since we wanna just loop
it through one at a time.
-
Don't get anything fancy.
-
So, let me go ahead and—
Come on. Format.
-
Whatever. Do this the hard way.
-
So... Wow, this is a pain
in the butt right now.
-
So, next, we have to write
what we want our program to do.
-
So let's go ahead and take the total.
-
And right now, the total is zero.
-
So what we wanna
add to it is, loop through.
-
Let me—let me type. I can't, uh,
think and type at the same time.
-
And that is our program right there.
-
So, what this is gonna do
is it's gonna go, all right.
-
You now have the variable i=0.
-
So we're gonna take the zeroth-
position number, which is 21,
-
and add it to the total.
-
Next, we're gonna loop through
when i equals one.
-
So when i equals one, we're gonna
take 18, and add it to the total.
-
So whatever 18 +21 is,
and that's gonna be the total.
-
Next, it's just gonna
loop through all of that
-
until we get through
all of these, right here.
-
So, just to output something on the screen
to prove to you guys that it worked.
-
Go ahead and printf.
-
Did I get that right? Yep.
-
Uh... let's go ahead... how
are we gonna format this?
-
"The total number is..." And I know
I'm spelling everything wrong.
-
I know I spelled "number" wrong, so don't
comment a thousand times tellin' me.
-
"%d." Let's go ahead
and add a new line.
-
No, we won't. Just kiddin', sucker.
-
"total." So, let me just run this real
quick, and then I'll explain to you guys
-
exactly what it did one last time, so
you guys aren't totally lost in the dark.
-
So it's gonna loop through, and it's
gonna say the total number is—
-
again, "numebr," is 111.
That's exactly what we wanted.
-
Now, one last time, let me explain
to you guys how, exactly, this worked.
-
The first thing we did is build
an array with five elements in it.
-
Now, those arrays are listed
zero through four,
-
just 'cause your computer's weird.
-
Next thing we do is go to
a counting variable,
-
and build a variable to hold
the sum of our numbers.
-
We started with a zero-with
element in our array,
-
and looped through until
we got to number four.
-
As you can say, "i" is less
than 5, which is 4.
-
And this is just the
loop-through one at a time.
-
We then took each of those elements
and added it into the total variable,
-
so it looped through each time,
and each time it did,
-
it added a new number
to the variable total.
-
And at the end—you guys
should know this by now.
-
What we did is simply
print out that number.
-
So again, that is one way that you
can use a loop to loop through
-
the elements in your array,
and this is just to show you guys
-
that array isn't totally useless, and
you can actually use it for something.
-
So, this is a very simple program.
I encourage you guys to build this.
-
Again, I wanna thank
you guys for watching.
-
Don't forget to subscribe to my channel.
-
Again, thank you, and make sure
to check out my next tutorial.