Problem to remove space after line break

Asked

Viewed 99 times

0

#include <stdio.h>

int main (){

    int x,y,i;

    scanf("%d",&x);
    scanf("%d",&y);

    if ((x<y)&&(x>1)&&(x<20)&&(y<10000)){
        for (i=1;i<=x;i++){
            printf("%d ",i);
                }
            printf("\n");
       for (i=(x+1);i<=y;i++){
           printf("%d ",i);
       }
   }
 return 0;
}

objective of the programme:

1st: The program reads two numbers X and Y (X minor Y). Next shows a sequence from 1 to Y, passing to the next line to each X numbers.

2º: Each sequence shall be printed on one line, with a blank space between each number.

3º: The entry contains two integers X (greater than 1 and less than 20) and Y (greater than X and less than 100000) respectively.

4º:OBS: The program does not need to have an answer for cases where the program does not run, and also does not need user interaction.

***Example demonstrating my program error:

inserir a descrição da imagem aqui

saida esperada com as entradas 8-16

By removing that last space before the break of each line the problem of the question will be solved, I just can’t find a way to do it.

2 answers

1

Treat the first one as an exception that doesn’t have a space, the rest will all go within the normal.

#include <stdio.h>

int main () {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    if (x < y && x > 1 && x < 20 && y < 10000) {
         printf("%d", 1);
        for (int i = 2; i <= x; i++) printf(" %d", i);
        printf("\n%d", x + 1);
        for (int i = x + 2; i <= y; i++) printf(" %d", i);
    }
}

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

You can do the reverse and treat the latter as an exception, but I don’t like it.

  • I adjusted the code this way and got, but there is a problem, the first line is correct, but for larger values ex:(x=10,y=50) the code is not skipping lines. How can I adjust it to be generic ? - Because if I put x=10,y=20 the code gets right, but as stated earlier for higher values of y the display gets wrong.

0


Your program displays the sequence of 1 a X, however, the question statement says nothing about displaying this sequence! Which of the two is correct?

To identify values of Y that are multiples of X just check if the rest of the division (or module) of Y for X is equal to zero, check it out:

if( y % x == 0 )
    printf( "Y eh multiplo de X!\n" );

Following his reasoning and the statement of the question, follows a commented solution:

#include <stdio.h>

int main( void )
{
    int i,x,y;

    /* 1) O programa le dois numeros X e Y (X menor Y)... */
    printf("X: ");
    scanf( "%d", &x );

    printf("Y: ");
    scanf( "%d", &y );

    /*
        3) A entrada contem dois numeros inteiros X (maior que 1 e menor que 20) e
           Y (maior que X e menor que 100000) respectivamente.
    */
    if( (x<y) && (x>1) && (x < 20) && (y < 10000) )
    {
        /* 1) A seguir mostra uma sequencia de 1 a Y... */
        for ( i = 1; i <= y; i++ )
        {
            printf( "%d", i );

            /* 1) Passando para a proxima linha a cada X numeros... */
            /* ...e passando pra ultima linha na ultima iteracao    */
            if( (i % x == 0) || (i == y) )
                printf("\n");
            else
                printf(" "); /* Pulo do gato! */
        }
    }
    else
    {
        /* 4) OBS: O programa nao precisar ter resposta para os casos em que o programa nao roda */
        printf("Entrada invalida!\n");
    }

    return 0;
}

Testing (X=19 and Y=60):

X: 19
Y: 60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60

Testing (X=3 and Y=17):

X: 3
Y: 17
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17

Testing (X=4 and Y=15):

X: 4
Y: 15
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15

Testing (X=2 and Y=8):

X: 2
Y: 8
1 2
3 4
5 6
7 8
  • Dear Lacobus, thank you for your help with the code... What adjustment can I make in this code you made based on mine so that there are 2 spaces between each number and only 1 space?

  • Done! See if it fits.

  • Thank you very much Lacobus, you helped me a lot --- There is only one situation that the program is accusing of "empty answer", of 7 tests it passed in 6. I will perform some tests manually and try to find this flaw in the program.

  • @Erick: what is this situation ?

  • the error occurred in the following situation x:1 , y=10 due to this line of code - if( (x<y) && (x>1) && (x < 20) && (y < 10000) //however the error was not mine yours, but the statement of the problem that said x had to be greater than 1 , then it makes no sense to have x=1 as the test of the problem... - I changed the above-mentioned line of code in - if( (x<y) && (x>=1) && (x <=20) && (y < 10000) and the problem was solved.

Browser other questions tagged

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