But I’m confused what to do or start first because I don’t want to include redundant things. |
Well working on one project at a time until you finish the project would probably be a good start.
Also using a lot of small functions aids in reducing the "redundant things".
So what is the very first thing I should do with my existing code here ? |
Start with your instructions, do each section one at a time. When you finish a section check it off and proceed to the next set of instructions.
Initialize a one-dimensional array with these values. (can be done once in main()).
Have the user enter an account number. (This could be it's own function)
Then use a simple linear search to locate the number entered by the user. (another function here)
(For code re-use the next two sections would be in another separate function)
If the user enters a number that is in the array, the program should display a
message indicating that the account number is valid.
If the user enters a number not in the array, the program should display a
message indicating it is invalid.
|
Break the big problem down to manageable chunks. Use many small functions to simplify re-use.
I've already given you a couple of suggestions as to how I would try to do this assignment.
Remember that there is two major parts to this program, a linear search and a binary search. These two major parts have several smaller parts in common. For example both are using the same array, both require you to get an account "number" from the user to do the particular searches, and both large parts require you to print a found/not found message.
So my first suggestion is to start by creating those "common" aspects. Create at least 3 common functions, one function to print the vector, one function to get the user input, and one function to print the found/not found messages.
These are what my function signatures look like for these "common" methods.
1 2 3 4 5 6
|
// A simple function to print the vector to insure that the data is entered and sorted correctly.
void print_accounts(const std::vector<std::string>& accounts);
// A couple of functions common to both search methods. This is the UI.
std::string get_search_account();
void print_message(const std::string& account_to_find, bool is_found);
|
Remember I'm using std::strings instead of ints, so my methods might differ from yours. And don't forget to document your code.
And also realize that I'm not actually calling these functions from main(), I'm calling them from the "search" functions.