Array [Part II]
By programmer on Jul 20, 2009 in Array and tagged C and C++, C and C++ Tutorials, C for fun, C Language, c programming, C Tutorial, Initializing and Declaring an array, Introduction to C, Learning C, Let us C, Programming Codes, Programming in C, The C blog
Array [Part II]
In the previous post we have learned to declare and initialize values in an array, in case if you have missed the previous post you can get back to that post by just clicking on this link –
http://programmer.mynewblog.com/2009/07/18/array-part-i/
We can perform arithmetic operations with arrays just like we did with the variables. We can add, subtract, divide and multiply the elements of an array.
These arithmetic operations are not constrained only within the elements of a single array but can also be done with other arrays. That is the elements of two different arrays can also be added, subtracted, multiplied and divided.
Now let us see some of the program codes related to the arithmetic operations of an array.
Example 3:
Write a program to initialize ten numbers to an array named a. Then find the product of all the elements present in that array. The ten numbers which you have to initialize are 15, 4, 9, 21, 12, 7, 19, 1, 33, 28. Display the product after finding the product.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10]={15,4,9,21,12,7,19,1,33,28};
int i;
double pro=1;
clrscr();
printf(”The numbers you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
for(i=0; i<10; i++)
pro=pro*a[i];
printf(”nnnn The product of the number is %.0lf”,pro);
getch();
}
Explanation:-
The ten numbers are initialized in the array in the statement –
int a[10]={15,4,9,21,12,7,19,1,33,28};
pro is the variable where the product of all the elements are stored, the initial value of pro is initialized as one. The statement
pro=pro*a[i];
is used to find the product of all the elements. At every iteration of the loop a new element is multiplied to the product. The loop runs for 10 times and thus all the 10 elements are multiplied. A figure is provided just below for understanding the operation. The diagram of the array a is given along with the sequential elements which are stored in the array.

Example 4:
Write a program to enter the marks of 10 students from the user. Then find and print the average marks of the ten students.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10];
int i,add=0;
float avg;
clrscr();
printf(”Enter the marks of the students n”);
for(i=0; i<10; i++)
scanf("%d",&a[i]);
clrscr();
printf(”The marks you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
for(i=0; i<10; i++)
add=add+a[i];
avg=(float) add/10;
printf(”nnnn The average marks of the students are %.2f”,avg);
getch();
}
The input of this program will be –
Enter the marks of the students
70
64
88
91
75
82
56
94
66
45
The output of this program will be –
The marks you have entered are –
70 64 88 91 75 82 56 94 66 45
The average marks of the students are 73.10
Explanation:-
This program first accepts the marks of 10 students from the user, then it adds all the marks present in the array. The statement
add=add+a[i];
is used to add all the elements of the array. Then to find the average marks the variable add is divided by 10. The average marks is stored in the variable avg, which has a float data type.
Example 5:
Write a program to accept 10 numbers from the user and find the sum of all even numbers and the sum of all odd numbers of the array.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10];
int i,odd=0,eve=0;
clrscr();
printf(”Enter the numbers in the array n”);
for(i=0; i<10; i++)
scanf("%d",&a[i]);
clrscr();
printf(”The numbers you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
for(i=0; i<10; i++)
{
if(a[i]%2!=0)
odd=odd+a[i];
if(a[i]%2==0)
eve=eve+a[i];
}
printf(”nnnThe sum of odd numbers is %d”,odd);
printf(”nnnThe sum of even numbers is %d”,eve);
getch();
}
The input of this program will be –
Enter the numbers in the array
11
12
16
21
25
31
42
43
19
24
The output of this program will be –
The numbers you have entered are –
11 12 16 21 25 31 42 43 19 24
The sum of odd numbers is 150
The sum of even numbers is 94
Example 6:
Write a program to accept 10 numbers from the user and store them in a single dimensional array, then delete the number which is present —
1. At the end of the array.
2. At the starting of the array.
3. At the 6th position of the array.
Delete the last number of the array
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10];
int i;
clrscr();
printf(”Enter the numbers in the array n”);
for(i=0; i<10; i++)
scanf("%d",&a[i]);
clrscr();
printf(”The numbers you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
a[9]=NULL;
printf(”nnnn The new array after deleting the last number is n”);
for(i=0; i<9; i++)
printf("%d ",a[i]);
getch();
}
Explanation:-
In this program we have to accept 10 numbers from the user and then we have to delete the number which is present at the last of the array. To delete an element of an array we initialize its value as null. The last number of the array is stored in a[9]. Thus we have initialize it as null in the statement
a[9]=NULL;
Now after deleting the last number we just print out the new array, that is the remaining 9 elements.
Delete the first number of the array
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10];
int i;
clrscr();
printf(”Enter the numbers in the array n”);
for(i=0; i<10; i++)
scanf("%d",&a[i]);
clrscr();
printf(”The numbers you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
for(i=0; i<9; i++)
a[i]=a[i+1];
a[9]=NULL;
printf(”nnnnThe new array after deleting the first number is –n”);
for(i=0; i<9; i++)
printf("%d ",a[i]);
getch();
}
Explanation:-
Here also we accept 10 numbers from the user, then we have to delete the first number of the array. The first number of the array is stored in the zeroth position a[0]. Now for deleting or removing the first number we are suppose to place the next number of the array to its position, because the zeroth element of an array should not be NULL. In short the number which is present in the first position is shifted to the zeroth position, and similarly the number which is present in the second position is shifted to the first position, and so on. The statement
a[i]=a[i+1];
performs this function. As this statement is inside a loop, thus all the nine elements of the array will shift one position ahead. Now let us see the dry run of the loop –
After first iteration –
a[0] = a[1];
The value of a[1] is copied to a[0].
After second iteration –
a[1] = a[2];
The value of a[2] is copied to a[1].
After third iteration –
a[2] = a[3];
The value of a[3] is copied to a[2].
After forth iteration –
a[3] = a[4];
The value of a[4] is copied to a[3].
After fifth iteration –
a[4] = a[5];
The value of a[5] is copied to a[4].
After sixth iteration –
a[5] = a[6];
The value of a[6] is copied to a[5].
After seventh iteration –
a[6] = a[7];
The value of a[7] is copied to a[6].
After eight iteration –
a[7] = a[8];
The value of a[8] is copied to a[7].
After ninth iteration –
a[8] = a[9];
The value of a[9] is copied to a[8].
After shifting all the nine elements of array by one position ahead the loop is terminated.
Now in the new array all the elements would be present except the first number. You should also notice that the values present in the eight and in the ninth position are same because the element present in the ninth position is copied in the eight position. So we initialize the ninth element of the array as NULL,
a[9]=NULL;
it done to remove the extra repetition of the ninth element.
Delete the number present at the 6th position
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10];
int i;
clrscr();
printf(”Enter the numbers in the array n”);
for(i=0; i<10; i++)
scanf("%d",&a[i]);
clrscr();
printf(”The numbers you have entered are –n”);
for(i=0; i<10; i++)
printf("%d ",a[i]);
for(i=6; i<10; i++)
a[i]=a[i+1];
a[9]=NULL;
printf(”nnnnThe new array after deleting the number in 6th position –n”);
for(i=0; i<9; i++)
printf("%d ",a[i]);
getch();
}
Explanation:-
In this question we are supposed to delete the number which is present in the sixth position. We know, that in an array the elements are stored in sequence, now for deleting an element which is present in the sixth position we have to shift all the elements which were present after the sixth position. That means the element which is present in the seventh position is to be shifted to the sixth position, and the element which is present in the eight position is to be shifted to the seventh position, and so on. In short the elements after the sixth position are to be shifted one position ahead. This operation is performed by the statement
a[i]=a[i+1];
This statement is inside the loop and the loop runs from sixth position to the end. Thus all the elements after the sixth position would be shifted. Let us see the dry run of the loop –
After first iteration –
a[6]=a[7];
The value of a[7] is copied to a[6].
After second iteration –
a[7]=a[8];
The value of a[8] is copied to a[7].
After third iteration –
a[8]=a[9];
The value of a[9] is copied to a[8].
Now just to remove the repetition error; since the value of a[8] and a[9] are equal we just initialize the value of a[9] as NULL.
A figure is given below to show the array diagram when the elements at different positions are deleted.

So today we have learned about deleting elements in an array and also about performing arithmetic operations in an array. In the next post we will learn to perform arithmetic operations between two different arrays and we will also learn to insert an element in an array. So please keep a track of the next post



2 Comment(s)
Another good piece of work.Each piece of the coding is well explained with the help of words as well as pictorial representation.And I am considering it for granted that your next post will b a blast..
The concept for deleting the first number of an array is really explained well, thanks a lot.