Array help

I have a program that asks a user to input the sales of 4 divisions (Northwest, Southwest, southeast, northeast) and it outputs which is the largest. We did the same assignment last week but using individual variables instead of arrays. Here it is for reference of what the output is supposed to be. http://cpp.sh/44fmk
I'm not sure what the errors mean on the one I am currently working on. Here is the shell link http://cpp.sh/6k3ca. I will post code too

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
  #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 *******************************
//*************************************************
int 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[4] = "Southwest";
	
	
	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, top_div_index); //will no longer print 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 f_size )
{
    for(int i = 0; i<f_size; i++){
        cin >> f_div_sales[i];
            // loop to input quarterly sales
            while (f_div_sales[i] <=0){
                cout << "Enter quarterly sales for the " << f_div_regions[i] << "division:$ ";
                }
    }
}

//*************************************************
//************ findHighest ************************
//*************************************************
int findHighest (float f_div_sales[], int top_div_index)
{
	float greatestSalesAmount = 0;
	int save_index;
	    for(int i =0; i < top_div_index; i++) {
	        if(f_div_sales[i] > greatestSalesAmount){
	            greatestSalesAmount = f_div_sales[i];
	            save_index = i;
	        }
	    }
	// Use a 'for' loop to help determine the array element with highest sales and
	// saves the winner's index to save_index
	
	return save_index;
}

//*************************************************
//************ print_result ***********************
//*************************************************
void print_result(float f_div_sales[], string f_div_regions[], int index)
{
    cout << "The division with the highest sales is " << f_div_regions[index] << "with $" << f_div_sales[index] << "\n";
	// Be sure to include the division name and quarterly Sales in the final display
}
error is:
In function 'int findHighest(float*, int)':
99:9: warning: 'save_index' may be used uninitialized in this function [-Wmaybe-uninitialized]
In function 'int main()':
56:41: warning: 'save_index' may be used uninitialized in this function [-Wmaybe-uninitialized]
Hi,

Because the assignment statement on line 83 is part of an if statement, it may not happen if the condition is false.

You could have an else statement that sets the value of save_index to a negative number just to keep the compiler quiet. EDIT: Alternatively assign a value to it when it is declared.

Also set the initial highest amount to the first value in the array, is the normal way of doing things.
Last edited on
How do I do that else statement my teacher isn't that good
Might be easier to initialize the variable on line 79.

Otherwise look at a tutorial to see how to do else statements. There is one on this site.

Good Luck !!
Topic archived. No new replies allowed.