IHYPRESS.NET > C PROGRAMMING
HOME | ASP | C | HTML/CSS | JAVASCRIPT | MATLAB | PERL | PHP | PYTHON | RUBY
C > 07. Arrays I (1D) [PROGRAM #07]
CODERESULT
<
/* 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);
}

Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
  1  2  3  4
 +
 10 20 30 40
-------------
 11 22 33 44
>
COPYRIGHT © 2019 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.
SHARE