### Sorting 2d Array to Find Highest Number

Hi there! I was wondering if someone would be so kind as to look at my code and tell me how I can fix my output! My goal is to get the corresponding name, and the sales amount, that are highest for each Quarter of the Year. This is all MY code I made from scratch. Pretty proud of it. Would love to hear some criticism on anything I could do to make it prettier. Thanks!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` `````` #include #include #include using namespace std; int main( ) { string name[3]; double sales[4][4]; int employeeNumber = 1; int quarter = 1; double highestSales = sales[0][0]; double highestSalesFirst; double highestSalesSecond; double highestSalesThird; double highestSalesFourth; cout.precision(2); for (int i = 0; i < 3; i++) { cout << "Enter the name of employee " << employeeNumber << "." << endl; cin >> name[i]; for (int j = 0; j < 4; j++) { cout << "Enter the sales for quarter " << quarter << "." << endl; cin >> sales[i][j]; quarter++; } quarter = 1; employeeNumber++; } cout << "\n" << endl; cout << "Name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter" << endl; cout << fixed << name[0] << " \$" << sales[0][0] << " \$" << sales[0][1] << " \$" << sales[0][2] << " \$" << sales[0][3] << endl; cout << fixed << name[1] << " \$" << sales[1][0] << " \$" << sales[1][1] << " \$" << sales[1][2] << " \$" << sales[1][3] << endl; cout << fixed << name[2] << " \$" << sales[2][0] << " \$" << sales[2][1] << " \$" << sales[2][2] << " \$" << sales[2][3] << endl; for (int k = 0; k < 3; k++) { if( sales[k][0] > highestSalesFirst ) { highestSalesFirst = sales[k][0]; } } for (int k = 0; k < 3; k++) { if( sales[k][1] > highestSalesSecond ) { highestSalesSecond = sales[k][1]; } } for (int k = 0; k < 3; k++) { if( sales[k][2] > highestSalesThird ) { highestSalesThird = sales[k][2]; } } for (int k = 0; k < 3; k++) { if( sales[k][3] > highestSalesFourth ) { highestSalesFourth = sales[k][3]; } } cout << "\n" << endl; cout << "1st: " << highestSalesFirst << endl; cout << "2nd: " << highestSalesSecond << endl; cout << "3rd: " << highestSalesThird << endl; cout << "4th: " << highestSalesFourth << endl; return 0; } ``````

Here's the output.

Enter the name of employee 1.
a
Enter the sales for quarter 1.
1000
Enter the sales for quarter 2.
2000
Enter the sales for quarter 3.
3000
Enter the sales for quarter 4.
4000
Enter the name of employee 2.
b
Enter the sales for quarter 1.
9000
Enter the sales for quarter 2.
2000
Enter the sales for quarter 3.
1000
Enter the sales for quarter 4.
5000
Enter the name of employee 3.
c
Enter the sales for quarter 1.
8594
Enter the sales for quarter 2.
2342
Enter the sales for quarter 3.
9089
Enter the sales for quarter 4.
9999

 ``` Name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter a \$1000.00 \$2000.00 \$3000.00 \$4000.00 b \$9000.00 \$2000.00 \$1000.00 \$5000.00 c \$8594.00 \$2342.00 \$9089.00 \$9999.00 1st: 9000.00 2nd: 7905617923685723573105887288915988576119202615849845111286267071393744028657026093581182178249767099152164562614556854156253125994337485421054844259202620157417935557688907161167080693155052298608682970938133851611805634081492328534568951495379314389786621378560.00 3rd: 9089.00 4th: 9999.00 ```

I need it to display:

 ``` Name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter a \$1000.00 \$2000.00 \$3000.00 \$4000.00 b \$9000.00 \$2000.00 \$1000.00 \$5000.00 c \$8594.00 \$2342.00 \$9089.00 \$9999.00 b had the most sales at \$9000.00 for quarter 1. c had the most sales at \$2342.00 for quarter 2. c had the most sales at \$9089.00 for quarter 3. c had the most sales at \$9999.00 for quarter 4. ```

Again, thank you for any help that is given!
Last edited on
Alright I have figured out how to make it display the output I want however I cannot for the life of me figure out why the output for quarter 2 is going haywire!!!! Please someone help me! Revisions are as follows:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include #include #include using namespace std; int main( ) { string name[3]; double sales[4][4]; int employeeNumber = 1; int quarter = 1; double highestSales = sales[0][0]; double highestSalesFirst; double highestSalesSecond; double highestSalesThird; double highestSalesFourth; string highestSalesFirstName; string highestSalesSecondName; string highestSalesThirdName; string highestSalesFourthName; cout.precision(2); for (int i = 0; i < 3; i++) { cout << "Enter the name of employee " << employeeNumber << "." << endl; cin >> name[i]; for (int j = 0; j < 4; j++) { cout << "Enter the sales for quarter " << quarter << "." << endl; cin >> sales[i][j]; quarter++; } quarter = 1; employeeNumber++; } cout << "\n" << endl; cout << "Name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter" << endl; cout << fixed << name[0] << " \$" << sales[0][0] << " \$" << sales[0][1] << " \$" << sales[0][2] << " \$" << sales[0][3] << endl; cout << fixed << name[1] << " \$" << sales[1][0] << " \$" << sales[1][1] << " \$" << sales[1][2] << " \$" << sales[1][3] << endl; cout << fixed << name[2] << " \$" << sales[2][0] << " \$" << sales[2][1] << " \$" << sales[2][2] << " \$" << sales[2][3] << endl; for (int k = 0; k < 3; k++) { if( sales[k][0] > highestSalesFirst ) { highestSalesFirst = sales[k][0]; highestSalesFirstName = name[k]; } } for (int k = 0; k < 3; k++) { if( sales[k][1] > highestSalesSecond ) { highestSalesSecond = sales[k][1]; highestSalesSecondName = name[k]; } } for (int k = 0; k < 3; k++) { if( sales[k][2] > highestSalesThird ) { highestSalesThird = sales[k][2]; highestSalesThirdName = name[k]; } } for (int k = 0; k < 3; k++) { if( sales[k][3] > highestSalesFourth ) { highestSalesFourth = sales[k][3]; highestSalesFourthName = name[k]; } } cout << "\n" << endl; cout << highestSalesFirstName << " has the highest sales for quarter 1 at " << highestSalesFirst << endl; cout << highestSalesSecondName << " has the highest sales for quarter 2 at " << highestSalesSecond << endl; cout << highestSalesThirdName << " has the highest sales for quarter 3 at " << highestSalesThird << endl; cout << highestSalesFourthName << " has the highest sales for quarter 4 at " << highestSalesFourth << endl; return 0; } ``````

 ``` Enter the name of employee 1. Blixa Enter the sales for quarter 1. 1500.00 Enter the sales for quarter 2. 3000.00 Enter the sales for quarter 3. 1750.50 Enter the sales for quarter 4. 5000.00 Enter the name of employee 2. Rachel Enter the sales for quarter 1. 8500.00 Enter the sales for quarter 2. 3500.00 Enter the sales for quarter 3. 2500.00 Enter the sales for quarter 4. 1000.00 Enter the name of employee 3. Link Enter the sales for quarter 1. 5600.00 Enter the sales for quarter 2. 3400.00 Enter the sales for quarter 3. 3900.00 Enter the sales for quarter 4. 9000.00 Name 1st Quarter 2nd Quarter 3rd Quarter 4th Quarter Blixa \$1500.00 \$3000.00 \$1750.50 \$5000.00 Rachel \$8500.00 \$3500.00 \$2500.00 \$1000.00 Link \$5600.00 \$3400.00 \$3900.00 \$9000.00 Rachel has the highest sales for quarter 1 at 8500.00 has the highest sales for quarter 2 at 7905617923685723573105887288915988576119202615849845111286267071393744028657026093581182178249767099152164562614556854156253125994337485421054844259202620157417935557688907161167080693155052298608682970938133851611805634081492328534568951495379314389786621378560.00 Link has the highest sales for quarter 3 at 3900.00 Link has the highest sales for quarter 4 at 9000.00 ```
Last edited on
First initialize highestSalesFirst = 0; and take one variable say int highestIndex;
 ``12345678`` ``````for (int k = 0; k < 3; k++) { if( sales[k][0] > highestSalesFirst ) { highestSalesFirst = sales[k][0]; highestIndex = k; } ``````

}
After this for loop you can print it like,
`cout << name[highestIndex<<"had the..........."<<highestSalesFirst<<" for Qt1";`
Last edited on
I have figured that part out. I cannot figure out why I have this huge number for my output.
I guess no one can help me.
You need to initialze all your highestSales... variables to 0:
 ``12345`` `````` double highestSales = 0; double highestSalesFirst = 0; double highestSalesSecond = 0; double highestSalesThird = 0; double highestSalesFourth = 0;``````

Otherwise those variables contain random values that might very well be the greater than one of the `sales` values. It's only by accident that just 'quarter 2' comes out that way
Thank you so much! Your a life saver!
Topic archived. No new replies allowed.