This is Anil from LearningLad and welcome to another video on C programming language.
Iin this video we will write a simple C program to add all the values stored in an integer
array and then we will display that result.
Here we will ask the user about how many values he wants to add then we will take the value
entered by the user and depending on that value we will create an array which can contain
that many values.
Then we will add all those elements values and then we will display the result.
so the first thing that we are going to do is we are going to create some variables.
the first variable that we need will be for storing the array size or we can say that
number of values that the user wants to enter and this will be our array size and the value
will be an integer number and variable name that I am going to give is numVal for number
of values and then we will ask the user about how many values he wants to add.
so we will use printf function and we will say how many values you want to add and then
whatever the value the user is gonna enter, we will read that and we will store it in
this numVal variable.
so we will use a scanf function for that and this will be %d for integer number and will
store that in our numVal variable.
ok
now we know how many values the user wants to enter or the array size.
now we are going to create our array.
so the array will contain integer values.
so the data type for the array is int.
let's give a name for the array.
I'll call it as values and the size of this array will be numVal or the value stored in
this numVal variable.
ok
now we have created an array.
The next thing that we need to do is we need to ask the user about the numbers that he
wants to add or the values that he wants to add and we will read that value and will store
that in this array values.
so for that purpose we need a loop and here we will use a for loop and in this for loop
we need a counter variable and I'm going to call it as loopcounter and this variable will
you know will point to the first element second element third element like that.
so we will use a for loop now.
The loopcounter will start from zero.
That’s because the array indexing start from zero.
The first element will be at the index 0 and then this loopcounter, so the number of values
the user wants to enter is stored in this numVal variable.
so here the condition will be loopcounter is less than numVal
Here we have less than that’s because the loop counter is starting from zero.
if it was starting from 1 then we will have the condition less than or equal to here.
ok
Then after that we will increment the value of our loopcounter variable so that with every
iteration we can point to the next element of the array.
so now we can display a message to the user saying that please enter the value %d and
here we will say loopcounter plus one.
So here it will display like please enter the value 1
Please enter the value 2 please enter the value 3
like that
Here the loopcounter for the first iteration will contain 0
When we do loopcounter plus one, it will display one for the first iteration and this statement
here which I have highlighted is not altering the value of this loopcounter variable.
OK
Now the next thing is the user is going to enter the value and what we are going to do
is we are going to read that value and we will store that in this values array.
we can directly read that value and we can store it in this values array but I want to
make this simple cause this video is for the beginners.
So i will create another variable here outside the for loop and I will call it as value which
will contain each individual value entered by the user.
So now after displaying the message about enter the value we will read the value entered
by the user and we will store that in this value variable first.
So its gonna be scanf function and here %d format specifier because we are reading an
integer value and we will store that in our value variable.
now here you can directly read that value to the values array.
But we are gonna simplify this as much as possible.
so after that this value variable will contain the value entered by the user and we will
store that in our array.
so we want to store that in this values array and here the position where we want to store
will be pointed by this loopcounter variable equal to value.
Ok
now here what we have done is we will ask the user to enter all that number that he
wants to enter and then we are reading those values and we are storing that in this array.
ok
now the next thing that we need to do is we need to calculate the sum of all these numbers
which are in this array.
so to store that value we will create a variable and we will call it as result and we will
initialize that with the value of zero
ok now here we will loop through all the elements
of the array and we will add that value to this result variable's value.
Ok
so we need the for loop.
so I am going to copy this for loop.
I just don't want to type it again.
now here what we want to do is we want to add the array elements the value to this result
variables value.
so what we can do is result equal to result plus, now we need to refer to the array element
that will be values array and the index will be in this loopcounter variable.
Ok
now here we are writing result equal to result plus values.
if you want you can simplify this and you can write result += values
now that statement will be exactly equal to result equal to result plus values array and
this index.
so first result variable will contain value 0 and then the values stored in this first
array element will be added to this result.
So now the result variable will contain the value of the first element of the array and
then we will add all other elements value and we will store that in this result variable.
ok
now the last thing that we are going to do is we are going to display the result.
so outside the for loop i’m gonna use the printf statement and I am going to say result
equal to %d and here the result will be in this result variable.
Ok
now we are gonna build and run this.
I just miss spelt this printf. should be printf and then will correct that
and build and run it.
This time it says how many values you want to add.
i’m going to say five values.
Enter the value 1 1
Enter the value 2 2
Enter the value 3 3
4 4
5 5
you can enter any any values and then if I hit the enter button it says 15.
So let’s count this.
5 + 4 which is 9 + 3 which is 12 + 2 which is 14 + 1 is 15
so the result is proper
now if you want you can run this program many times and give different values.
ok
now you have to make a simple modification to this code and that is I said that you can
directly store the value read from the user in this values array.
you don't have to use this value variable separately.
so you have to make that modification and you need post that code in the comment section.
I will look at your code and I will say whether it is right or wrong.
so try that out and if you like this to do hit the like button if you don't like it and
hit the dislike button and if you want to watch more tutorials then do subscribe to
the channel thank you for watching and I'll see you later in the next video.