RSS Feed for This PostCurrent Article

Array [Part III]

Array [Part III]

In the previous post we have discussed about arithmetic operations in a single one dimensional array and also about deleting an element from the given array. If in case you have missed that post you can go back to that post by just clicking on this link.

http://programmer.mynewblog.com/2009/07/20/array-part-ii/

Today we would learn about performing arithmetic operations between two single dimensional arrays. With every program code I have given an explanation and also a diagram for better understanding the program logic. If you still face any problem in understanding any code just submit your question in the comment box give below and I will try to answer it out for you.

Now lets check the program codes —

Example 7:

Write a program to accept 10 numbers from an user and then store them in an array a, then accept another 10 numbers from the user and store them in a second array b. Now add both the arrays and store the result in a third array c.

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf(”Enter the numbers in the first array \n”);
for(i=0; i<10; i++)

 scanf("%d",&a[i]);

printf(”\nEnter the numbers in the second array \n”);
for(i=0; i<10; i++)

 scanf("%d",&b[i]);

clrscr();
printf(”The numbers you have enterd in the first array are \n”);
for(i=0; i<10; i++)

 printf("%d  ",a[i]);

printf(”\n\n\nThe numbers you have entered in the second array are \n”);
for(i=0; i<10; i++)

 printf("%d  ",b[i]);

for(i=0; i<10; i++)

 c[i]=a[i]+b[i];

printf(”\n\n\n\n\nThe sum of the two arrays are \n\n”);
for(i=0; i<10; i++)

 printf("%d  ",c[i]);

getch();
}

Explanation:-
Here the user enters first 10 numbers and those numbers get stored in an array named a. Then the user enters another 10 numbers and those numbers get stored in an array named b. Now the compiler adds both the arrays and the result is printed in a third array named c.
In addition of two arrays, the two elements which are added have the same subscript. That means the zeroth element of array a is added to the zeroth element of array b and their summation is stored in the zeroth element of array c. In short the subscript of all the three arrays should be same. You can also see that in the statement

c[i] = a[i] + b[i];

The subscript of all the arrays is i, and thus for an iteration the subscripts are equal. This statement is written inside the loop. And as the loop is executed 10 times thus all the 10 elements of the array a and b are added; and their summation is stored accordingly in array c. Let see the dry run of the loop –

After first iteration –
c[0] = a[0] + b[0];
After second iteration –
c[1] = a[1] + b[1];
After third iteration –
c[2] = a[2] + b[2];
After forth iteration –
c[3] = a[3] + b[3];
After fifth iteration –
c[4] = a[4] + b[4];
After sixth iteration –
c[5] = a[5] + b[5];
After seventh iteration –
c[6] = a[6] + b[6];
After eight iteration –
c[7] = a[7] + b[7];
After ninth iteration –
c[8] = a[8] + b[8];
After tenth iteration –
c[9] = a[9] + b[9];

I have provided a diagram below showing both the elements of array a and b, and also the third array c is shown which is the summation of both these arrays.

adda

Example 8:

Write a program to initialize values in two arrays, then declare a third array c and store in it the difference between the two arrays.

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10]={15,4,9,21,12,7,19,1,33,28} ;
int b[10]={5,11,16,8,25,20,2,29,3,10};
int i,c[10];
clrscr();
printf(”The numbers of array a is \n\n”);
for(i=0; i<10; i++)

 printf("%d  ",a[i]);

printf(”\n\n\n\nThe numbers of array b is \n\n”);
for(i=0; i<10; i++)

 printf("%d  ",b[i]);

for(i=0; i<10; i++)

 c[i]=a[i]-b[i];

printf(”\n\n\n\nThe value of the resultant array c is \n\n”);
for(i=0; i<10; i++)

 printf("%d  ",c[i]);

getch();
}

Explanation:-
The two arrays are initialized at the beginning, then the difference between the two arrays are found out and it is stored in the third array c. A diagram is given below just to show the array elements. This program is almost similar to the previous program except the fact that in the previous question we have added two arrays and in this question we are finding the difference between the two arrays.

subs

Example 9:

Write a program to accept 10 numbers from an user and then store them in an array a, then accept another 10 numbers from the user and store them in a second array b. Now multiply both the arrays and store the result in a third array c.

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf(”Enter the numbers in the first array \n”);
for(i=0; i<10; i++)

 scanf("%d",&a[i]);

printf(”\nEnter the numbers in the second array \n”);
for(i=0; i<10; i++)

 scanf("%d",&b[i]);

clrscr();
printf(”The numbers you have enterd in the first array is \n”);
for(i=0; i<10; i++)
printf(”%d “,a[i]);
printf(”\n\n\nThe numbers you have enterd in the second array is \n”);
for(i=0; i<10; i++)

 printf("%d  ",b[i]);

for(i=0; i<10; i++)

 c[i]=a[i]*b[i];

printf(”\n\n\n\n\nThe product of the two arrays are \n\n”);
for(i=0; i<10; i++)

 printf("%d  ",c[i]);

getch();
}

Explanation:-
This question is mainly given for your own practice, like the previous two questions in this question also you just have to multiply two arrays. If you have understood the previous two questions then I would urge you to solve this question own your own first and then look for the solution. So that you can recheck your mistakes. A diagram is given below to show the memory representation of all the three arrays.

mula

Example 10:

Write a program to accept 10 numbers from an user and then store them in an array a, then accept another 10 numbers from the user and store them in a second array b. Now divide both the arrays and store the result in a third array c.

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10]={15,4,9,21,12,7,19,1,33,28};
int b[10]={5,11,16,8,25,20,2,29,3,10};
int i;
float c[10];
clrscr();
printf(”The numbers you have enterd in the first array is \n”);
for(i=0; i<10; i++)

 printf("%d  ",a[i]);

printf(”\n\n\nThe numbers you have enterd in the second array is \n”);
for(i=0; i<10; i++)

 printf("%d  ",b[i]);

for(i=0; i<10; i++)

 c[i] = (float) a[i]/b[i];

printf(”\n\n\n\n\nThe resultant array is \n\n”);
for(i=0; i<10; i++)

 printf("%.2f  ",c[i]);

getch();
}

Explanation:-
In this question we have to divide the elements of two arrays and the answer is stored in a third array c. When a number is divided by any other number the answer may be an integer or fractional. For storing fractional values the variable data type should be float, that is why we have declared the data type of array c as float. In the statement

c[i] = (float) a[i]/b[i];

we have divided the elements of the two arrays and the answer is stored in the array c in sequence. You can see that array a and b are integer type but array c is float type, since there is a mismatch of data types, thus we need to type cast all the data types into same type. Since the answer could be fractional thus we have converted all of them into float type. We have done this by explicit type casting, in short we have first converted the answer into float type and then this answer is stored in array c.
A diagram is given below which shows the elements and the memory representation of all the three arrays.

divide

Thus today we have completed all the arithmetic operations between two different arrays. In the next post we will learn to insert an element in a given array and we will also learn about sorting an array, thus please keep tract of the next post.

Add to Technorati Favorites



Software



Top Blogs

Trackback URL



  1. 2 Trackback(s)

  2. Jul 24, 2009: Array [Part IV] | C Program Codes
  3. Oct 12, 2009: Single Dimensional Array | C Program Codes

Post a Comment

Powered by WP Hashcash

  •  

    July 2009
    M T W T F S S
        Aug »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Meta

  • Ad Brite

  • Categories

  • Archives

  • IP