Basically, I'm trying to create another array to save the sum of the number in the first position in "arreglo1" with the last one in "arreglo2" (3 1), but I get an error at time of compiling that says "expression must have pointer-to-object type but it has type int". What am I doing wrong? ;(
#include <stdio.h>
int funcion1(int arreglo1,int arreglo2);
int main()
{
int arreglo1[5]={3,5,1,4,-2};
int arreglo2[5]={8,7,2,5,1};
funcion1 (arreglo1,arreglo2);
}
int funcion1(int arreglo1,int arreglo2){
int arreglo3[]={};
int n=5;
int i;
int suma;
for ( i = 0; i < 5; i )
{
arreglo3[i]= arreglo1[i] arreglo2[n-1];
}
for ( i = 0; i < 5; i )
{
printf("%d",arreglo3[i]);
}
}
CodePudding user response:
I am also beginner so please tell me if i am wrong.
The problem here is that you are defining the function with:
int fuction1(int aareglo1, int arreglo2);
You are defining that the function takes an integer as an input, but you are giving an array as an input, so the correct code should be:
int function1(int arrenglo1[], int arrenglo2[]);
And there is also a problem with its logic, you are giving the value to arrenglo3 as:
arrenglo3[i] = arrenglo1[i] arrenglo2[n-1];
Whereas, it should be:
arrenglo3[i] = arrenglo1[i] arrenglo2[n-i-1];
I have modified the code and it compiles correctly(atleast in my compiler):
#include <stdio.h>
int funcion1(int arreglo1[],int arreglo2[]);
int main()
{
int arreglo1[5]={3,5,1,4,-2};
int arreglo2[5]={8,7,2,5,1};
funcion1 (arreglo1,arreglo2);
}
int funcion1(int arreglo1[],int arreglo2[]){
int arreglo3[5];
int n=5;
int i;
int suma;
for ( i = 0; i < 5; i )
{
arreglo3[i]= arreglo1[i] arreglo2[n-i-1];
}
for ( i = 0; i < 5; i )
{
printf("%d\n",arreglo3[i]);
}
}
CodePudding user response:
int arreglo1
suggests arreglo1
is an integer.
However, you have passed in an array.
Modify your funcion1
to take a pointer to ints, as so:
int funcion1(int *arreglo1, int *arreglo2);
after that, this line:
int arreglo3[]={};
creates a zero-length array, you want 5 items, so write:
int arreglo3[5];
then actually compute suma
and return it.