Answer to Question #81481 in C++ for Elelwani

Question #81481
Consider the following table:
Pay Code Pay Rate
1 8
2 14
5 10
6 20
7 15
9 16
11 20
Notice that the pay codes in the array are in ascending numerical order. The user will enter the
pay code to search for in the array. Write C++ program that should search for the pay code in the
first column of the array, but the search should begin in the middle row, which is row 4. If the pay
code the user is searching for is located in the first column of row 4, the program should display
the corresponding pay rate from the second column in row 4. If the pay code the user is
searching for is greater than the pay code in row 4’s first column, the search continues in rows 5,
6, and 7. However, if the pay code the user is searching for is less than the pay code in row 4’s
first column, the search should continue in rows 3, 2, and 1.
(40
1
Expert's answer
2018-09-29T05:39:08-0400
#include <iostream>
#include <string>
using namespace std;

int main() {
// array with PayCode and PayRate
int array[7][2] = { {1,8}, {2,14}, {5,10}, {6,20}, {7,15}, {9,16}, {11,20} };

// flag for successful search
bool SearchOk = false;

// input pay code the user is searching for
int PayCode;
cout << "Enter the pay code: ";
cin >> PayCode;

// if the pay code the user is searching for is greater or equal than the pay code
if (PayCode >= array[3][0]) {
for (int i=3; i<7; i++)
if (PayCode == array[i][0]) {
// display the corresponding pay rate from the second column
cout << "Pay Rate is " << array[i][1] << endl;
SearchOk = true;
break;
}
}

// if the pay code the user is searching for is less than the pay code
else {
for (int i=2; i>=0; i--)
if (PayCode == array[i][0]) {
// display the corresponding pay rate from the second column
cout << "Pay Rate is " << array[i][1] << endl;
SearchOk = true;
break;
}
}

// if the pay code the user is searching for not found
if (!SearchOk)
cout << "Pay Code " << PayCode << " is not found" << endl;

// terminate program with code 0
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS