Please help me, i’m stuck at this.
I have a function which must sum two arrays of integers of size n
and i must pass as arguments for the function the first element of each array
Is this enven close?
12 void sumVect(int * v[], int * w[] ,int n)
13 //adds the second vector to the first vector; the vectors v and w have exactly n components
14 {
15 int i;
16 for(i=0;i<n;i++)
17 {
18 *v[i]+=*w[i];
19 }
20
21 }
I will give you the whole code
#include <stdio.h>
void sumVect(int * v[], int * w[] ,int n)
//adds the second vector to the first vector; the vectors v and w have exactly n components
{
int i;
for(i=0;i<n;i++)
{
*v[i]+=*w[i];
}
}
int main()
{
int i,j;
int n = 4; //the size of a vector
int k = 5; //the number of vectors
int v[k][n]; //the vector of vectors
printf("How many vectors? k=");
scanf("%d",&k);
printf("How many components for each vector? n=");
scanf("%d",&n);
for(i=1;i<=k;i++) //read the content of each vector from the screen
{
printf("\nv%d |\n_____|\n",i);
for(j=0;j<n;j++)
{
printf("v%d[%d]=",i,j);
scanf("%d", &v[i][j]);
}
}
for(j=1;j<k;j++)
{
sumVect(&v[j], &v[j+1], n);
for(i=0;i<n;i++)
{
printf("%d",v[j][i]);
}
}
return 0;
}
Note that you are passing type
int []but your formal parameters in the function isint *[]. Therefore your signature should be:void sumVect(int v[], int w[] ,int n)or
void sumVect(int *v, int *w,int n)Also you should access them in the function like
v[i] + w[i]becausevandwboth areint *orint [](depends on the formal paremeter)Also when calling you should do:
sumVect(v[j], v[j+1], n);or
sumVect(&v[j], &v[j+1], n);This is because as as
v[j]is type ofint [],&v[i]andv[i]will evaluate to the same address.