How to setup a linear/binary array search for a shopping program

So my program is a book store, and I have to create a linear and binary search function that will search for the book ID, the price of said book, and how many the user wishes to purchase, and outputs total price to user... I already have all of my parallel arrays set up and working, and the sorting set up, but I have no clue how to do search functions with this type of program.

I am completely bamboozled... I tried for over 2 hours experimenting, but I couldn't find a solution and ended up just deleting what I had because it just outputted garbage. Search function starts on line 127, its empty, but I need some advice on how to start this type of function that achieves what I listed above...

One thing about parallel arrays... Do I JUST have to find the BookID with the search function and it will automatically pull the price values for each book, since its in the parallel array?

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>

using namespace std;
using std::time;

int Menu(); // Menu for sorting and search options.
void bubbleSort(int [], int); // Applies bubble sort algorithm to sort the elements of an unsorted array
void selectionSort(int [], int); // Applies selection sort algorithm to sort the elements of an unsorted array
void linearSearch(string [], int [], double [], int); // Applies the linear search algorithm to search for book ID
void binarySearch(string [], int [], double [], int); // Applies the binary search algorithm to search for book ID
void display(string [], int [], double [], int); // To display the contents of parallel array in a tabular format



int main()
{
    int size = 10, choice;
    const int SIZE = 1000;
	const int MAXRANGE = 500;
	int selectionsort[ SIZE ] = {0};
	int bubblesort[ SIZE ] = {0};
	time_t t;
	srand((unsigned) time(&t));


    string bookTitle[] = {"Starting out with C++", "Java Programming", "Software Structures",
    "Design and Analysis of Algorithms", "Computer Graphics", "Artificial Intelligence: A Modern Approach",
    "Probability and Statistics", "Cognitive Science", "Modern Information Retrieval", "Speech and Language Processing"};   // Parallel Arrays

    int bookID[] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};

    double bookPrice[] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22,
    120.00, 42.25, 32.11, 123.75};

    while (choice != 5)
    {
        choice = Menu();
        if (choice == 5){
         cout << "Thanks for stopping by!";   break;}
        switch (choice)
        {
        case 1:
            display(bookTitle, bookID, bookPrice, size);
            linearSearch(bookTitle, bookID, bookPrice, size);
            break;
        case 2:
            display(bookTitle, bookID, bookPrice, size);
            binarySearch(bookTitle, bookID, bookPrice, size);
        case 3: // Case that holds bubblesort, in sorted and unsorted forms
            for (int i = 0; i < SIZE; i++)
            {
                bubblesort[i] = (rand()%MAXRANGE)+1;
            }

    cout << "Unsorted bubble sort for 1000 numbers!\n";
	for (int i =0; i< SIZE; i++)
	{
		cout << setw(4) << bubblesort[i];
	}
	cout <<endl;
	cout << "Sorted bubble sort for 1000 numbers!\n";
	// Call the sorting array function
	bubbleSort(bubblesort, SIZE );
	{
		for (int j=0; j< SIZE; j++)
			cout << setw(4) << bubblesort[j];
		cout << endl;
	}
	break;
        case 4: // Case that holds selection sort, in sorted and unsorted forms.
         // Given variable and constant declarations
	// Fill array with random numbers between 1-1000
	for(int i=0; i<SIZE; i++)
	{
		selectionsort[ i ] = (rand()%MAXRANGE)+1;
	}
	// Display the unsorted array
	cout << "Unsorted selection sort for 1000 numbers!\n";
	for (int i =0; i< SIZE; i++)
	{
		cout << setw(4) << selectionsort[i];
	}
	cout <<endl;

	// Display the sorted array
	cout << "Sorted selection sort for 1000 numbers!\n";
	// Call the sorting array function
	selectionSort(selectionsort, SIZE );
	{
		for (int j=0; j< SIZE; j++)
			cout << setw(4) << selectionsort[j];
		cout << endl;
	}
        }

    }
}

int Menu() // Function to display menu
{
    int choice;
    cout << "********** Welcome to the Book Search and Number Sorting System! ********** " << endl << endl;
    cout << "1 -> Linear Search" << endl;
    cout << "2 -> Binary Search" << endl;
    cout << "3 -> Bubble Sort" << endl;
    cout << "4 -> Selection Sort" << endl;
    cout << "5 -> Quit the Program" << endl << endl;
    cout << "******************************* " << endl;
    cout << "Enter your choice:" << endl;
    cin >> choice;
    return choice;

}
void display(string book[], int ID[], double price[], int size) // Function that displays contents of a parallel array.
{
    for (int i = 0; i < size; i++){
    cout <<"Book Name     " << book[i] << endl;
    cout <<"ID            " << ID[i] << endl;
    cout <<"Price         $" << price[i] << endl << endl;}
    cout << "If you can't view entire collection, scroll or make the screen larger!" << endl << endl;

}
void linearSearch(string book[], int ID[], double price[], int size)
{
    cout << "*****Linear search*****" << endl;
    cout << "Please enter the book ID you would like to purchase: " << endl;



}
void binarySearch(string book[], int ID[], double price [], int size)
{
    cout << "*****Binary search*****" << endl;
    cout << "Please enter the book ID you would like to purchase" << endl;
}

void selectionSort( int array[], int size )
{
	int temp; // temporary variable
	int i;
	int j;

	for (i=0; i< size; i++)
	{
		j = i;
		while (j > 0 && (array [j-1] > array [j]))
		{
			temp = array [j];
			array[j] = array [j-1];
			array [j-1] = temp;
			j--;
		}
	}
}
void bubbleSort(int array[], int size)
{
    bool swap;
    int temp;
    do
    {
        swap = false;
        for (int i = 0; i < (size - 1); i++)
        {
            if (array[i] > array[i+1])
            {
                temp = array[i];
                array[i]=array[i+1];
                array[i+1] = temp;
                swap = true;
            }
        }
    } while (swap);
}
Last edited on
I do suggest that you carefully read: http://www.cplusplus.com/reference/algorithm/find/

The std::find returns a value. Your searches do not.
Your searches ask something from the user. The std::find does not. It is totally focused on finding a value and nothing else. It is up to the caller to decide what to do with the result of the search.
Is there an alternative besides std::find? It involves vectors, which I haven't studied in class yet... There should be a way to do this without using find.
I did not say: "use std::find". I said: "read".

The std::find does not involve vectors. The example program that shows the use of std::find did happen to use vectors, because std::find is a generic algorithm that can be used with various containers, including vectors and plain arrays.


Earlier on that page there is text:
The behavior of this function template is equivalent to:

and some code. Can you regognize anything within that code?
Topic archived. No new replies allowed.