S
S
screbok2017-06-04 08:53:37
Programming
screbok, 2017-06-04 08:53:37

How to output values ​​in reverse order?

Hello.
I am going through the material again according to the book of Deitels, there is a task: transfer from decimal to other number systems.
Translation from q10 to q2

int q = 2;		// основание системы счисления
    int N = 13;		// число
    int r = 0;      // остаток

    while (N > q) {
        r = N % q;
        N = N / q;

        cout << r;

        if (N < q) {
            cout << N;
        }

        cout << flush;
    }

The bottom line is that you need to write this output in the reverse order, how to do it, taking into account the fact that at the time of reading the chapter, the arrays have not been traversed, the lines too. Those. in principle, there was nothing about the reverse of the output. How can it be done, maybe somehow with a mat. points of view?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-06-04
@screbok

For this, you must first find the maximum digit of the number in the specified number system and then output it in the desired order. And get used to giving meaningful names to variables, otherwise you yourself will get confused later.

int value = 13;
int base = 2;
int rank = 1;
while (rank*base <= value)
  rank *= base;
while (rank) {
  printf("%d", value/rank);
  value %= rank;
  rank /= base;
}

1
15432, 2017-06-04
@15432

Write values ​​to a vector and then output in any order

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question