Share on Google+Share on Google+

Addition of two arrays element in C

Advertisement
In this tutorial you will know how to get the sum of each element of an array. There are three function in this given example one for reading the array element, second for writing on console and last for addition of both array's element.

Code:

#include<stdio.h>
#include<conio.h>

void main()
 {
  void read(int *,int);
  void display(int *,int);
  void addarray(int * ,int *,int * ,int);
  int a[5],b[5],c[5],i;
  clrscr();
  printf("Enter the elements for first array \n");
  read(a,5);
  printf("Enter the elements for second array \n");
  read(b,5);
  addarray(a,b,c,i);
  printf("The sum corresponding element of both array is : \n");
  display(c,5);
  getch();
 }


 void addarray(int a[],int b[],int c[],int i)
{
 for(i=0;i<5;i++)
	{
	c[i]=a[i]+b[i];
	}
}
 void read(int c[],int i)
  {
	int j;
	for(j=0;j<i;j++)
	scanf("  %d",&c[j]);
  }

 void display(int d[],int i)
  {
	int j;
	for(j=0;j<i;j++)
	printf("  %d\n",d[j]);
  }

Output:

 

Download this code

Advertisements

Advertisement

Posted on: April 30, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.