Hello NoobCoder2333,
In regards to the question that you deleted. You could start with
https://www.learncpp.com/cpp-tutorial/chapter-10-comprehensive-quiz/ scroll down to "Question #3".
As
lastchance said your code does not compile, so lets take a look at it.
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
|
#include<iostream>
#include<iomanip>
using namespace std;
//global declaration
int i;
//function
char getMenu();
void getBinDisplay(int *ptr, int *xsize);
int getLoc(int *ptr, int size, int *xtarget);
int getSize();
void getInput(double *ptr, int xsize);
double getElim(double *ptr, int xsize);
void getTry();
int main()
{
char sagot = getMenu();
int *pt, *size;
pt = new int;
size = new int[12];
switch (sagot)
{
case'1':
getBinDisplay(pt, size);
break;
case'2':
getSize();
break;
case'3':
break;
}
}
char getMenu()
{
char choice;
do
{
cout << "======CHOICES======" << endl
<< "[1] Binary Searching" << endl
<< "[2]Grade Elimination" << endl
<< "[3] Exit" << endl
<< "Enter choice here: ";
try
{
cin >> choice;
if (choice > '3' || choice < '1')
{
throw choice;
}
}
catch (char c)
{
cout << c << " IS INVALID! PLEASE TRY AGAIN." << endl;
system("pause");
system("cls");
continue;
}
} while (choice != '1' && choice != '2' && choice != '3');
system("cls");
return choice;
}
void getBinDisplay(int *ptr, int *xsize)
{
ptr[xsize] = { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91 };
cout << "Option 1: Binary Searching" << endl
<< "Displaying elements in the array" << endl;
for (i = 0; i < *xsize; i++)
{
cout << setw(6) << ptr[i];
}
cout << endl;
}
//////////////////////////////
/////////////////////////////
int getLoc(int *ptr, int size, int*xtarget)
{
cout << "enter your target key:";
cin >> *xtarget;
for (i = 0; i < size; i++)
{
if (ptr[i] == *xtarget)
{
return i;
}
else i++;
}
cout << *xtarget << "is not in the list" << endl;
return -1;
}
|
I added some blank lines for readability which through the line numbers off from your original code.
Line 7 should look more like this:
1 2 3 4
|
//global declaration
//int i; // <--- Define in for loops not as a global variable,
// <--- Try to avoid all global variables that do not start with "const" or "constexpr".
constexpr int MAXSIZE{ 12 };
|
Defining "i" as a global variable just because it is used in more than 1 function following "main" is not a good idea. "i" should be defined in the for loops and kept as a local variable to the loop.
"MAXSIZE" has more use here. Defined as a constant it can not be changed, so it is not a problem.
To be more accurate line 9 should say "function prototypes" or "function forward declarations".
The function "getSize" is never defined or you just left it out. This causes a problem in "main" when compiled and the function can not be found.
Line 22 creates 2 pointers.
Line 24 creates an address using "new" to a single int. It still eludes me what this is for.
Line 26 creates an array, but has no values for the elements except garbage. This would work better for you:
numArrPtr = new int[MAXSIZE]{ 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91 };
. Notice the name change. This is just a suggestion. Use any name that you like, but make it descriptive of what it is.
In the switch: case 1 is calling the function with 2 parameters, but you could get by with 1.
Case 2 is calling a function that does not exist.
Another thing here is that the switch will only execute once and the program will end.
The "getMenu" function is over done, but does appear to work. We can work on that later if you want.
The "getBinDisplay" function is where you have problems.
In the function you define 2 pointers, but inside the function you are using them wrong.
This line:
ptr[xsize] = { 4, 7, 8, 10, 14, 21, 22, 36, 62, 77, 81, 91 };
is trying to take a pointer to a single int and make an array out of it. Can not be done. Then
ptr[xsize]
. Here you are using a pointer to an array and de-referencing the address pointing to the first element to create an array. The number that you are trying to use would be 4.
Back in "main" if you create the array and initialize it when passed to the function you already have something to work with. You would not need to create an array, that does not work in the first place, you already have 1 to use.
In the for loop you are using the wrong variable. "ptr" is a pointer to a single int and "xsize" is the array that you should be using.
Although it is legal to change the name of a variable in a function that does not mean that you have to every time. As I disused earlier creating the variable "numArrPtr" in "main" and then defining the same name as a parameter in the function makes it easier to keep track of what you are using.
Also in the for loop:
i < *xsize
. The question is what is the value of the 1st element of the array? This is where
i < MAXSIZE
makes use of the constant global variable.
For the "setw" 6 works, but appears on the screen to be a bit big. 4 seemed to work better for this array. Just a thought.
The only thing I have to say about the function "getLoc" is the parameter "int* xtarget". This does not need to be a parameter or even a pointer. It should be defined inside the function where it is used. Unless there is a reason to need its value after the function ends.
Andy