Compiler accusing error that I do not know

1
#include<stdio.h>
int sumofDie(int value1, int value2);
int main( void ){
int die1[7];
int die2[7];
int i;

srand(time(NULL));

printf("Rolling die 1 ..........\n");

for( i = 0; i < 6; i++){
    int value;
    value = 1 + rand() % 6;
    die1[i] = value;
}

printf("Rolling die 2......\n");

for ( i = 0; i < 6; i++){
    int value;
    value = 1 + rand() % 6;
    die2[i] = value;


}
for( i = 0; i < 6; i++){

    printf("The %d value of the array of the first die is: %d \n", i + 1, die1[i]);
    if ( i == 6){
        int k;
        printf("\n");
        for( k = 0; k < 6; k++){
            printf("The %d value of the array of the second die is: %d\n", k + 1, die2[k]);
        }

    }
}

sumofDie(die1,die2);

return 0;




}


int sumofDie( value1, value2){
int sum = 0;
int i;
for( i = 0; i < 6; i++){
    sum = value1[i] + value2[i];
    if ( i == 6){

        return sum;
    }
}



}

I've probably done it a very long way, even though there's a simpler way to do it, but come on: I did this program to "capture" the random numbers of two 6-sided data and then add them up. I tried to use a function in the end to practice more what I've been learning so far, but it happens that when I try to compile, an error that I still do not know identifies to appear > expected 'int' but argument is of type 'int *' I searched in some places and saw some people commenting on pointers, I have not yet entered this subject in C and, if that is the case, should I do this program so that I use smokes function to add up the final value?

    
asked by anonymous 05.01.2017 / 04:50

3 answers

3

The error happens because you passed die1 and die2 as parameters to the sumofDie function, and die1 and die2 are two vectors ('*' pointers); remembering that the function receives two int's as parameters, value1 and value2 .

    
05.01.2017 / 14:44
2

I think the error is in the function declaration. It should be something like:

int sumofDie(int *value1, int *value2)

or

int sumofDie(int value1[], int value2[]) 

or

int sumofDie(int value1[7], int value2[7]) 

And you have the question that Antonio Alexandre posted.

    
05.01.2017 / 13:23
2

Its sumofDie function (value1, value2) although it will always arrive at a time that i will be equal to 6 (in the last interaction), i think the compiler is implying because it has no return outside of the loop, other than its return is inside an if, which can often indicate an unreachable block. If it were for example if (i == 7), it would never enter that block, so its function would have no return and in the statement it says you should return an integer.

Try the following, instead of:

int sumofDie( value1, value2){
int sum = 0;
int i;
for( i = 0; i < 6; i++){
    sum = value1[i] + value2[i];
    if ( i == 6){

        return sum;
    }
}



}

Type:

int sumofDie(int value1[], int value2[])
{
    int sum = 0;
    int i;
    for( i = 0; i < 6; i++)
    {
        sum = sum + value1[i] + value2[i];
    }

    return sum;
}

Edited: Changed line:

sum = value1[i] + value2[i]; 

for

sum = sum + value1[i] + value2[i];

Because I believe that what you want is the sum of all the moves, otherwise it would not make sense to be inside the for.

    
05.01.2017 / 10:53