declare

C Programming Tutorial - 14 - Intro to Arrays



Sharing buttons:

what's up everyone its Bucky welcome to

your 14th seed tutorial in this tutorial

I'm going to be teaching you guys about

an array now what an array is it's

pretty much a way that you can store

multiple items of the same type in your

computer or in other words this might be

a little easier to understand you can

store multiple values in a single

variable so um you might not be able to

grasp a concept yet but I promise you by

the end of this tutorial you'll be an

array expert so the first thing that you

need to do again just like a normal

variable is define it so go ahead and

type whatever value you're going to be

returning or going to be storing it I'm

going to be just working with numbers so

I'm going to go ahead and write int for

injured next you're going to need to

name your array give it any name you

want I'm going to name mine Bucky now

the next thing you do is a little

different than uh finding a single

variable you need square brackets and

inside the square brackets you have to

type whatever number you're going to

want to reserved in your memory now

since I'm going to be storing five

different values in this array I'm going

to go ahead and write the number five

and what that does is tell your compiler

in your computer alright we're going to

have five numbers so go ahead go into my

memory and reserve five spaces for this

array so it's going to go okay done the

next thing that we're going to want to

do is assign variables or assign values

to the elements in this array now

although we have five different elements

we just built in Bucky we don't start

with the number one and go to five your

computer since it loves zeros and ones

it's going to start with the value 0 and

go 0 1 2 3 4 so 0 2 4 are the elements

you're going to be able to work with not

1 2 5 so your maximum is always 1 less

than whatever you define and I'll show

you guys what this means later but to

store different values in each of the

elements what we need to do is do just

that so this is how you do that go ahead

and write the name of your array and

this is just for assigning values then

since zero is our first element we need

to assign a value to this so go ahead

Bucky zero equals and we'll just put 16

and we do this for the entire list of

our array so Bucky one equals a 21 now

let's just go ahead and make a simple

print statement to make sure that this

is working all right / f % D might as

well put a new line for formatting and

go ahead and instead of your variable go

ahead and pull whatever element in their

rate is so Bucky zero now let's go ahead

and compile and run this and we should

get the number 16 come on come on no

whammy no whammy right there 16 but what

happens if we um try to print out an

element that hasn't been assigned a

value yet such as 3 well I'm not even

sure myself so let's go ahead and test

it out got feeding my curiosity you know

ooh that's interesting that might be a

location memory I don't know but we

don't want to do that so what you want

to do is before you go ahead and do it

all that you want to go through your

array and make your entire list and

assign values to each of those numbers

now

you can either do that Bucky zero Bucky

one way to Bucky three Bucky four and

that will give you your five but there's

a much easier way to do this and I'm

going to show you guys that right now

instead of doing one number by one

number what you can do is define the

values right when you're defining your

array so put Bucky five equals and then

in your curly braces all you have to do

is type in five values so let's type in

21 13 4 30

five fifty six and seven and what this

is going to do is assign the element

number zero one two three four five in

this is also called the position number

because it's what position in the array

it is so Bucky zero is going to equal

twenty one Bucky one is going to equal

13 Bucky to four thirty five 356 four

seven and remember even though this is

five that just means there's five

elements it doesn't mean you need five

or your position number doesn't go to

five so again when you define an array

and have this is how many um spaces in

memory is going to take up but remember

your position number starts at zero so

it's always 1 less than that now let's

say we want to print out a Bucky three

so again let's print that out and it's

going to go well here's Bucky zero

here's Bucky one two three so we're

going to get the number 56 so let's go

ahead and see that right there come on

come on no whammy no whammy right there

56 and this is how you can store

multiple values in a single variable and

go ahead and experiment with this and um

another thing I want to teach you guys

one last time is if you're making a list

and you want to find out how many

elements you have but you don't exactly

know how many you're have maybe you have

the user input a certain number of

things and you don't know how many it's

going to be you can leave this number a

blank in when defining your array and

what this will do is tell your computer

are right I don't know how many numbers

were going to have but I do know we're

going to have an array so just reserve a

bunch of space and then let's make a

couple more numbers here and then what

your computer does is automatically

that number and it pretty much just does

everything automatically for you that's

all you need to know for right now so

let's go ahead and print on a number

like a seven so we'll go 0 1 2 3 4 5 6 7

should get the number 32 so let's go

ahead and do that

come on 32 32 BAM right there

so again in conclusion that is your

intro to array and I know it might seem

still kind of confusing but I promise in

the next couple tutorials we're going to

be building some simple programs and I'm

showing you and I'll show you why in

array is actually useful and not just

have to have fun with so again thank you

guys for watching hopefully understood a

little bit about arrays but I'll promise

I'll be clearing this up for you in the

next couple tutorials so again thank you

don't forget to subscribe and I'll see

you guys next time