How do you reverse the digits of a vector and print them separately ? And can you differentiate 000 from 0 in C?

Asked

Viewed 56 times

0

I have to do a program to reverse the digits of a number and print it between brackets []; To begin , the reading loop has to stop when the number typed is equal to 0, however if the value is a sequence of 0s it has to continue (ex:000 or 00); And I don’t know if my logic is right, can you help me? follows the reading code:

    do
{
    scanf("%d", &n[i]);
    k=n[i];
    i++;
}while(k!=0);

and the entire code:

  #include <stdio.h>
  #include <math.h>
  int main ()
  {
    int n[100],v[100],a[100],b[100];
    int i=0,cont,j,k,x,y,t,p;
    do
    {
      scanf("%d", &n[i]);
      k=n[i];
      i++;
    }while(k!=0);
for(j=0;j<i;j++)
{
    if(n[j]>=1000)
    {
        p=n[j]/1000;
        v[j]=n[j]%1000;

        if(v[j]>=100)
        {
            y=v[j]/100;
            a[j]=v[j]%100;
            if(a[j]>=10)
            {
                t=a[j]/10;
                b[j]=a[j]%10;
                printf("[%d][%d][%d][%d]\n", a[j]%10,t,y,p);
            }
        }
    }
    else if(n[j]>=100 && n[j]<1000)
    {
        y=n[j]/100;
        a[j]=n[j]%100;
        if(a[j]>=10)
        {
            t=a[j]/10;
            b[j]=a[j]%10;
            printf("[%d][%d][%d]\n", a[j]%10,t,y);
        }
    }

}
return 0;

}

  • Mathematically zeroes on the left are totally expendable, there is no difference between the number 0 and 000. Maybe you don’t want to consider it as a number but as a string and then "'0" is different from "000".

  • 000 is not a number, and so the problem starts right there. Read everything as text and you won’t even have a problem

  • how char? Because in the statement of the problem says that if the number typed is 000 , it should print only 0, but if it is typed 000 the rest does not run

  • read with char,this should solve the problem

1 answer

0

The logic of the program that is in the question is very confusing, nor have I tried to understand.
Below is a program with a logic that I believe is much simpler.

P.S. Teachers never talk about documentation, comments ? In the program that is in question there is not a single comment!

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  int i, n_digits, valid_number;

  // vai tratar numeros com no maximo 4 digitos
  char digits[5];

  for (;;)
  {
    printf("*\n");
    printf("* digite um numero: ");

    // le uma string com no maximo 4 caracteres, ignora restante da linha
    scanf("%4s%*[^\n]", digits);

    // verifica se foi digitado um numero valido
    valid_number = 1;
    n_digits = strlen(digits);
    for (i = 0; i < n_digits; i++)
      if (!isdigit(digits[i]))
      {
        valid_number = 0;
        break;
      }

    if (!valid_number)
    {
      printf("* nao foi digitado um numero valido, tente novamente\n");
      continue;
    }

    // se foi digitado apenas um zero e nada mais, termina loop
    if (n_digits == 1 && digits[0] == '0')
      break;

    // mostra o numero na ordem inversa
    printf("* [");

    for (i = n_digits; i > 0; i--)
      printf("%c", digits[i-1]);

    printf("]\n");
  }

  printf("*\n");
}

Browser other questions tagged

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