finding index of a number

C++,
How do i make choice "C" to find the index of the value entered by the user? Here are my codes.

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

int main()
{
   int arr[20];
   char select;

   cout << "Enter 20 nonnegative integers: ";
   cout << endl;
   for(int i=0; i<20; i++){
    cout << "Element for " << i << ": ";
    cin >> arr[i];
   }
   cout << endl;
   cout << "A. Display\n";
   cout << "B. Sort\n";
   cout << "C. Search\n";
   cout << endl;

   cout << "Please select a choice above: ";
   cin >> select;
   cout << endl;

   switch(select)
   {
       case 'a': cout << "A. You chose to Display";
                cout << endl;
                break;

       case 'b': cout << "B. You chose to Sort";
                cout << endl;
                break;

       case 'c': cout << "C. You chose to Search";
                cout << endl;
                break;
   }

   if(select == 'a')
   {
       cout << "The elements you entered are: ";
       for(int i=0; i<20; i++){
        cout << arr[i] << " ";
       }
   }
   else if(select=='b'){
    for(int i=0; i<20; i++){
        for(int j=0; j<20; j++){
            if(arr[j] > arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    cout << "Sorted Array elements: ";
    for(int i=0; i<20; i++){
        cout << arr[i] << " ";
    }
    cout << endl;
   }
   else if (select=='c'){
        cout << "Enter a number to be searched: ";

   }

    return 0;
}
There are various issues with the code. Why use a switch() and then have if statements? There's also an issue with the bubble sort. Note that c++ has a std::swap() function. There is also a std::sort() function that could be used unless you have to code your own

https://cplusplus.com/reference/algorithm/sort/.

Perhaps something like this:

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

int main() {
	int arr[20] {};

	cout << "Enter 20 non-negative integers:\n";

	for (int i = 0; i < 20; ) {
		cout << "Element for " << i << ": ";
		cin >> arr[i];

		if (arr[i] < 0)
			std::cout << "Number must not be negative\n";
		else
			++i;
	}

	for (char select {}; select != 'd'; ) {
		cout << "\na. Display\n";
		cout << "b. Sort\n";
		cout << "c. Search\n";
		cout << "d. Quit\n";

		cout << "\nPlease select a choice above: ";
		cin >> select;

		switch (select) {
			case 'a':
				cout << "\na. You chose to Display\n";
				cout << "The elements are:\n";

				for (int i = 0; i < 20; i++)
					cout << arr[i] << " ";

				cout << '\n';
				break;

			case 'b':
				cout << "b. You chose to Sort\n";

				for (int i = 0; i < 19; ++i)
					for (int j = 0; j < 19 - i; ++j)
						if (arr[j] > arr[j + 1])
							swap(arr[j], arr[j + 1]);

				break;

			case 'c':
				{
					int n {};

					cout << "C. You chose to Search";
					cout << "\nEnter a number to be searched: ";
					cin >> n;

					bool fnd {};

					for (int i = 0; i < 20; ++i)
						if (arr[i] == n) {
							std::cout << "Number found at index " << i << '\n';
							fnd = true;
							break;
						}

					if (!fnd)
						std::cout << "Number not found!\n";
				}
				break;

			case 'd':
				break;

			default:
				cout << "Invalid option\n";
				break;
		}
	}
}


Also note that where a size of an array is used, this is usually coded as a const variable at the beginning of the program so if the size needs to be changed, there is only one place to change.
Last edited on
Topic archived. No new replies allowed.