|
-
August 1st, 2010, 05:19 PM
#1
Program won't output array position
Hi, I was given an assignment to create Boolean search that that will look through a list of 20 names and return the position in the array that the name is in (Some people here will probably recognize the program, I'm sure my class book is commonly used). Here's the code:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int SIZE = 20;
string name[SIZE] =
{"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",
"Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
"Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",
"Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"};
int startScan, minIndex;
string fullname;
for (startScan = 0; startScan < (SIZE - 1); startScan++)
{
minIndex = startScan;
fullname = name[startScan];
for(int index = startScan + 1; index < SIZE; index++)
{
if (name[index] < fullname)
{
fullname = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = fullname;
}
string inputname;
cout << "Enter a name and I will tell you what alphabetical position they are in.\n";
cout << "Names should be entered in format 'Last, First'.\n";
getline(cin, inputname);
cout << endl;
for (int count = 0; count < SIZE; count++)
{
cout << name[count] << " \n";
}
int first = 0;
int last = SIZE - 1;
int middle;
int position = -1;
bool found = false;
while (found = false && first <= last)
{
middle = ((first + last) / 2);
if (name[middle] == inputname)
{
found = true;
position = middle;
}
else if (name[middle] > inputname)
last = middle - 1;
else if (name[middle] < inputname)
first = middle + 1;
}
if (position == -1)
{
cout << "That person is not on the list\n";
}
else
cout << inputname << " is position " << position << " of 20.\n";
return 0;
}
It probably looks a bit messy since I haven't organized it into functions yet, but hopefully it's legible. It runs without any problems, but when I enter the name, for some reason it always tells me the name isn't on the list, even if it is (For example, entering "Moreno, Juan" should return position 10, but instead, it just says it's not on the list). My best guess is that position isn't changing and instead remains at -1, but I can't quite understand why this is happening.
Any help with this is greatly appreciated, it's confused me into a corner.
-
August 1st, 2010, 10:09 PM
#2
Re: Program won't output array position
I skim your source code and spot this
while (found=false && ....)
I don't know the rest
-
August 1st, 2010, 10:10 PM
#3
Re: Program won't output array position
 Originally Posted by ForteX
Hi, I was given an assignment to create Boolean search that that will look through a list of 20 names and return the position in the array that the name is in (Some people here will probably recognize the program, I'm sure my class book is commonly used). Here's the code:
First, there is nothing Visual C++ specific in your code, so you should have posted in the Non-Visual C++ forum.
Secondly, did you use the debugger to see what your program is doing? If not, please do so, as using the debugger is part and parcel of learning how to write a program. You cannot write a program that is more than a few lines in length without knowing how to debug it using the debugger. You are supposed to know what every line does, every path the code can take, etc. before you run the program, and have the debugger verify that the program is taking the proper paths, variables have the expected values, etc. You don't just write code, cross your fingers, hope for the best, and if it doesn't work, not know what to do.
Third, I suggest you hard-code a name you know is on the list and look for that name, instead of getting the name from the keyboard. This way, you are not taking the chance of some stray character getting into the keyboard input, messing up the search. Once you can get a hard-coded name to be found, then worry about keyboard input. For example:
Code:
string inputname = "a name on the list";
Then search that name.
Fourth, do you mean "binary search" and not "boolean search"? What's a "boolean search"? If it's a binary search, then the data must be sorted first before you attempt to find anything, otherwise the binary search will not work. Did you check to make sure the names are sorted? I don't see any diagnostic code to see if the names are sorted first -- again, you can't just write programs, and then hope they work without testing each piece out beforehand.
Regards,
Paul McKenzie
-
August 1st, 2010, 11:21 PM
#4
Re: Program won't output array position
 Originally Posted by Paul McKenzie
First, there is nothing Visual C++ specific in your code, so you should have posted in the Non-Visual C++ forum.
My apologies, Microsoft's Visual C++ 2008 is the compiler I use, so i figured it went here.
Secondly, did you use the debugger to see what your program is doing? If not, please do so, as using the debugger is part and parcel of learning how to write a program. You cannot write a program that is more than a few lines in length without knowing how to debug it using the debugger. You are supposed to know what every line does, every path the code can take, etc. before you run the program, and have the debugger verify that the program is taking the proper paths, variables have the expected values, etc. You don't just write code, cross your fingers, hope for the best, and if it doesn't work, not know what to do.
I used it to the best of my ability to trace the values being passed to my variables, but for some reason when I got to the while statement, it just exited the program and never read out anything else, no matter how many breakpoints I placed beyond that point.
Fourth, do you mean "binary search" and not "boolean search"? What's a "boolean search"? If it's a binary search, then the data must be sorted first before you attempt to find anything, otherwise the binary search will not work. Did you check to make sure the names are sorted? I don't see any diagnostic code to see if the names are sorted first -- again, you can't just write programs, and then hope they work without testing each piece out beforehand.
"Boolean search" is me typing while thinking about something else. I did indeed mean binary search. Sorry for that confusion. The data is sorted beforehand, and as it stood at the time of my posting, the code would output the sorted names in a list. Before my original post, I made sure to run all of the necessary checks to make sure that the input I was giving the program was what the program was receiving, and up until it was finished I left the list of sorted names in it as a way to make sure that my input was 100% accurate. I should have included all of this information in my first post, but at the time it slipped my mind.
As it turns out, the while loop that began here:
Code:
while (found = false && first <= last)
needed to be:
Code:
while (first <= last && !found)
It's good to have it fixed now, and thank you very much for your help.
-
August 2nd, 2010, 02:11 AM
#5
Re: Program won't output array position
that's good!
Now it is better for you to start changing this program into a better coding style for better consistency -- the STL C++ way by using
1. std::sort function to sort out the strings in whatever order you wish;
2. std's binary search function to look up the string you are searching
I state this not because I don't like the way exercises first years in colleges are offered, they are there to help students with better logical reasoning. Some old books miss "consistent" points in teaching C++ learners who might not be able to later distinguish which their code snip belongs to (C or C++)
-
August 2nd, 2010, 07:32 AM
#6
Re: Program won't output array position
 Originally Posted by ForteX
As it turns out, the while loop that began here:
Code:
while (found = false && first <= last)
needed to be:
Code:
while (first <= last && !found)
It's good to have it fixed now, and thank you very much for your help.
You missed what was really going on. It was pointed out earlier in the thread. By saying found = false, you're assigning the value false to found. You should have written found == false. Had you made that correction, your while loop would be logically equivalent to what you ended up with. By rewriting it as you did, you accidentally got rid of the improper assignment.
-
August 2nd, 2010, 08:39 AM
#7
Re: Program won't output array position
 Originally Posted by GCDEF
You missed what was really going on. It was pointed out earlier in the thread. By saying found = false, you're assigning the value false to found. You should have written found == false. Had you made that correction, your while loop would be logically equivalent to what you ended up with. By rewriting it as you did, you accidentally got rid of the improper assignment.
I totally agree, he doesn't sound impolite but his ignoring my help shows he does in actuality.
Something like that I think he should have learned it from parents right from early days of earning his first behaviors toward people around.
I am sad! :sad:
No more help for you next time
-
August 2nd, 2010, 05:27 PM
#8
Re: Program won't output array position
 Originally Posted by Turingmachine
I totally agree, he doesn't sound impolite but his ignoring my help shows he does in actuality.
Something like that I think he should have learned it from parents right from early days of earning his first behaviors toward people around.
I am sad! :sad:
No more help for you next time
Oh, I'm sorry. When I checked this thread last night, (at about midnight for me) I completely didn't even notice your post. Had I seen it I would have made sure to thank you for your input as well, since it was dead on the problem.
-
August 2nd, 2010, 06:05 PM
#9
Re: Program won't output array position
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|