Error in printing C numbers

Asked

Viewed 63 times

0

The error is that the printing of the numbers is appearing duplicated.

Enunciation: Enunciado da questão

The error message: erro

The code:

#include <stdio.h>
#include <stdlib.h>
 
int maiorValor(int *array[], int *q);
 
int main()
{
    int N, k;
    int vetor[] = {0};
    
    printf("Qual o tamanho do array: ");
    scanf("%d", &N);
    printf("Quantos valores por linha: ");
    scanf("%d", &k);
    
    fflush(stdin);
    
    for(int r = 0; r < N; r++)
    {
        vetor[r] = rand()%99;
    }
    
    int res = maiorValor(vetor, N);
    
    for(int r = 0; r < N; r++)
    {
        for(int l = 0; l < k; l++)
        {
            printf("%d  ", vetor[r]);
        }
        printf("\n");
    }
    
    printf("\nO maior valor foi: %d", res);
    return 0;
}
 
int maiorValor(int *array[], int *q)
{
    int maior = array[0];
    for(int r = 0; r < q; r++)
    {
        if(array[r] > maior)
        {
            maior = array[r];
        }
    }
    return (maior);
}
  • Do you have to use pointers in this code? Did I test you have a problem with the pointers? In the code rand()%99 generates values up to 98, but the image gives to see much higher values

  • Dude, it’s not very clear to me what’s duplicated... Is it what’s being printed inside the for? If that’s it, just remove the for... Have you tried debugging, seeing step by step what is occurring...

  • Also, you are allocating a vector of a single space instead of allocating a vector with the desired element size

  • I didn’t understand what you meant by "vector of a single space".

  • Your pointer is only enough to store a single integer value, not a vector

2 answers

0


#include <stdio.h>
#include <stdlib.h>

int maiorValor(int array[], int q);

int main()
{
    int N = 1, k;
    
    printf("Qual o tamanho do array: ");
    scanf("%d", &N);
    int vetor[N];
    printf("Quantos valores por linha: ");
    scanf("%d", &k);
    
    fflush(stdin);
    
    for(int r = 0; r < N; r++)
    {
        vetor[r] = rand()%99;
    }
    
    int res = maiorValor(vetor, N);
    
    for(int r = 0; r < N; r++)
{
    printf("%d  ", vetor[r]);

    if (((r+1) % k) == 0) //Verifica se r+1 é múltiplo inteiro de k
    {
        printf("\n"); //Se for, pula linha
    }           
}
printf("\nO maior valor foi: %d", res);
return 0;
}
int maiorValor(int array[], int q)
{
    int maior = array[0];
    for(int r = 0; r < q; r++)
    {
        if(array[r] > maior)
        {
            maior = array[r];
        }
    }
    return (maior);
}
  • not doubling, but still appearing absurd values.

  • Did you remove the pointers? These "absurd" numbers are due to the pointers that were allocated poorly

  • I removed the pointers and added the snippet of code that skips the line. Absurd values still appear. What he decided was to declare int vector after the input of the value N, which I had already done, but which had other parts of the code wrong. Thanks for the help.

0

It’s not just duplicated, your code prints the same value k times, in the following passage:

for(int l = 0; l < k; l++)
{
    printf("%d  ", vetor[r]);
}

To fix, use modular logic, thus:

for(int r = 0; r < N; r++)
{
    printf("%d  ", vetor[r]);

    if (((r+1) % k) == 0) //Verifica se r+1 é múltiplo inteiro de k
    {
        printf("\n"); //Se for, pula linha
    }           
}

Note that it is now necessary to validate the entry k so it’s not zero.

Browser other questions tagged

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