The seqSearch function does not work properly

IF the item is not in the list I get wrong results:

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
    #include<iostream>
#include<cassert>
using namespace std;

template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem) const;
void replaceAt(int location, const elemType& repItem);
void clearList();
int seqSearch(const elemType& item) const;
void insert(const elemType& insertItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType(const arrayListType<elemType>& otherList);
//~arrayListType();
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
//template<class int>
arrayListType<int>::arrayListType(int size)
{
   size = 100;
}                              
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
cout << "Item found at loc: " << loc;
break;
}
if (found)
return loc;
else
return -1;
cout << "Item not found" << endl;
} //end seqSearch

template<class elemType>
void arrayListType<elemType>::insert(const elemType &insertItem)
{
     int loc;
     
     if(length == 0)    //the list is empty
        list[length++] = insertItem;  //insert the item and
                                      //increment the length
     else
        if(length == maxSize)
           cerr<<"Cannot insert in a full list,"<<endl;
        else
        {
            loc = seqSearch(insertItem);
            
            if(loc == -1)    //the item to be inserted
                                //does not exist in the list
             list[length++] = insertItem;
            else
               cerr<<"The item to be inseted is already in "
                   <<"the list. No duplicates are allowed."<<endl;
                   }
}// end insert
template<class elemType>
void arrayListType<elemType>::print() const
{
     for (int i = 0; i < length; i++)
         cout << list[i] << " ";
     cout << endl;
}                   
int main()
{
    arrayListType<int> myList;
    myList.insert(56);
    myList.insert(111);
    myList.insert(54);
    myList.insert(1732);
    myList.print();

     myList.seqSearch(54);
}    

The item is in the list. I get correct results:
1
2
3
C:\Dev-Cpp\Chapter9>seqsearch
56 111 54 1732
Item found at loc: 2

If the item is not in the list. I get wrong results:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    arrayListType<int> myList;
    myList.insert(56);
    myList.insert(111);
    myList.insert(54);
    myList.insert(1732);
    myList.print();

     myList.seqSearch(99954);
}    


Item is not in the list. I get:
1
2
C:\Dev-Cpp\Chapter9>seqsearch
56 111 54 1732

The line:
 
cout << "Item not found" << endl;

does not print.
1
2
3
4
5
if (found)
return loc;
else
return -1;
cout << "Item not found" << endl;

It seems you return before cout...
I have interchanged line 4 and 5 as pointed out above, and now I get:
1
2
3
4
5
6
C:\Dev-Cpp\Chapter9>seqsearch
Item not found
Item not found
Item not found
56 111 54 1732
Item not found

It prints the list and it prints
 
Item not found.

which is correct.
I do not know about the three lines above:
1
2
3
Item not found
Item not found
Item not found
You may change it like so:
1
2
3
4
5
if (found)
  return loc;

cout << "Item not found" << endl;
return -1;
That still does not work
What does 'not work' mean?

By the way: What about line 35?
What if you added braces?
1
2
3
4
5
6
if (found)
return loc;
else {
cout << "Item not found" << endl;
return -1;
}

I would actually recommend removing the else statement altogether.

1
2
3
4
5
6
7
...
if (found)
    return loc;

cout << "Item not found" << endl;
return -1;
...


[codetemplate <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)][/code]

By the way what happens if your template class "elemType" is a non-numeric type for example a C-string (arrayListType<char*>)?

That still did not solve the problem jlb
Please post your modified code and tell us what the problem is. I.e. which suggestion did you use?

This is wrong:
1
2
3
4
arrayListType<int>::arrayListType(int size)
{
   size = 100;
}
You corrected it already in another thread!
Topic archived. No new replies allowed.