Passing Arrays: Top_Div

Oct 25, 2015 at 11:08pm
I'm a bit confused on how to pass an array that contains a string.
My program is supposed to calculate the winning division of a company's sectors.
The original pseudo code we are going off of is here:

http://toolkit.cs.ohlone.edu/~jond/cs102/top_div_array_start.txt

If you see anything else I can improve on, don't hesitate to let me know, again I'm still learning.

This portion has been solved, I am having a new issue at the bottom of this thread, only deleted because it was taking up space. Thank you!
Last edited on Oct 31, 2015 at 3:34pm
Oct 26, 2015 at 12:02am
print_result takes 3 arguments. You gave it 2 on line 26.

On line 41 of the definition of populate_div_sales, you don't supply the type for div_regions.
Last edited on Oct 26, 2015 at 12:03am
Oct 26, 2015 at 2:38am
Thank you! I got it to compile!
Oct 31, 2015 at 3:31pm
Compiling issue once again:

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

void populate_div_sales(float[], string[], int);
int findHighest (float[], int);
void print_result(float[], string[], int);

int main ()
{
        int top_div_index = 0;
        float div_sales[4];
        string div_regions[4];

        div_regions[0] = "Northeast";
        div_regions[1] = "Southeast";
        div_regions[2] = "Northwest";
        div_regions[3] = "Southwest";
        
        populate_div_sales(div_sales, div_regions, 4);
        
        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);
        
        cout << "debug for top_div_index: " << top_div_index << endl;    
                
        print_result(div_sales, div_regions, 4);
                        
                 
        return 0;
        
        }

void populate_div_sales(float f_div_sales[], string f_div_regions[], int size) {
	for(int i =0; i < size; i++) {
    	cin >> f_div_sales[i];
        	while (f_div_sales[i] <= 0) {
            	cout << "Enter sales for the " << f_div_regions[i] << "division: $";
            }
    }
}

int findHighest(float f_div_sales[], int top_div_index) {

	float greatestSalesAmount = 0;
    int save_index = 0;

    	for(int i = 0; i<index; i++){
        	if(f_div_sales[i] > greatestSalesAmount){
            	greatestSalesAmount = f_div_sales[i];
            	save_index=i;
         	}
		}
	return save_index;
}

void print_result(float f_div_sales[], string f_div_regions[], int index) {
cout << "The div with the most sales is " << f_div_regions[index] << " with $" << f_div_sales[index] << " in sales.\n" << "\n"; 
}



When trying to compile I get:

1
2
3
top_div_array.cpp: In function `int findHighest(float*, int)':
top_div_array.cpp:54: error: ISO C++ forbids comparison between pointer and 
   integer 


Last edited on Oct 31, 2015 at 11:59pm
Nov 1, 2015 at 12:06am
?
Nov 1, 2015 at 8:09am
I don't see any pointer-to-int comparison, but line 53 compares I to a non-existent variable "index". You probably meant "top_div_index".
Nov 1, 2015 at 5:33pm
So I changed index to top_div_index and it compiles. But the code isn't doing what I expected. I might be missing the point of the assignment and not doing it correctly. Here is the ouput:

1
2
3
4
5
6
debug print for array div_sales_array
3.98836e-34
3.99176e-34
3.99158e-34
3.98828e-34
debug for top_div_index: 0


Its supposed to ask the user for its sales, then spit out which division made the highest sales.

Any help please. Where am i going wrong??
Nov 1, 2015 at 6:49pm
I noticed your call to "top_div_index = findHighest(div_sales, top_div_index)" passes top_div_index, which still equals 0, in as the size, so your index of the highest value will never be found. The for loop on line 53 is never executed. You should have "top_div_index = findHighest(div_sales, 4);

The fact you got those wildly outlandish values suggests that your array of sales isn't being populated correctly and the floats all contain "garbage". your populate_div_sales() func looks right to me... any chance you entered strange or invalid (such as letters/punctuation) in put to your floats as part of the cin >> on line 41 ?
Topic archived. No new replies allowed.