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:
unsignedlong search(list<string> &first, list<char> &second, list<int> &third, list<unsignedlong> &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:
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...
unsignedlong search(list<string> &first, list<char> &second, list<int> &third, list<unsignedlong> &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?
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(); ?
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.
//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
unsignedlong search(list<string> &first, list<char> &second, list<int> &third, list<unsignedlong> &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.
So I realized in order to declare the long, I need to use unsignedlongint 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: