division sales (array and functions)

so my code finds the highest division sales but for some reason if i have the northeast as the highest sales , it doesn't recognize it as being the highest division sales! Please someone help.

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


// Function Prototypes
// These prototypes already contain the proper parameters -
// match your work accordingly

void populate_div_sales(float[], string[], int);
int findHighest (float[], int); // this function will now return the index for
// the highest division sales
void print_result(float[], string[], int); // displays result based on the index of
// highest division sales


//*************************************************
//************ main *******************************
//*************************************************
main ()
{
int top_div_index = 0; // will be assigned the index of the top division sales
float div_sales[4]; // array holding the divisions' sales amounts
string div_regions[4]; // array holding division names
  
// populate div_regions array
div_regions[0] = "Northeast";
div_regions[1] = "Southeast";
div_regions[2] = "Northwest";
div_regions[3] = "Southwest";
// etc.
  
populate_div_sales(div_sales, div_regions, 4); // params are already given to
// help get started
  
// leave debug statement in final product
//cout << "debug print for array div_sales_array" << endl;
for (int i=0; i<4; i++) {
cout << div_sales[i] << endl;
}   
  
top_div_index = findHighest(div_sales,4); //will no longer prints the result

// leave debug statement in final product
//cout << "debug for top_div_index: " << top_div_index << endl;
  
print_result(div_sales, div_regions,4 );

return 0;
}

//************ subroutine definitions below *******************

//*************************************************
//************ populate_div_sales *****************
//*************************************************
// The params for the function definition are given to help get started
// - note the f_ preceding variable names. Use
// this convention to help relate and contrast both the calling and the
// receiving variables.
void populate_div_sales(float f_div_sales[], string f_div_regions[], int size )
{
       float sale;
       cout <<"Enter Division Sales for 4 regions\n";
for(int i=0; i<size;i++)
{
   while(true)
   {
      
       cout <<f_div_regions[i]<<": ";
       cin >> sale;
       if(sale >=0)
               {
                   f_div_sales[i]=sale;
                   break;
                     
               }
               else
               {
               cout <<"Sale should be greater or equal to 0, try again....\n";
               }
              
   }
       }
}

//*************************************************
//************ findHighest ************************
//*************************************************
int findHighest (float sales[], int size)
{
float greatestSalesAmount = 0;
int save_index;
  
greatestSalesAmount=sales[0];
for(int i=0; i<size;i++)
{
           if(sales[i]>greatestSalesAmount)
           {
               greatestSalesAmount=sales[i];
               save_index=i;
           }
       }
  
return save_index;
}

//*************************************************
//************ print_result ***********************
//*************************************************
void print_result(float f_sales[], string f_divisions[], int size )
{
        int highest=findHighest(f_sales,4);
       //cout << divsions[i] << " \t"<< sales[i]<< endl;
       cout << "Highest sale: "<< f_sales[highest]<<" "<< f_divisions[highest ];
   
}
Make sure save_index gets initialized.
i did ! and it works now, one more question how did initialize save_index =0 work ?
greatestSalesAmount is initialized to sales[0] so if that is the highest value it means sales[i]>greatestSalesAmount will never be true so save_index will never never be set.
Topic archived. No new replies allowed.