问题
The exercise says "Make a function with parameters two int arrays and k which is their size. The function should return another array (size k) where every element of it is the sum of the two arrays of the same position. That's what I wrote, but it crashes. Do I have to do it with pointers?
#include <stdio.h>
#include <stdlib.h>
void sumarray(int k,int A[k],int B[k]){
int sum[k],i;
for(i=0;i<k;i++){
sum[i]=A[i]+B[i];
printf("sum[%d]=%d\n",i,sum[i]);}
}
main(){
int i,g,a[g],b[g];
printf("Give size of both arrays: ");
scanf("%d",&g);
for(i=0;i<g;i++){
a[i]=rand();
b[i]=rand();
}
sumarray(g,a,b);
system("pause");
}
Example: If i have A={1,2,3,4} and B={4,3,2,1} the program will return C={5,5,5,5).