Sorting and Searching.

Pages: 123
Did that already. Still don’t know how to do. I’m well aware of a google search engine
Tell me, out of genuine interest, why aren't you taking advantage (assuming you're at some school or other) of sitting down with your tutor, lecturer, teacher or prof?
I'm a student in college taking intro to c++ online. Trust me I do send the teacher a lot of emails but he isn't as helpful. Mainly tells me look in the book, but doesn't break things down for me to understand it.
Well, that's not good if he/she's the only person you can go to, especially if he/she tells everybody 'just look in the book'. I'd go to their principal but that's often easier said than done and you have to be careful.

It still leaves using code from other sources - that's basically the same as what you are doing here.

https://www.geeksforgeeks.org/binary-search/ shows a recursive and iteration way of doing the search - my preference is iteration.

Use that, if you have any specific questions about anything on that page then ask. But it has to be specific - there are no injections for gaining understanding.

You now have the wherewithal and chance to shine with binary searching.
One thing I notice is that you should be using more functions to split the work into manageable chunks.

First start with the first part of the program:


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
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. 

At this point main() might look something like:

int main()
{
    // I prefer std::vector over arrays, as a std::vector is easier and safer, use arrays if that is what you want.
    std::vector<std::string> accounts{"8149420", "5333174", "3080098", "6755963",
                                      "9526981", "4449539", "9387197", "5104726",
                                      "2931356", "4282637", "1750219", "6086650",
                                      "3164838", "2419590", "4578589", "9718904",
                                      "6749941", "2545408"};

    do_linear_search(accounts);

    return 0;
 


Notice how simple main() is, all the work are done by a single function (as far as main() is concerned) or by several small simple functions unknown to main(). Each function should be doing as little as possible. For example a function for getting the account number from the user, another function for printing whether or not the entered, and yet another function to do the actual linear search.

Also note that I use a std::string to hold the account number, because not all "numbers" should be treated as actual numbers. For example an account number many times have both digits and alpha characters.


By the way do you realize that a binary search is usually a recursive algorithm?

RECURSIVE ALGORITHMS can be a little hard to understand at first, IMO.
literally add } at the end of the stuff you posted and take the stray slash out and it runs fine.

then, literally, call your sort program on the array. line 21 is a good spot.
then, literally, iterate over the array printing each value. just make it right after above.
is the array sorted? look at the output. you may want to make your array something easier (comment out the real array and replace it with {10,9,8,7,6,5,4,3,2,1,0} if you want it to be simple to debug).

post your code with those 2 changes (3 if you make the array simple). Show us that you have gotten the sort working.

then we can look at the search.
You have reached the do or do not stage. Honestly, Im trying to keep an open mind but you have been acting like the kids I tutored that just keep saying 'im confused' and 'I dont understand' until someone does it for them.
Last edited on
Jlb I saw your recommendation about starting the first part of the program ? But I’m confused what to do or start first because I don’t want to include redundant things.

So what is the very first thing I should do with my existing code here ?

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
Charles Blackwell CIS 211 Module 9
#include <iostream>
using namespace std;
//Function Prototype
const int number_list = 18;
bool searcharray(int entered_number, int lookup_number[number_list]);
int main()
{
	int account_number[] = { 8149420, 5333174, 3080098, 6755963, 9526981,
4449539, 9387197, 5104726, 2931356, 4282637, 1750219, 6086650,
3164838, 2419590, 4578589, 9718904, 6749941, 2545408 };
	int chargenumber = 0;
	bool find = true;
	cout << "                                   *Charge Account Validation*         " << endl;
	//input of account number
	cout << "_______________________________________________________________________" << endl;
	cout << "Please enter the account number: " << endl;
	cin >> chargenumber;
	//If/else statement based on if the account number is right or wrong
	find = searcharray(chargenumber, account_number);
	if (find==true)
		cout << "\nCharge " << chargenumber << " is a valid number" << endl;
	else
		cout << "\nCharge " << chargenumber << " is not a valid number" << endl;
	return 0;
}
//For loop
bool searcharray( int entered_number, int lookup_number[number_list] )
{
	bool find = false;

	for (int i = 0; i < number_list; i++)
	{
		if (entered_number == lookup_number[i])
			return true;
	}
	return false;
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.




This is what I started with based off your instructions. What would I do next ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

void print_accounts(){
	
}

void user_input(){
	
}

void print_message(){
	
}

int main(){

    
}


Can someone help me figure out the rest

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

void print_accounts(){
	
}

void user_input(){
	int chargenumber = 0;
	cout << "Please enter the account number: " << endl;
	cin >> chargenumber;
}

void search_account(){
	find = searcharray(chargenumber, account_number);
	if (find==true)
		cout << "\nCharge " << chargenumber << " is a valid number" << endl;
	else
		cout << "\nCharge " << chargenumber << " is not a valid number" << endl;
	return 0;
}
}

void print_message(){
	
}

int main(){

    int account_number[] = { 8149420, 5333174, 3080098, 6755963, 9526981,
4449539, 9387197, 5104726, 2931356, 4282637, 1750219, 6086650,
3164838, 2419590, 4578589, 9718904, 6749941, 2545408 };


}


This is what I started with based off your instructions. What would I do next ?

What good are those functions? They are not accepting any values nor are they returning any values.

As you can see, I don’t know what I’m doing. I’m trying to get help with my assignment
I’m trying to get help with my assignment

You need to help yourself. No one is going to do all the work for you. You've been given several hints, yet you've yet to really try to implement the suggestions.

I don’t know what I’m doing.

Maybe you need to put the program away and go back to your course materials and re-read the material. I'm quite sure that your course materials should cover most of the general specifics of this assignment.

As you can see,

Actually what I am seeing is that you are trying to convince someone to write the program for you.
jlb,

I started this thread off with code for my assignment. So please don't say I'm trying to get someone to do the program for me. If that was the case all i would of done was upload the assignment instrutions and have no code and say "Hey can you tell me the code for this".

What you and others on here fail to realize is that I'm a beginner. I just started learning c++. I know the pure basics of cin and cout. Loops, pointers, etc I DO NOT KNOW. Imagine if a person is first starting to learn math and all they know is addition and subtraction. That's the equivalent of me here.
Well then good luck. I save my advice for those who want it.

I do want help but you guysBjarne Stroustrup on here in this beginner forum act like everyone is supposed to be Bjarne Stroustrup
You are the stupidest person ever to appear on the forum.
That's the problem.
We tried.
rude
Imagine if a person is first starting to learn math and all they know is addition and subtraction. That's the equivalent of me here.

Then do problems with addition and subtraction and add in multiply. The program you are trying to solve is more advanced than you can handle if you do not understand a loop. Sorting requires multiple loops with logic added in and more. If you can't loop, you can't do this yet. And, it bears repeating, but you can't binary search if you can't sort.

Data structures and algorithms (this problem is algorithms) problems come after 1-2 courses in basic language and simple problems. You cannot jump into it directly.
Last edited on
Pages: 123