Return to Video

C Programming Tutorial - 15 - Simple Array Program

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

more » « less
Video Language:
English
Duration:
05:32

English subtitles

Revisions