Read a vector up to a stop bit

1

I want to read an 8-bit binary vector backwards, and stop until I read the last 1 .

For example: [00010101]

asked by anonymous 19.09.2015 / 14:49

1 answer

1

Hello, Good morning!

I had a hard time understanding your question, but from what I realized, you want to read '1' and '0' back and forth until you stop in the last 1 and put them in a new vector. p>

I made a program to exemplify my solution using a vector of characters, since it is binary only we will have two characters (1 and 0). I used the malloc function of the stdlib library to scale the vector to 8-bit size. (n = 8). n1, i and j are auxiliary variables for performing the reverse reading of the vector.

Follow the code

#include <stdio.h>
#include <stdlib.h>
/*Desenvolva as funções como desejar*/
void funcaoAssociada1 () {
}

void funcaoAssociada0 () {
}
int main () {
   char *v, *v1;
   int i, n, j, n1;
   n = 8;
   j = 0;
   n1 = 0;
   v = malloc (n * sizeof (char));
   v1 = malloc (n* sizeof (char));
   printf ("Digite o vetor binário (sem espaços)\n");
   for (i = 0; i < n; i++)
      scanf ("%c", &v[i]);
   for (i = n-1; i >= 0; i--,j++)
      if (v[i] == '1') n1 = j;
   for (i = 0; i <= n1; i++)
      v1[i] = v[n-1-i];

   v1[i-1] = '
#include <stdio.h>
#include <stdlib.h>
/*Desenvolva as funções como desejar*/
void funcaoAssociada1 () {
}

void funcaoAssociada0 () {
}
int main () {
   char *v, *v1;
   int i, n, j, n1;
   n = 8;
   j = 0;
   n1 = 0;
   v = malloc (n * sizeof (char));
   v1 = malloc (n* sizeof (char));
   printf ("Digite o vetor binário (sem espaços)\n");
   for (i = 0; i < n; i++)
      scanf ("%c", &v[i]);
   for (i = n-1; i >= 0; i--,j++)
      if (v[i] == '1') n1 = j;
   for (i = 0; i <= n1; i++)
      v1[i] = v[n-1-i];

   v1[i-1] = '%pre%';/*Caractere nulo, indica fim do vetor*/
   printf ("%s",v1);

   for (i = 0; v1[i] != '%pre%'; i++)
      if (v1[i] == '1') funcaoAssociada1 ();
      else funcaoAssociada0 ();
   free (v1);   
   return 0;
}
';/*Caractere nulo, indica fim do vetor*/ printf ("%s",v1); for (i = 0; v1[i] != '%pre%'; i++) if (v1[i] == '1') funcaoAssociada1 (); else funcaoAssociada0 (); free (v1); return 0; }

I hope it was helpful. Any questions please.

MS

    
20.09.2015 / 20:01