 Νέο: ενεργοποιείστε μεταφράσεις και λεζάντες που δημιουργήθηκαν από θεατές στο κανάλι σας στο YouTube!

## ← C Programming Tutorial - 15 - Simple Array Program

• 2 Οπαδόςs
• 94 Γραμμήs

### Πάρτε τον Κωδικό ενσωμάτωσης x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=BqU3_ouKHwk" data-team="null"></div> ``` 1 Γλώσσα

• Αγγλικά [en] αρχικό

Showing Revision 3 created 12/05/2019 by jmmartin.

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