Write a program in 'c' to calculate the sum of the corresponding elements of two arrays of integers of same size.
Answers
Answer:
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() */
Answer:
Explanation:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void main()
{
int i,n;
int *a,*b,*c;
printf("how many elements in each array\n");
scanf("%d",&n);
a=(int *)malloc(n * sizeof(int));
b=(int *)malloc(n * sizeof(int));
c=(int *)malloc(n * sizeof(int));
printf("enter elements of first list\n");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
printf("enter elements of second list\n");
for(i=0;i<n;i++)
{
*(c+i)=*(a+i)+*(b+i);
}
printf("resultant list is\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(c+i));
}
}