How to sum rows and sections in 3d array.

Dec 8, 2015 at 1:45pm
Hi guys,

I am new to programming and I am new to the site as well. This is my very first post and I am finishing my very first semester in college. I am having a very hard time figuring out how to add the rows in my 3d array. I have a total printed to the screen, but I am not sure how to really calculate the total. My brain has been pounding and I am sure that it is something small that I am doing wrong or missing. Please advise.


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

const int NUM_SECTIONS = 3,
ROWS_IN_SECTION = 5,
SEATS_IN_ROW = 8;




typedef double seatTable[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW];

// Function prototypes
void fillArray(seatTable);
void showArray(const seatTable, double price);


int main()
{

double price = 150.00;
double f = price,
s = f - (f * 0.10),
t = s - (s * 0.10),
l = t - (t * 0.15);
double finalPrice;


// Define 3-D array to hold seat prices
double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW] = {{{f,f,f,f,f,f,f,f},
{f,f,f,f,f,f,f,f},
{f,f,f,f,f,f,f,f},
{s,s,s,s,s,s,s,s},
{s,s,s,s,s,s,s,s}},

{{t,t,t,t,t,t,t,t},
{t,t,t,t,t,t,t,t},
{t,t,t,t,t,t,t,t},
{t,t,t,t,t,t,t,t},
{t,t,t,t,t,t,t,t}},

{{l,l,l,l,l,l,l,l},
{l,l,l,l,l,l,l,l},
{l,l,l,l,l,l,l,l},
{l,l,l,l,l,l,l,l},
{l,l,l,l,l,l,l,l}}};




showArray(seats, price);
finalPrice = (f * 24) + (s * 16) + (t * 40) + (s * 40);
cout << "\nThe total collected for the SOLD OUT show equals: $" << finalPrice << endl;

return 0;
}



void showArray(const seatTable array, double price)
{
cout << fixed << showpoint << setprecision(2);



for (int section = 0; section < NUM_SECTIONS; section++)
{
double rowTotal = 0.0;
cout << "\n\nSection" << (section+1);
for (int row = 0; row < ROWS_IN_SECTION; row++)
{

cout << "\nRow " << (row+1) << ": ";
for (int seat = 0; seat < SEATS_IN_ROW; seat++)
cout << setw(7) << array[section][row][seat];
rowTotal += array[ROWS_IN_SECTION] [SEATS_IN_ROW];
cout << " Total =" << rowTotal;
}
}
cout << endl;
}

Last edited on Dec 8, 2015 at 3:50pm
Dec 8, 2015 at 3:51pm
@RayDALL

Just a few small errors. Here is your program, with the corrections.
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

const int NUM_SECTIONS = 3,
ROWS_IN_SECTION = 5,
SEATS_IN_ROW = 8;




double seatTable[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW];

// Function prototypes
void fillArray(double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW]);
void showArray(double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW], double price);

int main()
{

	double price = 150.00;
	double f = price,
		s = f - (f * 0.10),
		t = s - (s * 0.10),
		l = t - (t * 0.15);
	double finalPrice;

	// Define 3-D array to hold seat prices
	double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW] = { { { f, f, f, f, f, f, f, f },
	{ f, f, f, f, f, f, f, f },
	{ f, f, f, f, f, f, f, f },
	{ s, s, s, s, s, s, s, s },
	{ s, s, s, s, s, s, s, s } },

	{ { t, t, t, t, t, t, t, t },
	{ t, t, t, t, t, t, t, t },
	{ t, t, t, t, t, t, t, t },
	{ t, t, t, t, t, t, t, t },
	{ t, t, t, t, t, t, t, t } },

	{ { l, l, l, l, l, l, l, l },
	{ l, l, l, l, l, l, l, l },
	{ l, l, l, l, l, l, l, l },
	{ l, l, l, l, l, l, l, l },
	{ l, l, l, l, l, l, l, l } } };

	showArray(seats, price);
	finalPrice = (f * 24) + (s * 16) + (t * 40) + (s * 40);
	cout << "\nThe total collected for the SOLD OUT show equals: $" << finalPrice << endl;

	return 0;
}

void showArray(double seats[NUM_SECTIONS][ROWS_IN_SECTION][SEATS_IN_ROW], double price)
{
	cout << fixed << showpoint << setprecision(2);

	for (int section = 0; section < NUM_SECTIONS; section++)
	{
		double rowTotal = 0.0;
		cout << "\n\nSection" << (section + 1);
		for (int row = 0; row < ROWS_IN_SECTION; row++)
		{

			cout << "\nRow " << (row + 1) << ": ";
			for (int seat = 0; seat < SEATS_IN_ROW; seat++)
			{
				cout << setw(7) << seats[section][row][seat];
				rowTotal += seats[section][row][seat];
			}
			cout << " Total =" << rowTotal;
		}
	}
	cout << endl;
}


And, please, when posting code, use the formatting button to the right, that looks like <>. Highlight your code in the post, and click the <> symbol. This way, it'll show line numbers, and make reading the code a lot easier. And we can use the line numbers to point you to the area of code that needs looking at.
Last edited on Dec 14, 2015 at 5:17pm
Dec 8, 2015 at 5:30pm
Perfect!! It works and compiles. Thank you very much for your help and thank you for letting me know how to properly post in the future. I greatly appreciate it. Since I am new and the semester is coming to an end... do you have any recommendations for books to read during break so that I can continue to learn and prepare for whats ahead?
Dec 8, 2015 at 5:32pm
finalPrice = (f * 24) + (s * 16) + (t * 40) + (s * 40);

Should s appear twice in this line?
Dec 8, 2015 at 5:40pm
thank you for catching that. It was supposed to be 'l'. I have fixed it now. Thank you very much. One thing that I notice is that one wrong button press can change EVERYTHING with your program. Thank you again cire for catching that for me.
Topic archived. No new replies allowed.