简体   繁体   中英

Reverse the output of printf function in while loop

I wrote a code for a b-adic representation of a chosen number.

#include <stdio.h>

int b_adisch (int a, int b)
{

    int x, y, mod, mod1;

    x = a / b;
    mod1 = a % b;

    printf("%i\n", mod1);

    do {
    y = x / b;
    mod = x % b;
    x = y;
    printf("%i\n", mod);
    } while(x != 0);
    return a ;
}

int main (void)
{
    int a, b;
    printf("pls input a ");
    scanf("%i", &a);
    printf("pls input b ");
    scanf("%i", &b);
    b_adisch(a, b);

    return 0;
}

The output order will be reversed since the printf has to be put into the while loop and the calculation starts with the last number of the representation.

Example if a = 10 and b = 2
The output is 0101
but it should be 1010

How can I change my code to make this happen?

How can i change my code to make this happen?

2 approaches:

Compute the digits from least to most significant and save in a adequate sized buffer. This is similar to OP's approach yet saves the results of each digit's computation for later printing.

#include <assert.h>
#include <limits.h>

void b_adisch(int value, int base) {
  // Let us work with simple cases first.
  assert(value >= 0);
  assert(base >= 2 && base <= 10);

  // Adequate sized buffer
  char buffer[sizeof value * CHAR_BIT + 1];
  // Start at end
  char *end = &buffer[sizeof buffer - 1];
  *end = '\0';

  do {
    end--;
    int digit = value%base;  // Find least digit
    value /= base;
    *end = digit + '0';  // save the digit as text
  } while (value);

  printf("<%s>\n", end);  // print it as a string
}

Use recursion. A more radical change; This computes and prints the output of the more significant digits first.

void b_adischR_helper(int value, int base) {
  // If the value is at least 2 digits, print the most significant digits first
  if (value >= base) {
    b_adischR_helper(value/base, base);
  }
  putchar(value % base + '0');  // Print 1 digit as text
}

void b_adischR(int value, int base) {
  // Let us work with simple cases first.
  assert(value >= 0);
  assert(base >= 2 && base <= 10);

  printf("<");
  b_adischR_helper(value, base);
  printf(">\n");
}

Test

int main() {
  b_adisch(10, 2);
  b_adischR(10, 2);
  b_adisch(INT_MAX, 10);
  b_adischR(INT_MAX, 10);
  b_adisch(INT_MAX, 2);
  b_adischR(INT_MAX, 2);
}

Output

<1010>
<1010>
<2147483647>
<2147483647>
<1111111111111111111111111111111>
<1111111111111111111111111111111>

You can store the output in an array as here it is stored in "arr" and later print the output in reverse order (from end to start).

#include <stdio.h>
int arr[10000]={0};
void b_adisch (int a, int b)
{
    int x, y, mod, mod1,i=0,j;
    x = a / b;
    mod1 = a % b;
    arr[i++]=mod1;
    do {
    y = x / b;
    mod = x % b;
    x = y;
    arr[i++]=mod;
    } while(x != 0);
    for(j=i-1;j>=0;j--)
    printf("%i\n",arr[j]);
}

int main (void)
{
    int a, b;
    printf("pls input a ");
    scanf("%i", &a);
    printf("pls input b ");
    scanf("%i", &b);
    b_adisch(a, b);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM