Searching through multiple lists

hey guys, so i need some input on this assignment i'm trying to complete. Here is the link to the code that was supplied: http://pastebin.com/QXwahqts

You will need to complete the following function:
unsigned long search(list<string> &first, list<char> &second, list<int> &third, list<unsigned long> &fourth)

This function takes in four lists and returns a long stored in the fourth list. To find the long in the fourth list you will need to do searches through the first three list according to the following clues. To do these searches you will need to use various functions in the STL You will also need to create an iterator for each of the lists. The iterator that you create depends on the type of list being iterated.


Here is the code I've written for the first two lists you have to search through, and I was wondering if these seem correct? Once these are correct I'm sure I'll have more questions:

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
unsigned long search(list<string> &first, list<char> &second, list<int> &third, list<unsigned long> &fourth)
{
        //declare a string with "Linky" to search
        //through later at the correct index
        string linky  = "Linky";
        int i = k = x = y = 0;

//Clue 1
//Search the first list for the string "Linky" this index will refer to which character in the
//word linky we are looking for

        //create a string iterator and set it equal
        //to the first element in the list
        list<string>::iterator itString;
        itString = first.begin();



        //iterates through all elements of first
        while(itString)
        {
                //if "Linky" is found, program will store
                //the character found within "Linky" at that location
                if (first.at(i) == "Linky"
                {
                        char linkyLetter = string.at(i);
                }
                else
                {
                        i++;
                        itString++;
                }
        }

//Clue 2
//look for the letter found in clue 1 in the second list

        //create a string iterator and set it equal
        //to the first element in the list
        list<char>::iterator itChar;
        itChar = second.begin();

        //iterates through all elements of second
        while(itChar)
        {
                //if the char is found at element (i),
                //the index is recorded in indexThird
                if(second.at(k) == char)
                {
                        int indexThird = k;
                }
                else
                {
                        k++;
                        itChar++
                }
        }
With nothing but a quick glance, things look correct. But here is something I thought about. It may not make a difference in the results, but may aid in maintainability.

One thing I like to do is break the problem down into smaller sections. So the big problem of "I have these 4 lists, and I need information from each" can be broken down to "I need a result from List 1" then "I need a result from list 2" etc.

That makes the "search" method look something like:

1
2
3
4
5
6
7
unsigned long search(list<string> &first, list<char> &second, list<int> &third, list<unsigned long> &fourth)
{
	char whichChar = findChar("Linky", first);
	int whichInt = findInt(whichChar, second);
	int whichLong = findInt(whichInt, third);
	return findLong(whichLong, fourth);
}


Then you can write a function that just tackles the first part of your problem, then move on to the next part of the problem, etc.

This is just a suggestion that works well for me.
I like that suggestion, but I'm not sure if that's how my instructor wants me to write it. He gave us the "clues" within one function, and I think he wants us to put it all within that function. If all looks good, that's awesome.

Question I have now is, am i going to have nested loops/ifs within one main for loop? I guess my question is, after I am going through the first and find the index of the string "Linky", how do I jump out of the while loop to go onto the next one? I don't want to return because it will cut me out of the functional entirely...
Check out the keyword "break." It takes you out of while/do-while/for loop blocks without taking you out of the outer blocks. An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
	int rowLength[] = {1, 2, 3, 2, 1};

	for (int i = 0; i < 5; i++) {
		if (rowLength[i] == 3)
			break;
	}
	cout << "I'm still here!" << endl;
	return 0;
}
Wow, I can't believe I completely forgot about break;. Thank you so much, lol.
Alright so here's where I'm at so far:

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
unsigned long search(list<string> &first, list<char> &second, list<int> &third, list<unsigned long> &fourth)
{
        //declare a string with "Linky" to search
        //through later at the correct index
        string linky  = "Linky";
        char whichChar;
        int i = k = x =  y = 0;
        int whichInt, whichLong;

//Clue 1
//Search the first list for the string "Linky" this index will refer to which character in the
//word linky we are looking for

        //create a string iterator and set it equal
        //to the first element in the list
        list<string>::iterator itString;
        itString = first.begin();

        //iterates through all elements of first
        for(itString; itString!=first.end(); itString++)
        {
                //if "Linky" is found, program will store
                //the character found within "Linky" at that location
                if((*itString) == "Linky"
                {
                        whichChar = string.at(i);
                        break;
                }
                else
                        i++;
        }

//Clue 2
//look for the letter found in clue 1 in the second list

        //create a string iterator and set it equal
        //to the first element in the list
        list<char>::iterator itChar;
        itChar = second.begin();

        //iterates through all elements of second
        for(itChar; itChar!=second.end(); itChar++)
        {
                //if the char is found at element (i),
                //the index is recorded in indexThird
                if((*itChar) == whichChar)
                {
                        whichInt = k;
                        break;
                }
                else
                        k++;
        }

//Clue 3
//find the nth number in the integer list counting from the back(n is from clue 2)

        //creates an int iterator and sets it
        //equal to the end of the third list
        list<int>::iterator itInt;
        itInt = third.end();

        //iterators backwards through the third list
        //indexThird times, and stores that integer value
        for(itInt; itInt!=third.begin(); itInt--)
        {
                //if whichInt = 0, then it has iterated through the list
                //whichInt times and it will store that integer value
                if(whichInt == 0)
                {
                        whichLong = (*itInt);
                        break;
                }
                else
                        whichInt--;
        }


//Clue 4
//The value in clue 3 refers to the location of the combination it is the xth largest
//number in the list. To find it sort the list and iterate through it.


        list<long> * ptr;
        list<long> * ptr2;
        for(x; x < fourth.size(); x++)
        {
                for(y; y < fourth.size()-kx1; j++)
                {
                        ptr = fourth.at(j);
                        ptr2 = fourth.at(j + 1);

                        if((*ptr) > (*ptr2))
                        {



}


I saw how my teacher (in other parts of the code) dereferenced the iterators to print the data they were pointed to, so I did that for the if statements within the while loops. I added "break;" periodically where I thought it needed to break from the loop once it found what it was looking for. I also think I am correct on the code for the third search loop through the third list.

Now what I am working on is the sorting of the fourth list, which I have to do before I search through it. The sort is under "clue 4" in the above code, so can someone take a look at what I have so far and let me know what they think?
Are you allowed to use list::sort?

http://cplusplus.com/reference/stl/list/sort/
Yeah I'm pretty sure i'm allowed to use any part of the STL. As I'm looking at the example that is on that page, I can't understand how to implement it in my program...

EDIT: would I be able to just use fourth.sort(); ?
Last edited on
Well I believe this section of your code is the start of attempting a sort:

1
2
3
4
5
6
7
8
9
10
11
list<long> * ptr;
        list<long> * ptr2;
        for(x; x < fourth.size(); x++)
        {
                for(y; y < fourth.size()-kx1; j++)
                {
                        ptr = fourth.at(j);
                        ptr2 = fourth.at(j + 1);

                        if((*ptr) > (*ptr2))
                        {


Instead, you should be able to sort the list by simply calling "fourth.sort()". Once it is sorted, iterating through it should give values in numerical order.
Alright exactly as I thought. Took a while of looking at the pseudocode; thank you :)

EDIT: Here is the code I have written for the fourth clue. I took out all the jumble I was trying to write and just used fourth.sort();. The fourth for loop is very similar to the for loop in clue 3, except that it's iterating forward through the list this time instead of backwards. This will find the nth largest number in list 4.

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
//Clue 4
//The value in clue 3 refers to the location of the combination it is the xth largest
//number in the list. To find it sort the list and iterate through it.

        //creates a long iterator and sets it
        //equal to the beginning of the fourth list
        list<long>::iterator itLong;
        itLong = fourth.begin();

        //call the sort member function to sort the list
        fourth.sort();

        for(itLong; itLong!=fourth.end(); itLong++)
        {
                if(whichLong == 0)
                {
                        theLong = (*itLong);
                        break;
                }
                else
                        whichLong--;
        }

        return theLong;

}


My question right now, is do I have the longs declared properly?
1
2
3
4
5
6
7
8
unsigned long search(list<string> &first, list<char> &second, list<int> &third, list<unsigned long> &fourth)
{
        //declare a string with "Linky" to search
        //through later at the correct index
        string linky  = "Linky";
        char whichChar;
        int i = k = x =  y = 0;
        int whichInt, whichLong, theLong;

Is what I have right now, but I"m not sure they are correct. I've never really worked with longs.
Last edited on
So I realized in order to declare the long, I need to use unsigned long int theLong. When I compile though, I'm getting problems on each individual for loop I have written:

1
2
3
4
5
linknapped.cpp: In function 'long unsigned int search(std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::list<char, std::allocator<char> >&, std::list<int, std::allocator<int> >&, std::list<long unsigned int, std::allocator<long unsigned int> >&)':
linknapped.cpp:87: warning: statement has no effect
linknapped.cpp:109: warning: statement has no effect
linknapped.cpp:131: warning: statement has no effect
linknapped.cpp:159: warning: statement has no effect


Each of the for loops is essentially for(iterator; iterator!=beginning or end; iterator++ or iterator--), so I thought that was correct?
The compiler is warning about the first part of the for header (for (iterator;). It does nothing. If you have nothing to do or declare there, just leave it empty: for (;. You can leave empty any or all of the three parts: for (;;){ /*...*/ } //loop forever
oh wow, i should have been able to realize that. and thanks for telling me about leaving those parts empty, I was never aware. Anyways, I ran the a.out file and here's the output:

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
~ jn $ a.out
 him again him never Linky see
 y k n i L L i n k y
 62 18 82 59 15 17 66 35 33 18
 2489235779818 3728371571011 3635161695316 2346519731734 2813927134888
3244420958131 1702939490677 2089729610614 2844791332138 206766664786
1174544433757 674544438307 2367383929075 3961951815799 2539235779363
3781581447070 198124689556 465532094530 3533556757969 3896149347262
1652939491132 3644544411280 885655547497 647013574360 2842075282780
1255161716974 1817877761236 1414914801940 2990840713525 2688865407631
2451828372751 2338371583660 917754312637 1755161712424 1506766652956
411581477737 2598988865239 1210470359356 2392816027609 389853082873
4040470333603 1061581471822 3316643179696 2472569113303 659482710049
323803700758 1317383938630 3251704908682 3579112313110 3259606143178
1762816033342 3984791321764 765038264644 2916766640125 2584914791293
2046272820886 1179482705317 1763309860498 1420593814234 70099999363
2792322196813 1783186403527 3029976515638 545408637013 2396149360912
3604174041277 2338988867605 151581480103 1337630852026 3657630830914
1385161715791 141828393772 3750099965875 622816043716 3154420958950
2558248124869 1851211094266 155655554140 129482714872 498001230037
1530470356444 837383942998 1675161713152 988248139156 1879112328580
2933433306640 763803696754 3732445645048 2114174054836 2669359234969
1028371595581 2603556766432 2141828375572 1479853072954 602692587109
1921458007207 1438001221483 1027507398058 1142939495773 111713702687

The code for the safe deposit box is: 2539235779363

Seems to be in working order :). Thank you guys.
Topic archived. No new replies allowed.