Course Content

Course Content

Accessing Array Elements

Hello everyone, my name is (name) and we are continuing with this C programming course in which we have learned in the previous video, arrays.

We had seen what is the need of arrays in C programming.

After that we saw how we can declare the arrays, initialise them with different values.

So, all these things we saw in the previous videos.

So, we learned that in arrays basically we make the same type of variable chain and one by one we store the values in them.

Now after understanding and learning these things.

Now, we are going to see with the help of this video, we have stored the values in the array but how should we use that value.

One particular value like we said that one chain of variables is made.

 And we don't have to give a lot of names to each and every variable.

Rather than that we give one index position and with the help of index position, the value that is there in the particular variable, we either insert it or get it extracted. So, with the help of index value, we basically conduct the operations in arrays.

Here we are going to see in which way on the basis of index value, how we can extract these things from the array.

So, we are seeing this.

That is why we are going to talk about how to access elements from the array.

Here if we talk about it, “an element is accessed by placing the index of the element within the square brackets after the name of the array.”

Now, we had seen one more thing in the last video.

When we do the declaration of an array, which means before the name of the array we are writing its datatype, at that time our square bracket defines the size of the array.

But without any data type if we’re using the array’s name somewhere.

So, with the name of the array, our square brackets come and inside that we denote the index position.

So, if the data type is along with it, what we will define, we will define the size of the array.

And if the data type is not along with it, only the array’s name and square brackets are there.

So, the value that would be written in the square bracket, we would be actually pointing at some index position.

So, this was our basic concept and, in this way, basically, after writing this index position in the square bracket, we can extract the element from the array, we would be able to access it.

So, here for an example we will see, income is our normal variable, of double type.

What I have done is, from an array named balance, the value which was on the ninth index position, the value which was stored on the ninth index position, I have basically transferred to the income variable.

So, in this way we extract the values from the arrays, and we are able to access it.

Now, to understand it properly, we will see how these arrays can be practically used and we will straight away go towards our text editor and we will see that the simple array program which is there, in that the declaration or initialization that is there and the part of accessing the element which is there, how does that work.

So, now this is a simple program, where we will use the arrays.

 What are we doing in this? We are not doing much in this.

We will get the statement printed and we will show that on which index position, which value has been stored with the help of our print F statements.

So, we will quickly go on to our program.

In the program, the first statement is our preprocessor command, the second is our main function, the main function which starts in the second line, if we come inside that you can see here, along with the variables you can see one array named N, which is initialised or declared.

In front of which we have put a square bracket and the value inside the square bracket, that is basically the size of our array because we have just discussed whenever we put data type before the name of the array, what happens at that time? The value in the square bracket represents the size of the array and not the index value.

So, here the size shows that basically the array's size is 5.

So, in those five variables or five blocks would be there and we can store 5 values.

So, this is about our array declaration.

So now we will go ahead, we will see that we have made one for loop in which we are initialising I with zero and this for loop is going from where to where? That is going from 0 to less than 5.

Till the time its value would be less than five, till then the for loop will go on.

So, now we will come inside for a loop.

And at the start the I’s value would be zero because we have initialised it.

So, let's suppose we have come now, suppose we are compilers and we have come inside the for loop.

For now, the I’s value is zero.

What will happen here? We have to pay attention here.

You can see that N is our array basically and before that we have not written its data type.

So, in this square bracket whatever we will write, it will denote our index value.

So, here we have written the variable So, it will basically consider the variables value inside.

So, the variable is I.

So, I as we know, it is 0 the first time.

So, basically what is it doing, on the N’s 0th index position, whatever value is written ahead,

it is getting stored.

What is the value that is getting stored? I’s value was already 0.

In that we added and stored 100.

This entire statement is an array initialization statement.

And in this you can see that for the first time on the index position 0, we have stored this value.

So, when we again went up to the loop and the condition of the loop became true, when our I’s value was 1.

We will come again in the loop.

And this time on the first index position of our N array, we will be inserting 1+100.

In the same way till the time our loop doesn't run.

Till that time our index value will keep on increasing and even here the value will keep changing and it will keep getting stored inside.

So, this part we call as array initialization, before this we have seen array declaration.

After seeing both these things, now comes the time to know how we extract the value from these, we even have to get it printed with the help of printf statement.

So, how will we access it? We'll explore that part.

So, we will come down, and we will see that we have made one more for loop.

So, in the first for loop we were using the variable I and this time we are making one more for loop where we are using the variable J.

 So, even this works in the same way.

At the start, J’s value is 0.

So, it has been initialised with 0.

And it will go on till less than five conditions and there is an increment operator.

So, it will happen in this way, it will be zero first, then 1, then 2, 3, 4 and it will not work in the fifth one because five is not less than five, five is equal to five.

So, in this condition this will not work and in the other condition it will go on working.

And here when the J’s value would be zero.

So, we will see with the help of a printf statement what will get printed.

So, you will see that a few statements are written here, elements and then in square brackets we have put a %d or a format specifier.

And here if you check the corresponding variable, first with the %d, we are printing j.

So, when the J’s value is 0, here in the element’s square bracket 0 will come and what this element basically shows is, array’s element on 0th position, basically it is printing that.

Which position would be on the 0th position? That we

will show with the %d.

So, here in %d, what is it actually printing? N which was our array, it will print which index value of that? J’s index position, in J, first bar is 0.

After that the J’s value will increase and become 1.

In this way these things will be changed.

In this way with the help of loop one by one all the values from our array will be accessed.

And with the help of printf statements those will be printed.

So, with this particular process, we will get to know from this process that on which index position, which value has been stored.

So, you can see the loop once again properly, the first loop was of array initialization, which means storing the value in the array.

The second for loop that we saw, it is accessing the value, which means in which way the saved or the stored value in the array, how will we extract or access the value.

That method, we have shown in the second for loop.

So, this is our basic program related to arrays.

We will run it and see.

With Ctrl+S, it was already saved.

So, I will straightaway right click on it and I will run the code.

Here if we are running the code, you can see that my printf statement, it has been printed five times because we had put it in the loop and in the loop the value is starting from 0 and it will go on till the time it is less than five, which means 0, 1, 2, 3 and 4.

It means that it will run 5 times.

So, we have printed this statement five times.

So, in the first statement, you will see that we have printed J in the square bracket and when the J was 0, in that particular index value which we had stored, with the help of our array that has been stored as 100.

At the time of initialization, you can see that when the value of I was 0.

0 means 0+100.

It remains 100.

When the I’s value became 1, it became 1 plus 100, it became 101 and then 101 got printed over here.

So, in this way we will see on the 0th index, 100 is the value.

In the first index it is 101, on second it is 102, third it is 103 and on the fourth 104.

In this way, we complete our declaration, initialization and accessing element’s part inside one array.

And we take help of for loops.

So, with this you also get to understand why we learned for loops.

Because we have to this many times that we have to print one statement constantly based on the condition, that's why we are using this particular loop.

So, with this we understood that and how we declare initialise and access our elements.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.

The next topic that we will discuss will be called various types of arrays.

Going ahead we will see in which ways we can use these arrays.

So, we will see in the coming videos, what are the various types of arrays.

So, till that time you keep on practising arrays and we will meet in our next video.

Thank you so much.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere