Is there any more beautiful way for me to change the numbers?

Asked

Viewed 47 times

-5

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#define tam 20

int main (void){
setlocale(LC_CTYPE, "");
    int num[tam];
    int troca[tam];
        for(int i = 0; i < tam; i ++){
            printf("\n\tDigite aqui o %dº número >==> ", i+1);
                scanf("%d", &num[i]);
        }
            for(int i = 0; i < tam; i ++){
            if(i == 10){
                printf("\n");
            }
                troca[0]=num[1];
                troca[2]=num[3];
                troca[4]=num[5];
                troca[6]=num[7];
                troca[8]=num[9];
                troca[10]=num[11];
                troca[12]=num[13];
                troca[14]=num[15];
                troca[16]=num[17];
                troca[18]=num[19];
                troca[19]=num[18];
                troca[17]=num[16];
                troca[15]=num[14];
                troca[13]=num[12];
                troca[11]=num[10];
                troca[9]=num[8];
                troca[7]=num[6];
                troca[5]=num[4];
                troca[3]=num[2];
                troca[1]=num[0];

            sleep(1);

            printf("\t%d", troca[i]);

        }
return 0;
system("pause");
}
  • 1

    You could explain better what you want to do, only the title of the question does not clarify enough.

  • 1

    There is, but explain better what your need is by editing the question.

  • So I drew a 20 position vector, right after that I had to invert the vector values in the following way. the first value with the second, the third with the fourth, the fifth with the sixth and so on until the exchange of the nineteenth with the twentieth.

1 answer

3

How about this?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#define tam 20

int main(void) {
    setlocale(LC_CTYPE, "");
    int num[tam];
    int troca[tam];
    for (int i = 0; i < tam; i++) {
        printf("\n\tDigite aqui o %dº número >==> ", i + 1);
        scanf("%d", &num[i]);
    }
    for (int i = 0; i < tam; i++) {
        if (i == 10) {
            printf("\n");
        }
        for (int j = 0; j < tam; j += 2) {
            troca[j] = num[j + 1];
            troca[j + 1] = num[j];
        }
        sleep(1);
        printf("\t%d", troca[i]);
    }
    system("pause");
    return 0;
}

That makes the exchange in your program more beautiful and more logical. Despite that, I do not understand what is the purpose of this program.

Or better yet, like this:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#define tam 20

int main(void) {
    setlocale(LC_CTYPE, "");
    int num[tam];
    int troca[tam];

    /* Lê todos os números. */
    for (int i = 0; i < tam; i++) {
        printf("\n\tDigite aqui o %dº número >==> ", i + 1);
        scanf("%d", &num[i]);
    }

    /* Realiza as trocas. */
    for (int j = 0; j < tam; j += 2) {
        troca[j] = num[j + 1];
        troca[j + 1] = num[j];
    }

    /* Mostra o resultado. */
    for (int i = 0; i < tam; i++) {
        if (i % 10 == 0) printf("\n");
        printf("\t%d", troca[i]);
    }

    system("pause");
    return 0;
}
  • More correctly, it would be for (int j = 0; j < tam; j += 2) { troca[j] = num[j + 1]; troca[j + 1] = num[j]; }

  • @Wtrmute According to the original code yes, but clearly that is not the purpose of the questioner (although I don’t know what it would be).

  • did not make the exchanges with this code, only returned everything 0 and -1, but yes, from now on I can solve, I thank you!

  • @fvsgamers Now that you explained the purpose of your program in a comment on the question, I made a second version and put in this answer. If this solves your problem, don’t forget to click on what appears below the voting numbers of this answer to mark this question as solved and mark my answer as the solution.

  • @Victorstafusa: It may not be the original purpose of the questioner, but note that in your answer you read the numbers for the vector num and then invert the values in the vector troca that has not been initialized.

  • @Wtrmute Oh, well noted. Thank you.

Show 1 more comment

Browser other questions tagged

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