Index-1              Index-2              Home Page

Array : Adding Two Arrays

/* Problem: Add the corresponding values from two arrays 
of the same size. */
#include <stdio.h>
 
/* the function that adds the two arrays a1 and a2. it "returns" a3 back */
void
addarrays (int a1[], int a2[], int a3[], int n)
{
	int i;

	/* do the adding of every corresponding cells */
	for (i=0; i<n; ++i)
		a3[i] = a1[i] + a2[i];
}

int
main (void)
{
	int x[] = {1,2,3,4}, i;
	int y[] = {10,20,30,40};
	int z[4];

	/* call the function */
	addarrays (x, y, z, 4);

	/* print a report */
	for (i=0; i<4; ++i)
		printf ("%3d", x[i]);

	printf ("\n + \n");

	for (i=0; i<4; ++i)
		printf ("%3d", y[i]);

	printf ("\n-------------\n");

	for (i=0; i<4; ++i)
		printf ("%3d", z[i]);

	return (0);
}

16 comments:

  1. Get the easy way of approaching any array program in c at PROGRAMMING HUB. You will like it

    ReplyDelete
  2. //Add the corresponding values from two arrays of the same size.
    include(stdio.h)
    void add (int a1[],int a2[],int a3[],int n)
    {
    int i=0;
    while(i<n)
    {
    a3[i]=a1[i]+a2[i];
    i++;
    }
    }
    int main(void)
    {
    int x[4]={99,6,55,36},i=0;
    int y[4]={12,1,44,30};
    int z[4];
    add(x,y,z,4);
    while(i<4)
    {
    printf("%4d",x[i]);
    i++;
    }
    printf("\n+\n");
    i=0;
    while(i<4)
    {
    printf("%4d",y[i]);
    i++;
    }
    printf("\n-----------------\n");
    i=0;
    while(i<4)
    {
    printf("%4d",z[i]);
    i++;
    }
    getch();
    }

    ReplyDelete
  3. #include
    main()
    {
    int a[4]={2,3,4,5};
    int sum=0,i;
    int b[4]={3,4,5,6};
    for(i=0;i<4;i++)

    {
    sum=a[i]+b[i];

    printf("%d\n",sum );
    }
    }

    ReplyDelete
  4. #include
    using namespace std;
    int main()
    {
    int array[4]={2,3,4,5},array2[4]={4,6,2,8},i,sum=0;
    for(i=0;i<4;i++)
    {
    sum=array[i]+array2[i];
    cout<<sum<<endl;
    }
    }

    ReplyDelete
  5. #include
    int main(){
    int p[3]={1,2,3};
    int q[3]={3,2,1};
    int i,sum=0;
    for(i=0;i<=3;i++)
    {
    sum+=p[i]+q[i];

    printf("%d\n",sum);
    }
    }

    ReplyDelete
  6. #include
    int main()
    {
    int sum=0,i,a[5]={1,2,3,4,5},b[3]={6,7,8};
    for(i=0;i<3;i++)
    {
    sum=a[i]+b[i];
    printf("\n%d",sum);
    }
    }

    ReplyDelete
  7. Name : Sumsunnahar Nawa
    ID : 201710090
    Batch:54th
    https://drive.google.com/open?id=1TMD0g81TcLL7Nvr67ThRz_gJeR6PdCXr

    ReplyDelete
  8. Shipon Hossan
    ID-201810009

    https://drive.google.com/open?id=1XN2CHOa9H6isNsV_qestSHunf-TMHKsg

    ReplyDelete
  9. Dilruba Parvin Tonni
    ID-201810641
    https://drive.google.com/open?id=1g_99AeL_Bh_6zEJ9j-sKllZJb0d9CtAB

    ReplyDelete
  10. Fazlul Haque Rana
    ID-201810233
    https://drive.google.com/open?id=1OQ2IWscUD5_6sZB99JNSpPhYxdBEmFJA

    ReplyDelete
  11. ID: 201810037
    https://drive.google.com/open?id=1JKxVj5TS4_UsIdb7BfP07m2Lq9oMF7Fs

    ReplyDelete
  12. Shamima Islam Sima
    ID-201810532
    https://drive.google.com/open?id=1QDvygS8_K0Pov5ry02yFwFLEpK81Bbdd

    ReplyDelete
  13. https://drive.google.com/open?id=1xnfg_-Rd3rQla4mjY4bsHMszOnIpKXVI
    ID: 201710404

    ReplyDelete
  14. ID #201610288
    https://drive.google.com/open?id=19FUFQCTJYnCXYl1XlVEsXGNo91cj5Am37wd4Y844WSU

    ReplyDelete
  15. ID #201611036
    Link--> https://drive.google.com/open?id=1c0DGNcvG_2ZN0LZDKZcbPg3MEK034Mns

    ReplyDelete
  16. ID:201620318
    https://drive.google.com/open?id=1IV_dyPc9F9MPDrFo6tWOVr6MXUJ7LeBG

    ReplyDelete