Need help

Can someone help me with a c++ program that reads in a sequence of binary digits (values 0 and 1) and stores them into a STL container. The input should terminate on any input that is not a 0 or 1. After finishing the read-process, apply a "bit-stuffing" algorithm to the container. In this case the bit stuffing should occur after four consecutive bits of the same value.i,e. four 0's or four 1's..
Also write the de-stuffing code to process the stuffed data to recreate the original data and verify that the original data is recovered correctly.
This is gonna be good start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>
#include <iostream>

int main()
{
	std::vector<int> data;
	int input_data;
	std::cin >> input_data;

	while ( (input_data == 0 || input_data == 1) )
	{
		data.push_back(input_data);
		std::cin >> input_data;
	}

	for (int i = 0; i < data.size(); ++i)
	{
		std::cout << data[i] << " ";
	}
	std::cout << std::endl;


	std::cin.get();
	return 0;
}



Sorry I don't know about "bit-stuffing" algorithm .

Good luck
May I add that there is a specialization of the std::vector<bool> class, which uses less memory, and may thus be useful. http://en.cppreference.com/w/cpp/container/vector_bool
@Bourgond Aries,
That's good. Thanks for bringing this up. I though about it first, but the problem is in this case any non-zero case is considered as true (i.e. 1, 2, 3, ... are true).
Is there a way to force the true case to be only 1??
Last edited on
@CroCo

Doesn't the while loop limit input data to 0 and 1? Also I don't think you need the extra parenthesis there.
To force the true case to only be 1? Perhaps the tenary operator can be of good use?

 
data.push_back(input_data == 1 ? true : false);
@Bourgond Aries,
Yes, the while loop does limit the input data to be 0 or 1.
Croco and Bourngond- thanks for the reply.

i did run the program but the output obtained is specifically not correct.

here is the code i am trying with using string (as STL container) but i am unable to run it due to error.
can u please help me with this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // read bits '0'/'1' into a string; terminate on any other char
    std::string data_stream ;
    char bit ;
    while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;

    // stuff a zero bit after four consecutive bits of the same value.
    std::string stuffed_stream ;
    int cnt = 0 ;
    char bit_last_seen = 0 ;
    int stuff_bits_added = 0 ;
    
    for( bit<=data_stream);           //for each bit in the data_stream
    {
        if( bit==bit_last_seen)       //it is the same as the previous bit
			cnt++;                      

        else // it is a different bit 
        {
            bit_last_seen := this bit ;
            cnt := 1 ; // restart count at 1
        }

        stuffed_stream += bit ; // add the bit 

        if( cnt == 4 ) // there are four consecutive bits of the same value
        {
            stuffed_stream += '0' ; // stuff with a zero bit
            cnt := 0 ; // and reset cnt to zero
            ++stuff_bits_added ; // increment the count of stuff bits added
        }
    }

	std::string destuffed_stream ;
    int cnt = 0 ;
    char bit_last_seen = 0 ;
    int stuff_bits_added = 0 ;
    
    for( bit<=data_stream);           //for each bit in the data_stream
    {
        if( bit==bit_last_seen)       //it is the same as the previous bit
			cnt--;                      

        else // it is a different bit 
        {
            bit_last_seen := this bit ;
            cnt := 1 ; // restart count at 1
        }

        stuffed_stream -= bit ; // sub the bit 

        if( cnt == 4 ) // there are four consecutive bits of the same value
        {
            destuffed_stream += '0' ; // stuff with a zero bit
            cnt := 4 ; // and reset cnt to 4
            --stuff_bits_added ; // decrement the count of stuff bits added
        }
    }
    
    // print out the results  
    std::cout << "     data stream: " << data_stream << '\n'
    std::cout << "  stuffed stream: " << stuffed_stream << '\n'
    std::cout << "stuff bits added: " << stuff_bits_added << '\n' ;
	std::cout << "de-stuff bits added: " << destuffed_stream << '\n' ;
           
}


Last edited on
Topic archived. No new replies allowed.