problem in C with remainder of the split [closed]

2

Develop a program that reads ten elements of a matrix A vector type. Construct a matrix B of the same type, observing the following formation law: 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 contents of matrix A and B .

When I compile the program it 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 = index%;" as the line with error. From now on 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]);
    
asked by anonymous 22.09.2015 / 16:11

3 answers

3

You just missed dividing by 2 to see if it's even or not. You can not stop the math expression in the middle.

#include <stdio.h>

int main(void) {
    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 % 2; //<=========== dividi por 2
     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]);
    return 0;
}

See running on ideone .

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

    
22.09.2015 / 16:15
1
resp = indice %;

The line is not valid, the expression is incomplete, an argument is missing. By the statement of the question I suggest that it is number 2 because there is a need to find out if the number is even or not. The posted error can be corrected as well.

resp = indice % 2;
    
22.09.2015 / 16:17
1

The % operator gives you the remainder of the division of a number (the dividend) by another number (the divisor). So, you need to provide both numbers for 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 for the operator. It was missing supplying the divider. It would be something like 5 % , which does not make sense to the compiler.

To correct the error, simply supply the divider for the % operator:

resp = indice % 2;

I hope I have clarified your question and helped you to correct the error.

    
22.09.2015 / 17:32