sequence help!

i am doing my assignment but i think i am not understand the question correctly asked in the comment and getting the wrong answer. Can someone please check this and let me know where am i wrong and how to solve it? Thanks

All the instructions are written on the top of the function

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134


namespace main_savitch_3
{
    // CONSTRUCTOR


	sequence::sequence()
	{
		counting = 0;
		occupied = 0;
	}

	// MODIFICATION MEMBER FUNCTIONS

//     void start( )
//     Postcondition: The first item on the sequence becomes the current item
//     (but if the sequence is empty, then there is no current item).

	void sequence::start()
	{
	   counting = 0;
	}

//     void advance( )
//     Precondition: is_item returns true.
//     Postcondition: If the current item was already the last item in the
//     sequence, then there is no longer any current item. Otherwise, the new
//     current item is the item immediately after the original current item.

	void sequence::advance()
	{
		assert(is_item()==true);
		if(occupied==counting)
        {
          arr[counting]=0;
          counting++;
        }
        else
        {
          arr[counting]=arr[counting+1];
          counting++;
        }
    }

//     void insert(const value_type& entry)
//     Precondition: size( ) < CAPACITY.
//     Postcondition: A new copy of entry has been inserted in the sequence
//     before the current item. If there was no current item, then the new entry
//     has been inserted at the front of the sequence. In either case, the newly
//     inserted item is now the current item of the sequence.

	void sequence::insert(const value_type& entry)
	{
    assert (size()< CAPACITY);
    if (is_item() == false){
        counting=0;}
    for (int i= occupied; i>counting;i--)
    {
        arr[i] = arr [i-1];
    }
    arr[counting]= entry;
    occupied++;
	}

//     void attach(const value_type& entry)
//     Precondition: size( ) < CAPACITY.
//     Postcondition: A new copy of entry has been inserted in the sequence after
//     the current item. If there was no current item, then the new entry has
//     been attached to the end of the sequence. In either case, the newly
//     inserted item is now the current item of the sequence.

	void sequence::attach(const value_type& entry)
	{
    assert (size()< CAPACITY);
    if (is_item() == false){
        arr[occupied-1]= entry;}
    for (int i= occupied; i> counting; i--)
    {
        arr[i]= arr[i+1];
    }
    arr[counting]= entry;
    occupied++;

	}

//     void remove_current( )
//     Precondition: is_item returns true.
//     Postcondition: The current item has been removed from the sequence, and //the item after this (if there is one) is now the new current item.   

	void sequence::remove_current()
	{
    assert(is_item()== true);
    for (int i= counting + 1; i< occupied - 1; i++)
    {
        arr [i]= arr[i+1];
        occupied--;
    }
	}

	// CONSTANT MEMBER FUNCTIONS

//   size_type size( ) const
//     Postcondition: The return value is the number of items in the sequence.

	sequence::size_type sequence::size() const
	{
		return occupied;
	}

//   bool is_item( ) const
//     Postcondition: A true return value indicates that there is a valid
//     "current" item that may be retrieved by activating the current
//     member function (listed below). A false return value indicates that
//     there is no valid current item.

	bool sequence::is_item() const
	{
	    if (counting < occupied)
        return true;
        else
        return false;
	}

//     value_type current( ) const
//     Precondition: is_item( ) returns true.
//     Postcondition: The item returned is the current item in the sequence.

	sequence::value_type sequence::current() const
	{
	    assert(is_item()==true);
        return arr[counting];
	}
}
Last edited on
Topic archived. No new replies allowed.