Darklight said:
Question:
Write a function named sumarrays() that accepts two arrays that are the same
size. The function should add each element in the array together and place
the values in the thrid array.
Which answer is the better practice;
The second, but with some caveats.
/*EX9.C*/
#include<stdio.h>
#define MAX 5
int array2[MAX] = {1,2,3,4,5};
int array3[MAX] = {6,7,8,9,10};
You should avoid file scope (global) variable definitions where
possible. For a toy program like this it's no big deal, but as you
start writing larger, more complex programs, they become a pain to
manage effectively.
int sumarray(int array2[], int array3[]);
It's a good idea to pass the array size as a separate parameter along
with the arrays themselves. One of C's quirks is that it doesn't pass
arrays by value (regardless of what the prototype looks like it's
saying); rather, a pointer to the first element of the array gets
passed to the function, so it's impossible to determine the size of an
array that's passed as an argument. Therefore, if you want to sum
arrays of any size (not just a constant size determined at compile
time), you will need to pass the size as a separate parameter:
int sumarray (int *array2, int *array3, size_t arrsize);
int main(void)
{
sumarray(array2,array3);
return 0;
}
int sumarray(int array2[], int array3[])
{
int ctr;
int total[MAX];
for(ctr = 0; ctr < MAX; ctr++)
{
total[ctr] = array2[ctr] + array3[ctr];
printf("Total %d\n",total[ctr]);
}
return total[count];
}
Are you sure you're doing what you mean here? You're returning a
single int value, not an array. Secondly, you're attempting to
subscript with an undeclared variable (count). Thirdly, if you mean
'ctr' instead of 'count', you're attempting to return the value
immediately following the last element of the array, which is probably
not what you want.
____________________________________________
/* EX9-1.C*/
#include<stdio.h>
#define SIZE 5
void sumarray(int [], int []);
int main(void)
{
int a[SIZE] = {1,2,3,4,5};
int b[SIZE] = {6,7,8,9,10};
Better. a and b are limited to auto scope. You should still pass the
array size as a separate parameter.
sumarray(a,b);
return 0;
}
void sumarray(int first[], int second[])
{
int total[SIZE];
int ctr;
for(ctr = 0; ctr < SIZE; ctr++)
{
total[ctr] = first[ctr] + second[ctr];
printf("Total %d\n",total[ctr]);
}
}
It would probably be handy to have the summed array available to other
routines, so you should ideally return it to the caller. Since
functions can't have array return types, you need to either return a
pointer to the array (which means dynamically allocating the array,
which you probably don't want to get into yet), or passing the sum
array as yet another parameter. Here's an example:
#include <stdio.h>
void sumarray (int *arr1, int *arr2, int *sumarr, size_t arrsize)
{
size_t i;
for (i = 0; i < arrsize; i++)
{
sumarr
= arr1 + arr2;
}
}
int main (void)
{
int a1[5] = {0,2,4,6,8};
int a2[5] = {1,3,5,7,9};
int sum[5];
int i;
sumarray (a1, a2, sum, 5);
for (i = 0; i < sizeof sum / sizeof sum[0]; i++)
{
printf ("sum[%d]: %d\n", sum);
}
return 0;
}