Compiler accusing error I don’t know

Asked

Viewed 71 times

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 probably did it in a very long way, even though there was a simpler way to do it, but let’s go: I made this program to "capture" the random numbers of two dice on 6 sides and after thatlos... tried to use a function at the end to practice more what I’ve been learning so far but it turns out that when I try to compile, a mistake I still don’t know how to identify appears >> expected 'int' but argument is of type 'int *' I researched in some places and saw some people commenting on pointers, I haven’t gotten into this subject in C and, if that’s the case, how should I do this program so that I use smokes function to add up the final value?

inserir a descrição da imagem aqui

  • What’s the line of error? Usually compilers point... or not ... for so long that I don’t touch C that I can’t even remember....

  • It’s not about programming, but given in English it’s Dice. Die is die. By the way, you’ve given at home?

  • Detail: No problems, but you have 7 positions in your data, but only use 6.

  • 1

    Is it not necessary to indicate the type of the vector? type int sumofDie(int *value1, int *value2) or int sumofDie(int value1[], int value2[]) or int sumofDie(int value1[7], int value2[7])

  • in addition several of the warnings are also errors.

3 answers

3


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

  • 1

    Other things, you created int die[7], a vetor 7 positions (0, 1, 2, 3, 4, 5, 6). But you only travel from i = 0 to i = 5. (6 positions)

2

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

int sumofDie(int *value1, int *value2)

or else

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

or

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

and there’s the question that Antonio Alexandre posted.

  • +1 for you. Truth, I did not remember but in C you have to declare the type of parameters. I even edited in my answer.

2

Its function sumofDie(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 there is no Return out of the loop, besides that your Return is inside an if, which can often indicate an unreachable block. If there were for example if(i==7), it would never enter that block, then its function would have no return and in the statement of it says that 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;
    }
}



}

Write:

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 the 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 have no sense to be inside the.

  • 1

    additionally it seems to me that for(...;i<6;...) implies that it will never happen i==6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.