Need to Increment Length of Sorted Array
Jan 13, 2016 at 11:24pm UTC
Hello, I am working on a Sorted Array Program. I need to be able to increment the length of the Sorted Array as I add items into the list. I tried doing this by adding the code line: length++ in my PutItem() function but it is not doing what I want it to do. Length remains 0 no matter what. I appreciate any help offered.
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
#include <iostream>
enum RelationType { LESS, GREATER, EQUAL };
using namespace std;
class ItemType
{
public :
ItemType() {};
RelationType ComparedTo(ItemType otherItem) const ;
void GetValue() const ;
void Initialize(int number);
private :
int value;
};
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::GetValue() const
{
cout << value << endl;
}
void ItemType::Initialize(int number) {
value = number;
}
class SortedType{
public :
SortedType();
int GetLength() const ;
ItemType GetItem(ItemType& item, bool & found);
void PutItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
ItemType GetNextItem(ItemType& item);
void SplitList(SortedType list, ItemType item, SortedType& list1, SortedType& list2);
private :
int length;
ItemType* info;
int currentPos;
};
SortedType::SortedType() {
length = 0;
}
int SortedType::GetLength() const
{
cout << length << endl;
return length;
}
void SortedType::PutItem(ItemType item) {
bool moreToSearch;
int location = 0;
moreToSearch = (location < length);
while (moreToSearch)
{
switch (item.ComparedTo(info[location])) {
case LESS: moreToSearch = false ;
break ;
case GREATER: location++;
moreToSearch = (location < length);
break ;
}
for (int index = length; index > location; index--)
info[index] = info[index - 1];
info[location] = item;
length++;
}
}
int main(){
SortedType List;
List.GetLength(); //results in 0 because no items have been added
ItemType item;
item.Initialize(5);
List.PutItem(item); //adds item into list
List.GetLength(); //after item has been added length remains 0
return 0;
}
Last edited on Jan 13, 2016 at 11:25pm UTC
Jan 13, 2016 at 11:36pm UTC
1 2 3 4
for ( ... )
line 1;
line 2;
line 3;
Is the same as:
1 2 3 4 5
for ( ... )
line 1;
line 2;
line 3;
except that the latter more accurately reflects what's happening.
If you want a loop to govern more than a single line of code, you must make it a compound statement (iow, surround the body with curly brackets.)
1 2 3 4 5 6
for (... )
{
line 1;
line 2;
line 3;
}
That said:
moreToSearch = (location < length);
will never be true since you start with a length of 0.
Last edited on Jan 13, 2016 at 11:37pm UTC
Jan 14, 2016 at 12:18am UTC
I solved it on my own.
Topic archived. No new replies allowed.