problem in C with rest of the division

Asked

Viewed 1,025 times

2

Develop a program that scans ten elements of an A-matrix vector type. Construct a matrix B of the same type, observing the following law of formation: if the value of the index is even, the value must be multiplied by 5, being odd, must be added with 5. At the end show the content of matrix A and B.

When I go to compile the program shows the following error.

2417 19 C: Users User Documents DEV C++ aula03.cpp [Error] expected Primary-Expression before ';' token

It highlights the line "Resp = Indice %;" as the line with error. I am grateful for your help.

    int matriza[10];
    int matrizB[10];
    int indice;
    int resp;

    for( indice = 0; indice < 10; indice++ )
     {
     printf("digite um valor numerico inteiro: ");
     scanf ("%d", &matriza[indice]);
     }

    for(indice = 0; indice < 10; indice++)
     {
     resp = indice %;
     if (resp == 0)
     matrizB[indice] = matriza[indice] * 5;
     else
     matrizB[indice] = matriza[indice] + 5;
     }

    for(indice = 0; indice < 10; indice++)
     printf ("\nConteudo da matriz a indice %d = %d", indice,           matriza[indice]);

    for(indice = 0; indice < 10; indice++)
     printf ("\nConteudo da matriz B indice %d = %d", indice, matrizB[indice]);
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

3

Just missed you make the split by 2 to know if it’s even or not. You can’t stop the math expression in between.

#include <stdio.h>

int main(void) {
    int matriza[10];
    int matrizB[10];
    int indice;
    for (indice = 0; indice < 10; indice++) {
        printf("digite um valor numerico inteiro: ");
       scanf ("%d", &matriza[indice]);
     }
    for (indice = 0; indice < 10; indice++) {
        if (indice % 2 == 0) matrizB[indice] = matriza[indice] * 5;
        else matrizB[indice] = matriza[indice] + 5;
    }
    for (indice = 0; indice < 10; indice++) printf ("\nConteudo da matriz a indice %d = %d", indice, matriza[indice]);
    for (indice = 0; indice < 10; indice++) printf ("\nConteudo da matriz B indice %d = %d", indice, matrizB[indice]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I didn’t check if the rest of the logic is right, I just solved the question problem. nor did I simplify and organize the code better. The code is pretty bad to read. try to learn to do things in a more organized way and avoid silly mistakes like this.

  • This code was not made by me, I copied and pasted here, it is one of the exercises of the workbook, I did it in Devc++, and he presented this error.

  • 1

    Don’t try to learn from such a bad material. Even one that must have indicated using Dev-C++ as a good thing to work with :)

  • @user8470 you should look for other sources to learn as already mentioned and also change IDE.

1

resp = indice %;

The line is not valid, the expression is incomplete, an argument is missing. From the statement of the question I suggest number 2 because there is a need to find out if the number is even or not. The error posted can be fixed like this.

resp = indice % 2;

1

The operator % provides you the rest of the division of a number (the dividend) by another number (the divisor). So, you need to provide the two numbers to the operator. Some examples:

5 % 2 = 1
6 % 4 = 2
9 % 3 = 0

The error occurred due to the fact that you only provided the dividend to the operator. Missing the divisor. It would be something like 5 %, which makes no sense to the compiler.

To fix the error, just provide the splitter to the operator %:

resp = indice % 2;

I hope I’ve clarified your doubt and helped you correct your mistake.

Browser other questions tagged

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