Multidimensional Arrays

I have an assignment that requires multidimensional arrays. I have succesfully inputted the values, but I am not sure how to do the rest of the processes with the array.

The assignment is to take in sales for 4 stores for 3 months. I then need to sum the sales for each store. They would like for me to identify the highest sum, and divide the other sums by it. That number represents how many "*" to output on a bar graph.

So far, I've been doing the addition and division manually. Any help? Also my function I am trying to pass Quarter_sales is not working properly.

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
//Setup standard environment
#include <iostream>
#include <iomanip>
#include <cmath> 
using namespace std;


void title ()
{
// Output program name
cout << endl;
cout<< "                    Store Bar Chart- Store Comparison              " <<endl<<endl<<endl;
}

int Quarter_sales()
{  

int sales[12][12];
int i, j;
const int STORE=5;
const int MONTHS=4;
	
	for(i=1;i<STORE;i++)
    {
        for(j=1;j<MONTHS;j++)
        {
        cout<<"Store "<<i<<" Month "<<j<<":        $";
        cin>>sales[i][j];
        }
        
    }

	return sales[12][12];
}

int main()
{
    title();
    int i,j,k,l,m;
    int sales[12][12];
	int total1,total2,total3,total4,output1,output2,output3,output4;
	const int STORE=5;
	const int STORE1=2;
    const int MONTHS=4;


    cout<<    "Input store sales for the quarter by months. Negative values are not allowed."<<endl<<endl;
    cout<<"Please enter store sales in whole dollar values"<<endl<<endl<<endl;

//I need to be able to pass sales[][]
Quarter_sales();



total1=sales[1][1]+sales[1][2]+sales[1][3];
total2=sales[2][1]+sales[2][2]+sales[2][3];
total3=sales[3][1]+sales[3][2]+sales[3][3];
total4=sales[4][1]+sales[4][2]+sales[4][3];

output1=total1/2000;
output2=total2/2000;
output3=total3/2000;
output4=total4/2000;




        return 0;
}
I think this would be the correct code

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
//Setup standard environment
#include <iostream>
#include <iomanip>
#include <cmath> 
using namespace std;

#define STORE 5
#define STORE1 2
#define MONTHS 4


void title () {
    // Output program name
    cout << endl;
    cout<< "                    Store Bar Chart- Store Comparison              " <<endl<<endl<<endl;
}

// returns an array of a 12 x 12 data
int Quarter_sales() {  
    int sales[12][12];
    const int STORE=5;
    const int MONTHS=4;
    
    // you'll want to use 0 here instead of 1, it more common in C++
    for(int i = 0; i < STORE; i++) {
        for(int j = 1; j < MONTHS; j++) {
            cout << "Store: "<< i <<", Month: "<< j <<" -     $";
            cin >> sales[i][j];
            cout << "\n";
        }
    }

    // you need to return the array variable name, not data from the array
    return sales;
}

int main() {
    // setup some variables
    int sales[12][12];
    int total1, total2, total3, total4, output1, output2, output3, output4;

    // display the intro
    title();

    cout << "Input store sales for the quarter by months. Negative values are not allowed." << "\n\n";
    cout << "Please enter store sales in whole dollar values" << "\n\n\n";

    sales = Quarter_sales();

    // and do some math
    total1 = sales[0][0] + sales[0][1] + sales[0][2];
    total2 = sales[1][0] + sales[1][1] + sales[1][2];
    total3 = sales[2][0] + sales[2][1] + sales[2][2];
    total4 = sales[3][0] + sales[3][1] + sales[3][2];
    output1 = total1 / 2000;
    output2 = total2 / 2000;
    output3 = total3 / 2000;
    output4 = total4 / 2000;

    return 0;
}



That should run, but I'm away from a compiler right now so I can't really test it.

And for returning arrays, you can't return like this:

 
return sales[0][0];


Thats for returning the data stored in the array at section (0, 0);

you need to just return the array without those brackets. You might also want to make sure that the array your filling with the function is the same size as the returned thing.

This will produce an error:

1
2
3
4
5
6
int returnArray() {
    int tempArray[5] = {1, 2, 3, 4, 5 };
    return tempArray;
}

int array[2] = returnArray();


This works though:
1
2
3
4
5
6
int returnArray() {
    int tempArray[] = {1, 2, 3, 4, 5 };
    return tempArray;
}

int array[] = returnArray();
Last edited on
Topic archived. No new replies allowed.