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

Create a class to act as a generic array (i.e. the user will be able to choose t

ID: 3673992 • Letter: C

Question

Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on the entire array. Errors within operators and constructors will be handled with exceptions (try, throw, catch). Demonstrate that your array operators work inside a function if the array is passed in by reference as a “const”. Provide a Copy method to provide the same functionality as the = operator. Also provide GetAt and SetAt methods to provide ways of getting and setting values at a particular index in the array.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Current Code that I have

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Main.Cpp

#include <iostream>

using namespace std;

#include "Second.h"
#include "Header.h"


using namespace DynamicArray;
void main()
{
   const int LB(-5);
   const int UB(15);
   int   i = 10;
   Array <int, 0, 15>       I1;
   //   Array <int, 1, i>       I2;   // variables cannot be used for a value argument to a template
   Array <int, -5, 5>       I3;
   Array <int *, 0, 8>       I4;
   Array <int, 0, 15>       I6;
   Array <double, LB, UB>   D1;   // named constants can be used for a value argument to a template
                               //   Array <int, 5, -5>       I7;
   Array <int, 5, 5>       I8;
   DynamicArray::Array <int>   I9(20);

   Array <Array <int, 0, 8>, -5, 5>   A2D;

   //   I1 = I3; // these are not the same type of array because bounds listed in template arguments are different
   I1 = I6;

   I1[3] = 3;
   //   std::cout << I1 [3] << std::endl;
   cout << I1[3] << endl;

   A2D[-3][5] = 0;
   for (i = LB; i < UB; i++)
       //   std::cout << D1 [i] << std::endl;
       cout << D1[i] << endl;

   //   std::cout << I9 [3] << std::endl;
   cout << I9[3] << endl;
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Second.h

#ifndef SECOND_H
#define SECOND_H

namespace DynamicArray
{

#include <assert.h>
#include <memory.h>

   //   NOTES:
   //       Whatever data type (class) you use for the template argument below
   //       MUST support the overloaded > for comparison purposes

   template <class T>
   class Array
   {
   public:
       Array();
       Array(const Array <T> &);
       Array(int);
       ~Array();
       T &           At(int);
       T           At(int) const;
       Array <T> &   Copy(const Array <T> &);
       void       Sort();
       Array <T> &   operator =   (const Array <T> &);
       T &           operator []   (int);
       T           operator []   (int) const;
   private:
       T *       pData;
       int       NumElements;
   };

   template <class T>
   inline T & Array <T>::At(int i)
   {
       return (*this)[i];
   }

   template <class T>
   inline T Array <T>::At(int i) const
   {
       return operator [] (i);
   }

   template <class T>
   inline Array <T> & Array <T>::Copy(const Array <T> & A)
   {
       return operator = (A);
   }

   template <class T>
   Array <T>::Array()
   {
       NumElements = 1;
       pData = new T[NumElements];
   }

   template <class T>
   Array <T>::Array(const Array <T> & A)
   {
       int       i;
       NumElements = A.NumElements;
       pData = new T[NumElements];
       for (i = 0; i < NumElements; i++)
           pData[i] = A.pData[i];
       //       memcpy (pData, A.pData, NumElements * sizeof (T));
   }

   template <class T>
   Array <T>::Array(int Size)
   {
       assert(Size > 0);
       NumElements = Size;
       pData = new T[NumElements];
   }

   template <class T>
   Array <T>::~Array()
   {
       delete[] pData;
   }

   template <class T>
   void Array <T>::Sort()
   {
       int       i;
       int       Num;
       bool   Sorted;
       T       Temp;

       Num = NumElements - 1;
       do {
           Sorted = true;
           for (i = 0; i < Num; i++)
               if (pData[i] > pData[i + 1])
               {
                   Temp = pData[i];
                   pData[i] = pData[i + 1];
                   pData[i + 1] = Temp;
                   Sorted = false;
               }
       } while (!Sorted);
   }

   template <class T>
   Array <T> & Array <T>::operator = (const Array <T> & A)
   {
       if (NumElements != A.NumElements)
       {
           delete[] pData;
           NumElements = A.NumElements;
           pData = new T[NumElements];
       }
       else;
       for (int i = 0; i < NumElements; i++)
           pData[i] = A.pData[i];
       return *this;
   }

   template <class T>
   T & Array <T>::operator [] (int i)
   {
       assert((i >= 0) && (i < NumElements));
       return pData[i];
   }

   template <class T>
   T Array <T>::operator [] (int i) const
   {
       assert((i >= 0) && (i < NumElements));
       return pData[i];
   }

};
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Header.h

#ifndef ARRAY_H
#define ARRAY_H

#include <assert.h>
#include <memory.h>

template <class T, int LB, int UB>
class Array
{
public:
   Array();
   Array(const Array <T, LB, UB> &);
   ~Array();
   T &       At(int);
   T       At(int) const;
   Array <T, LB, UB> & Copy(const Array <T, LB, UB> &);
   void   Sort();
   Array <T, LB, UB> & operator = (const Array <T, LB, UB> &);
   T &       operator [] (int);
   T       operator [] (int) const;
private:
   T   data[UB - LB + 1];

   template <class T, int LB, int UB>
   inline T & Array <T, LB, UB>::At(int i)
   {
       return (*this)[i];
   }

   template <class T, int LB, int UB>
   inline T Array <T, LB, UB>::At(int i) const
   {
       return operator [] (i);
   }

   template <class T, int LB, int UB>
   inline Array <T, LB, UB> & Array <T, LB, UB>::Copy(const Array <T, LB, UB> & A)
   {
       return operator = (A);
   }

   template <class T, int LB, int UB>
   Array <T, LB, UB>::Array()
   {
   }

   template <class T, int LB, int UB>
   Array <T, LB, UB>::Array(const Array <T, LB, UB> & A)
   {
       int       i;
       for (i = 0; i < (UB - LB); i++)
           data[i] = A.data[i];

   }

   template <class T, int LB, int UB>
   Array <T, LB, UB>::~Array()
   {
   }

   template <class T, int LB, int UB>
   void Array <T, LB, UB>::Sort()
   {
       int       i;
       int       Num;
       bool   Sorted;
       T       Temp;

       Num = NumElements - 1;
       do {
           Sorted = true;
           for (i = 0; i < Num; i++)
               if (data[i] > data[i + 1])
               {
                   Temp = data[i];
                   data[i] = data[i + 1];
                   data[i + 1] = Temp;
                   Sorted = false;
               }
       } while (!Sorted);
   }

   template <class T, int LB, int UB>
   Array <T, LB, UB> & Array <T, LB, UB>::operator = (const Array <T, LB, UB> & A)
   {
       if (NumElements != A.NumElements)
       {
           delete[] data;
           NumElements = A.NumElements;
           data = new T[NumElements];
       }
       else;
       for (int i = 0; i < NumElements; i++)
           data[i] = A.data[i];
       return *this;
   }

   template <class T, int LB, int UB>
   T & Array <T, LB, UB>::operator [] (int i)
   {
       assert((i >= LB) && (i <= UB));
       return data[i - LB];
   }

   template <class T, int LB, int UB>
   T Array <T, LB, UB>::operator [] (int i) const
   {
       assert((i >= 0) && (i < NumElements));
       return data[i - LB];
   }
};

#endif

Explanation / Answer

Array.h

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

#ifndef ARRAY_H

#define ARRAY_H

#include <assert.h>

#include <memory.h>

template <class T, int LB, int UB>

        class Array

               {

               public:

                                      Array ();

                                      Aray (const Array <T,LB, UB> &);

                                      ~Array ();

                       T &            At             (int);

                       T              At             (int) const;

                       Array <T, LB, UB> & Copy (const Array <T,LB, UB> &);

                       void    Sort    ();

                       Array <T, LB, UB> & operator = (const Array <T, LB, UB> &);

                       T &            operator [] (int);

                       T              operator [] (int) const;

               private:

                       T       Data [UB - LB + 1];

template <class T, int LB, int UB>

        inline T & Array <T, LB, UB>::At (int i)

               {

               return (*this) [i];

               }

template <class T, int LB, int UB>

        inline T Array <T, LB, UB>::At (int i) const

               {

               return operator [] (i);

               }

template <class T, int LB, int UB>

        inline Array <T, LB, UB> & Array <T, LB, UB>::Copy (const Array <T, LB, UB> & A)

               {

               return operator = (A);

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::Array ()

               {

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::Array (const Array <T, LB, UB> & A)

               {

               int            i;

               for (i = 0; i < (UB - LB); i++)

                       Data [i] = A.Data [i];

//             memcpy (Data, A.Data, NumElements * sizeof (T));

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::~Array ()

               {

               }

template <class T, int LB, int UB>

        void Array <T, LB, UB>::Sort ()

               {

               int            i;

               int            Num;

               bool    Sorted;

               T              Temp;

               Num = NumElements - 1;

               do      {

                       Sorted = true;

                       for (i = 0; i < Num; i++)

                               if (Data [i] > Data [i + 1])

                                              {

                                              Temp                   = Data [i];

                                              Data [i]               = Data [i + 1];

                                              Data [i + 1]   = Temp;

                                              Sorted                 = false;

                                              }

                       } while (!Sorted);

               }

template <class T, int LB, int UB>

        Array <T, LB, UB> & Array <T, LB, UB>::operator = (const Array <T, LB, UB> & A)

               {

               if (NumElements != A.NumElements)

                               {

                               delete [] Data;

                               NumElements    = A.NumElements;

                               Data           = new T [NumElements];

                               }

                       else;

               for (int i = 0; i < NumElements; i++)

                       Data [i] = A.Data [i];

               return *this;

               }

template <class T, int LB, int UB>

        T & Array <T, LB, UB>::operator [] (int i)

               {

               assert ((i >= LB) && (i <= UB));

               return Data [i - LB];

               }

template <class T, int LB, int UB>

        T Array <T, LB, UB>::operator [] (int i) const

               {

               assert ((i >= 0) && (i < NumElements));

               return Data [i - LB];

               }

#endif



Array2.h

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
135
136

#ifndef ARRAY2_H

#define ARRAY2_H

namespace DynamicArray

        {

#include <assert.h>

#include <memory.h>

//      NOTES:

//             Whatever data type (class) you use for the template argument below

//             MUST support the overloaded > for comparison purposes

template <class T>

        class Array

               {

               public:

                                              Array          ();

                                              Array          (const Array <T> &);

                                              Array          (int);

                                              ~Array         ();

                       T &                    At                     (int);

                       T                      At                     (int) const;

                       Array <T> &    Copy           (const Array <T> &);

                       void           Sort           ();

                       Array <T> &    operator =     (const Array <T> &);

                       T &                    operator []    (int);

                       T                      operator []    (int) const;

               private:

                       T *            pData;

                       int            NumElements;

               };

template <class T>

        inline T & Array <T>::At (int i)

               {

               return (*this) [i];

               }

template <class T>

        inline T Array <T>::At (int i) const

               {

               return operator [] (i);

               }

template <class T>

        inline Array <T> & Array <T>::Copy (const Array <T> & A)

               {

               return operator = (A);

               }

template <class T>

        Array <T>::Array ()

               {

               NumElements    = 1;

               pData          = new T [NumElements];

               }

template <class T>

        Array <T>::Array (const Array <T> & A)

               {

               int            i;

               NumElements    = A.NumElements;

               pData          = new T [NumElements];

               for (i = 0; i < NumElements; i++)

                       pData [i] = A.pData [i];

//             memcpy (pData, A.pData, NumElements * sizeof (T));

                }

template <class T>

        Array <T>::Array (int Size)

               {

               assert (Size > 0);

               NumElements = Size;

               pData          = new T [NumElements];

               }

template <class T>

        Array <T>::~Array ()

               {

               delete [] pData;

               }

template <class T>

        void Array <T>::Sort ()

               {

               int            i;

               int            Num;

               bool    Sorted;

               T              Temp;

               Num = NumElements - 1;

               do      {

                       Sorted = true;

                       for (i = 0; i < Num; i++)

                               if (pData [i] > pData [i + 1])

                                              {

                                              Temp                   = pData [i];

                                              pData [i]              = pData [i + 1];

                                              pData [i + 1] = Temp;

                                              Sorted                 = false;

                                              }

                       } while (!Sorted);

               }

template <class T>

        Array <T> & Array <T>::operator = (const Array <T> & A)

               {

               if (NumElements != A.NumElements)

                               {

                               delete [] pData;

                               NumElements    = A.NumElements;

                               pData          = new T [NumElements];

                               }

                       else;

               for (int i = 0; i < NumElements; i++)

                       pData [i] = A.pData [i];

               return *this;

               }

template <class T>

        T & Array <T>::operator [] (int i)

               {

               assert ((i >= 0) && (i < NumElements));

               return pData [i];

               }

template <class T>

        T Array <T>::operator [] (int i) const

               {

               assert ((i >= 0) && (i < NumElements));

               return pData [i];

               }

        };

#endif


Main.cpp

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

#include <iostream>

using namespace std;

#include "Array2.h"

#include "Array.h"

//using namespace DynamicArray;

void main ()

        {

        const int LB (-5);

        const int UB (15);

        int     i = 10;

        Array <int, 0, 15>             I1;

//      Array <int, 1, i>              I2;     // variables cannot be used for a value argument to a template

        Array <int, -5, 5>             I3;

        Array <int *, 0, 8>            I4;

        Array <int, 0, 15>             I6;

        Array <double, LB, UB> D1;     // named constants can be used for a value argument to a template

//      Array <int, 5, -5>             I7;

        Array <int, 5, 5>              I8;

        DynamicArray::Array <int>      I9 (20);

        Array <Array <int, 0, 8>, -5, 5>      A2D;

//      I1 = I3; // these are not the same type of array because bounds listed in template arguments are different

        I1 = I6;

        I1 [3] = 3;

//      std::cout << I1 [3] << std::endl;

        cout << I1 [3] << endl;

        A2D [-3] [5] = 0;

        for (i = LB; i < UB; i++)

        //      std::cout << D1 [i] << std::endl;

               cout << D1 [i] << endl;

//      std::cout << I9 [3] << std::endl;

        cout << I9 [3] << endl;

        }

Edit & Run

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

#ifndef ARRAY_H

#define ARRAY_H

#include <assert.h>

#include <memory.h>

template <class T, int LB, int UB>

        class Array

               {

               public:

                                      Array ();

                                      Aray (const Array <T,LB, UB> &);

                                      ~Array ();

                       T &            At             (int);

                       T              At             (int) const;

                       Array <T, LB, UB> & Copy (const Array <T,LB, UB> &);

                       void    Sort    ();

                       Array <T, LB, UB> & operator = (const Array <T, LB, UB> &);

                       T &            operator [] (int);

                       T              operator [] (int) const;

               private:

                       T       Data [UB - LB + 1];

template <class T, int LB, int UB>

        inline T & Array <T, LB, UB>::At (int i)

               {

               return (*this) [i];

               }

template <class T, int LB, int UB>

        inline T Array <T, LB, UB>::At (int i) const

               {

               return operator [] (i);

               }

template <class T, int LB, int UB>

        inline Array <T, LB, UB> & Array <T, LB, UB>::Copy (const Array <T, LB, UB> & A)

               {

               return operator = (A);

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::Array ()

               {

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::Array (const Array <T, LB, UB> & A)

               {

               int            i;

               for (i = 0; i < (UB - LB); i++)

                       Data [i] = A.Data [i];

//             memcpy (Data, A.Data, NumElements * sizeof (T));

               }

template <class T, int LB, int UB>

        Array <T, LB, UB>::~Array ()

               {

               }

template <class T, int LB, int UB>

        void Array <T, LB, UB>::Sort ()

               {

               int            i;

               int            Num;

               bool    Sorted;

               T              Temp;

               Num = NumElements - 1;

               do      {

                       Sorted = true;

                       for (i = 0; i < Num; i++)

                               if (Data [i] > Data [i + 1])

                                              {

                                              Temp                   = Data [i];

                                              Data [i]               = Data [i + 1];

                                              Data [i + 1]   = Temp;

                                              Sorted                 = false;

                                              }

                       } while (!Sorted);

               }

template <class T, int LB, int UB>

        Array <T, LB, UB> & Array <T, LB, UB>::operator = (const Array <T, LB, UB> & A)

               {

               if (NumElements != A.NumElements)

                               {

                               delete [] Data;

                               NumElements    = A.NumElements;

                               Data           = new T [NumElements];

                               }

                       else;

               for (int i = 0; i < NumElements; i++)

                       Data [i] = A.Data [i];

               return *this;

               }

template <class T, int LB, int UB>

        T & Array <T, LB, UB>::operator [] (int i)

               {

               assert ((i >= LB) && (i <= UB));

               return Data [i - LB];

               }

template <class T, int LB, int UB>

        T Array <T, LB, UB>::operator [] (int i) const

               {

               assert ((i >= 0) && (i < NumElements));

               return Data [i - LB];

               }

#endif

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