Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When I compile and run the program, no error is given.It gives me an unexpected

ID: 3670490 • Letter: W

Question

When I compile and run the program, no error is given.It gives me an unexpected result, and the output is
-858993460
. I thought it should give me the first
value of the array (check my main.cpp file). Please guide me to the right direction. Thanks!


// main.cpp
1.#include
2.#include "sequenceClass.h"
3.using namespace std;
4.
5.int main()
6.{
7. sequence bag;
8.
9. bag.insert(1);
10. bag.insert(2);
11. bag.insert(3);
12. bag.insert(4);
13. bag.insert(5);
14. bag.start();
15. cout << bag << endl;
16.
17. return 0;
18. }

//sequenceClass.cpp

19.#include
20.#include
21.#include "sequenceClass.h"
22.using namespace std;

//const sequence::size_type sequence::CAPACITY;

23.sequence::sequence()
24.{
25. current_index = 0;
26. used = 0;
27.}
28.
29.sequence::size_type sequence::size() const
30.{
31. return used;
32.
33.}

34.void sequence::start()
35.{
36. current_index = 0;
37.}
38.
39.sequence::value_type sequence::current() const
40.{
41. return data[current_index];
42.
43.}
44.
45.void sequence::advance()
46.{
47. assert(is_item());
48. current_index++;
49.}

//CONSTANT MEMBER FUNCTIONS
50.bool sequence::is_item() const
51.{
52. return current_index < used;
53.}

// insert entry before the current item
54.void sequence::insert(const value_type& entry)
55.{
56. assert(size() < CAPACITY);
57. for (int i = used; i > current_index; i--)

58. {
59. data[i] = data[i - 1];
60. data[current_index] = entry;
61. used++;
62. }
63.}

// insert entry after the current item
64.void sequence::attach(const value_type& entry)
65.{
66. assert(size() < CAPACITY);
67. for (int i = used; i > current_index; i--)
68. {
69. data[i] = data[i + 1];
70. data[current_index] = entry;
71. used++;
72. }
}
73.void sequence::remove_current()
74.{
75. assert(is_item());
76. for (int i = current_index + 1; i < used - 1; i++)
77. {
78. data[i] = data[i + 1];
79. used--;
80. }
}

81.ostream& operator<< (ostream& outs, const sequence& source)
82.{
83. outs << source.current() << " ";
84. return outs;
85.}

// sequenceclass.h

#ifndef SEQUENCECE_CLASS_H
#define SEQUENCECE_CLASS_H

#include // Provides size_t
using namespace std;

class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANT
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;

//CONSTRUCTOR
sequence();

//MODIFICATION MEMBER FUNCTIONS
void start();
void advance();
// insert entry before the current item
void insert(const value_type& entry);
// insert entry after the current item
void attach(const value_type& entry);
void remove_current();

//CONSTANT MEMBER FUNCTIONS
size_type size() const;
bool is_item() const;
value_type current() const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
//NONMEMBER FUNCTIONS for the point class
ostream& operator<< (ostream& outs, const sequence& source);

#endif

[/CODE]

Explanation / Answer

Consider the following program:

//FILE: sequenceClass.h
//CLASS PROVIDED: sequence (a class for a collection of items)
//
//TYPEDEFS and MEMBER CONSTANTS for the sequence class:
//   typedef____ value_type
//       built-in type (int, char, etc)
//
//   typedef____ size_type
//      
//   static const size_type CAPACITY = ____
//       sequence::CAPACITY is the maximum number of items that a bag can hold.
//
//CONSTRUCTOR for the sequence class:
//   sequence( )
//       postcondition: The sequence has been initialized as an empty bag.
//  
//MODIFICATION MEMBER FUNCTIONS for the sequence class:
//   void start( )
//       postcondition: The function returns the first item in the sequence.
//
//   void advance( )
//       postcondition: The function returns the next item in the sequence.
//  
//  
//   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. In either case, the new item is now
//                       the current item of the sequence.
//
//   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. In either case, the new item is now
//                       the current item of the sequence.
//
//   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.
//
//CONSTANT MEMBER FUNCTIONS for the sequence class:
//   size_type size( ) const
//       postcondition: The return value is the number of items in the sequence.
//
//   bool is_item( ) const
//       postcondition: The function returns a boolean value to indicate whether
//                   there actually is another item for current to provide
//                   Ture: a valid "current" item.
//                   False: no valid current item.
//
//   value_type current( ) const
//       postcondition: The function returns a item in the sequence.
//
//
//VALUE SEMANTICS for the sequence class:
//   Assignments and the copy constructor may be used with sequence objects.
//
#ifndef SEQUENCECE_CLASS_H
#define SEQUENCECE_CLASS_H

#include <cstdlib> // Provides size_t
using namespace std;

class sequence
{
public:
   // TYPEDEFS and MEMBER CONSTANT
   typedef int value_type;
   typedef std::size_t size_type;
   static const size_type CAPACITY = 30;
  
   //CONSTRUCTOR
   sequence ( );

   //MODIFICATION MEMBER FUNCTIONS
   void start ( );
   void advance ( );
   // insert entry before the current item
   void insert (const value_type& entry);
   // insert entry after the current item
   void attach (const value_type& entry);
   void remove_current( );
  
   //CONSTANT MEMBER FUNCTIONS
   size_type size( ) const;
   bool is_item( ) const;
   value_type current( ) const;
private:
   value_type data[CAPACITY];
   size_type used;
   size_type current_index;
};
//NONMEMBER FUNCTIONS for the point class
ostream& operator <<(ostream& outs, const sequence& source);


#endif
-------------------------------------------------------------

//File: sequenceClass.cxx
//CLASS IMPLEMENTED: sequence
//INVARIANT for the sequence class:
//
//

#include <iostream>
#include <cassert>
#include <cstdlib>
#include "sequenceClass.h"
using namespace std;

//const sequence::size_type sequence::CAPACITY;

sequence::sequence ( )
{
   current_index = 0;
   used = 0;
}

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

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

sequence::value_type sequence::current( ) const
{
   return data[current_index];
  
}

void sequence::advance ( )
{
   assert (is_item());
   current_index++;
}

//CONSTANT MEMBER FUNCTIONS
bool sequence::is_item( ) const
{
   return current_index < used;
}

// insert entry before the current item
void sequence::insert (const value_type& entry)
{
   assert( size( ) < CAPACITY);
   for (int i = used; i > current_index; i--)
   {
       data[i] = data[i-1];
       data[current_index] = entry;
       used++;
   }  
}
  
// insert entry after the current item
void sequence::attach (const value_type& entry)
{
   assert( size( ) < CAPACITY);
   for (int i = used; i > current_index; i--)
   {
       data[i] = data[i+1];
       data[current_index] = entry;
       used++;
   }  
}
void sequence::remove_current( )
{
   assert (is_item());
   for (int i = current_index+1; i < used-1; i++)
   {
       data[i] = data[i+1];
       used--;
   }
}

ostream& operator <<(ostream& outs, const sequence& source)
{
   outs << source.current() << " ";
   return outs;
}

Driver file
--------------------------------------------------------------
#include <iostream>
#include "sequenceClass.h"
using namespace std;

int main ( )
{
   sequence bag;
   bag.insert(1);
   bag.insert(2);
   bag.insert(3);
   bag.insert(4);
   bag.insert(5);
   bag.start( );
   cout << bag.current( ) << endl;
   system ("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote