Blogger templates

Pages

Friday 29 June 2012

WAP that Accepts Two Arrays and Compute the Sum of Corresponding Elements of Two Arrays

Sum of Two Arrays

Note: C does not support performing any operation on the entire Array. The whole Array cannot be processed as a single element. But, it allows the Programmer to perform certain operations on an element-by-element basis.

So, Let us understand this Note with the help of Example:

Statement of C Program: This Program accepts two integer Arrays and Find the Sum of the corresponding elements of these Arrays:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int b[10];
int sum[10];
int i , n;
clrscr();
printf(" Enter the size of Array A and B\n");
scanf("%d" , &n);


printf(" Enter the elements of Array A\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &a[i]);
}
 
printf(" Enter the elements of Array B\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &b[i]);
}

for(i=0 ; i<n ; i++)
sum[i] = a[i] + b[i];

printf(" Sum of elements of A and B\n");
{
printf("%d\n" , sum[i] );
}
}                                       /* End of main() */


Output:
Enter the size of Array A and B
3
Enter the elements of A
2
4
6
Enter the elements of B
8
10
12
Sum of elements of A and B
10
14
18

                                                                     That's All

0 comments:

Post a Comment