Can you make one simpler than that?

Asked

Viewed 191 times

2

I don’t understand this decrease with the increment together.

Read 2 integers I, F with I < F and check whether I is smaller than the F. Print I, F, successor to I, predecessor of F, successor of F, predecessor of the predecessor of F

For example, for the input 3, 10, the output should be:

3, 10, 4, 9, 5, 8, 6, 7.

Code:

#include <stdio.h>

int main (){

    int I, F;

    do{
        scanf("%d", &I); /*recebe valor de I*/
        scanf("%d", &F); /*recebe o valor de F*/
    }while (I >= F); /* se I for maior do que F fica no loop*/

    for (int i = I, j= F; i <= j; i++, j--/*Não entendi essa parte*/ /*como saber qual numero pegar ?*/){
        if (i == j){ /*tem como fazer 2 for não?*/
            printf("%d ", i);
        }else{
        printf("%d %d ", i, j);
    }
    }

    printf("\n");
    printf("fim de programa\n");

return 0;
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

4 answers

2

Yes it is possible, no need to create auxiliary variables and no need to take care of the exception within the loop. It would eliminate the increment in one of the variables, but it would create other needs that would complicate the code. It also has how to exchange the for by a while which would somehow become simpler.

#include <stdio.h>

int main () {
    int I, F;
    do {
        scanf("%d", &I);
        scanf("%d", &F);
    } while (I >= F);
    for (; I < F; I++, F--) printf("%d %d ", I, F);
    if (I == F) printf("%d ", I);
    printf("\nfim de programa\n");
}

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

With while:

while (I < F) printf("%d %d ", I++, F++);

2

Below I will be explaining your doubt in your code and soon after I will be making a code for you upon your reasoning.

Your code: The j in your code you will be receiving the whole F. How you left it for the user to input the data using the scanf, you will not know what value decrease (withdraw), but assuming the user typed in 10 for the whole F and 5 for the whole I what will occur in the loop(for) will be the following...

  • "i = 5, j = 10; 5 <= 10; i++, j-- "

What this tie(for) means: The accountant(i) begins with 5 and 10 and while the 5 is less than or equal to 10 he will add (increase) +1 in variable entire i and will take (decrease) 1 in the integer variable J. He will do this until he is out of the loop. But this loop is completely confused and illogical.

#include <stdio.h>

int main (){

    int I, F;

    do{
        scanf("%d", &I); /*recebe valor de I*/
        scanf("%d", &F); /*recebe o valor de F*/
    }while (I >= F); /* se I for maior do que F fica no loop*/

    for (int i = I, j= F; i <= j; i++, j--/*Não entendi essa parte*/ /*como saber qual numero pegar ?*/){
        if (i == j){ /*tem como fazer 2 for não?*/
            printf("%d ", i);
        }else{
        printf("%d %d ", i, j);
    }
    }

    printf("\n");
    printf("fim de programa\n");

return 0;
}

My conclusion about this tie and variables: For my person, the way it is written is not pleasant for the maintenance of this code, what I would do is the following:

#include <stdio.h>

int main (){

    int valorUm, valorDois;

        // Recebe os dois Valores    
        scanf("%d", &valorUm);
        scanf("%d", &valorDois);

    do{ 
        // Armazena os dois valores obtidos nas variáveis auxiliares
        int auxValorUm = valorUm;
        int auxValorDois = valorDois;

        // Soma mais 1 na variável inteira auxValorUm
        // Ex: valorUm = 5; auxValorUm = 5; 5+1 = 6
        auxValorUm++;

        // Tira 1 na variável inteira auxValorDois
        // Ex: valorDois = 5; auxValorDois = 5; 5-1 = 4
        auxValorDois--;

        // O problema aqui é que as variáveis NUNCA serão iguais, pois haverá o incremento e decremento.
        if(auxValorUm == auxValorDois){
            printf("%d", auxValorUm);
            printf("\nFim do Programa");
            break; // Caso entre neste IF o programa dará a resposta e fechará.
        }else {
            printf("%d %d", auxValorUm, auxValorDois);
            printf("\nFim do Programa");
            break; // Caso entre neste ELSE o programa dará a resposta e fechará.           
        }
    }while(true); // Ocorrerá um LOOP INFINITO, pois a condição é sempre verdadeira.


    return 0;
}

Conclusion of your Code and Logical Reasoning: As you can see to my comments your reasoning are wrong. Why is it wrong:

1) You do a Do-While to record several values, but for you record all these values you must use the vector to allocate them in memory.

2) Your IF will never enter it, as one variable sums and the other decrease.

3) You have to write the variable names with the following rules: short and understandable and camelCase. You also do not can go out commenting on the code, keep in mind that the comment has to be little and in reasoning that are difficult to facilitate the work maintenance code, if another programmer or even you come change the lines of code.

Important remark: In order for me to create the code correctly, you need to edit your answer and leave the statement.

1

Here’s the code you need.

#include <stdio.h>
int main(void) {
  int I = 3;
  int F = 10;

  while(I<F){
    printf("%d, %d ", I, F);
    I++;
    F--;
  }
  return 0;
}

This should compute what you want. If you need to read the numbers at runtime, just use the scanf to read I and F.

  • 1

    Thanks for the editing and correction, @Rafael Tavares.

0

The above solutions differ from your code by the fact that most of them change I and F, while your loop uses auxiliary variables i and j to calculate I-i keeping the information in I. In case you want to keep this information, another simple way to loop is to use a single auxiliary variable, since i and j always has the same value:

int i=0;
for(; I+i< F-i; i++) printf("%d %d ", I+i, F-i);
if (I+i == F-i) printf("%d", F-i);

You can also do the same thing by looking at the difference between I and F:

for(int i=0; i<=(F-I)/2; i++)
    if (I+i == F-i) printf("%d", F-i);
    else printf("%d %d ", I+i, F-i);

Browser other questions tagged

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