HELP! Array Program

Does anyone know how to program an array, where in the actual program you'll need to enter values for each buyer for example and then calculate the values given to them and then the average of all of them.


Here's the sample output

Enter five item price for each buyer
Buyer #1
45 78 22 101
Buyer #2
89 138 789 400
Buyer #3
18 55 46 249
Buyer #4
978 75 67 24
Buyer #5
100 287 101 26


Buyer---------- Item price------------------------------------------ Total Price
Buyer #1----- 45.00----- 78.00----- 22.00----- 101.00----- 246.00
Buyer #2----- 89.00----- 130.00----- 789.00----- 400.00----- 1408.00
Buyer #3----- 18.00----- 55.00----- 46.00----- 249.00----- 360.00
Buyer #4 978.00----- 75.00----- 67.00----- 24.00----- 1144.00
Buyer #5 100.00----- 287.00----- 101.00----- 26.00----- 514.00

Total ----- 3672.00

-------------------------------------------------------------------------------

It's a little bit hard for me to analyze the code for this so maybe a little help would be appreciated thanks!

P.S Using Array



Last edited on
There are 5 buyers, so you need an array of 5 elements, one for each buyer.

Each buyer has 4 item prices. So there's an array for that. The easiest way to handle this is a two-dimensional array. Here is code that enters the info into a 2D array:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    const size_t NumBuyers = 5;
    const size_t NumPrices = 4;
    double prices[NumBuyers][NumPrices];

    for (size_t buyer = 0; buyer < NumBuyers; ++buyer) {
        std::cout << "Buyer #" << buyer+1 << std::endl;
        for (size_t price = 0; price < NumPrices; ++price) {
            std::cin >> prices[buyer][price];
        }
    }
}


Thanks a lot! this was really helpful
these are my codes but its not accurate. its not adding each buyer, can you help me fix this?

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

int main()
{
	double a[5][4], b(0);
	int x,y;
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	cout<<"Enter five item price for each buyer\n";
	for (x=0;x<5;x++){
		cout <<"Buyer #" << x+1 << endl;
			for (y=0;y<4;y++){
				cin >> a[x][y];
			}
	}
	cout << setw(10) <<"Buyer"
		 << setw(30) <<"Item price"
		 << setw(30) <<"Total price"
		 << endl;
	for (x=0;x<5;x++){
		cout << setw(10) << x+1;
		for (y=0;y<4;y++){
			cout << setw(10) << a[x][y];
			b+=a[x][y];
		}
		cout << setw(15) <<b <<endl;
	}
	return 0;
}


anyone? I need it now :( can anyone help me fix this?
Last edited on
titepanatiks, you need to reset b to zero after printing each row. Also, keep in mind that you need to print the grand total at the very end, so you may want another variable to store that.
Topic archived. No new replies allowed.