samedi 20 août 2016

for loop not executing propperly

i was making a program to practice with Templates and encountered a strange problem:

[code]
#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;
}

[/code]

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?

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!

Aucun commentaire:

Enregistrer un commentaire