Template exercise
Aug 19, 2016 at 5:07pm UTC
i was making a program to practice with Templates and encountered a strange problem:
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
#include<iostream>
#include<new>
using namespace std;
template <class T>
class Vector {
private :
int Vector_Size;
T* Vector_Element;
int Free_Vector_Elements;
int Occupied_Vector_Elements;
int * Free_Element_List;
int * Occupied_Element_List;
public :
Vector() : Vector_Size(0), Vector_Element(0) {}
Vector(T Size) : Vector_Size(Size), Vector_Element(new T[Size]) {
for (int Element = 0; Element < Vector_Size; ++Element) { Vector_Element[Element] = 0; }
}
~Vector() {
delete [] Vector_Element;
delete [] Free_Element_List;
delete [] Occupied_Element_List;
}
T& operator [](int Element);
const T& operator [](int Element) const ;
void Resize_Vector(int Rellocated_Size);
void Scan_Vector();
void Show_Vector_Info();
};
template <class T>
T& Vector<T>::operator [](int Element) { return (Vector_Element[Element]); }
template <class T>
const T& Vector<T>::operator [](int Element) const { return (Vector_Element[Element]); }
template <class T>
void Vector<T>::Resize_Vector(int Rellocated_Size) {
if (Rellocated_Size < Vector_Size) { cout << "Error:smaller size rellocated\n" ; return ; }
int Element;
T* Rellocated_Vector = new (nothrow) T[Rellocated_Size];
if (Rellocated_Vector == 0) { cout << "Error:memory could not be allocated\n" ; return ; }
for (Element = 0; Element < Vector_Size; ++Element) {
Rellocated_Vector[Element] = Vector_Element[Element];
}
for (int Clear = 0; (Clear + Element) < Rellocated_Size; ++Clear) { Rellocated_Vector[Clear + Element] = 0; }
delete [] Vector_Element;
Vector_Element = Rellocated_Vector;
Vector_Size = Rellocated_Size;
}
template <class T>
void Vector<T>::Scan_Vector() {
Free_Vector_Elements = 0;
Occupied_Vector_Elements = 0;
int Element, Free_Counter = 0, Occupied_Counter = 0;
for (Element = 0; Element < Vector_Size; ++Element) {
if (Vector_Element[Element] == 0) { ++Free_Vector_Elements; }
else if (Vector_Element[Element] != 0) { ++Occupied_Vector_Elements; }
}
Free_Element_List = new int [Free_Vector_Elements];
Occupied_Element_List = new int [Occupied_Vector_Elements];
for (Element = 0; Element < Vector_Size; ++Element) {
if (Vector_Element[Element] == 0) { Free_Element_List[Free_Counter] = Element; ++Free_Counter; }
else if (Vector_Element[Element] != 0) { Occupied_Element_List[Occupied_Counter] = Element; ++Occupied_Counter; }
}
}
template <class T>
void Vector<T>::Show_Vector_Info() {
if (Free_Vector_Elements != 0) {
cout << "There are: " << Free_Vector_Elements << " free elements" << endl;
for (int Element = 0; Element < Free_Vector_Elements; ++Element) {
cout << "Element " << Free_Element_List[Element] << ": Free" << endl;
}
}
if (Occupied_Vector_Elements != 0) {
cout << endl;
cout << "There are: " << Occupied_Vector_Elements << " occupied elements" << endl;
for (int Element = 0; Element < Occupied_Vector_Elements; ++Element) {
cout << "Element " << Occupied_Element_List[Element] << ": " << Vector_Element[Occupied_Element_List[Element]] << endl;
}
}
}
int main() {
Vector<int > Test_Vector(5);
Test_Vector.Resize_Vector(10);
Test_Vector.Scan_Vector();
Test_Vector.Show_Vector_Info();
return 0;
}
in line 49 the for loop acts strangely i.e: when i set Clear to 0 the program executes properly but after making a table of program flow i noticed that it should clear the 4th element aswell which is unwanted behavior but it doesn't and strangely it works as if it clears the fifth element but when i set Clear to 1 element 5 doesn't get cleared and it confuses me why? the first loop iteration
sets Rellocated_Vector at counter + element which is 0+4 and should in theory clear the 4th element but it clears the 5th what is the reason?
Aug 19, 2016 at 5:14pm UTC
Other than that my questions are:
-is my code understandable?
-is my code clean?
-is my program efficient or could i remove certain parts to make it better ?
-where can i find medium to high difficulty exercises with templates as mostly the ones i find are too easy or i make them myself like the one above that takes a few hours to come up with something challenging.
thanks in advance for any help!
Topic archived. No new replies allowed.