How to insert avg & min & max in the program

Hi everyone, I'm developing a function, compute() that takes a 2D array with the information shown in
start and end month as parameters, traverses through the array to compute
the following for the given period:
- average sales of each store
- the store with the highest sales
- the store with the lowest sales

I'm stuck at the average section where it couldn't display the answer after i run the program. Anyone can advise me on how to resolve it?

I'm also working on the highest & lowest codes. How do i insert into the source 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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;


int main() {
	cout << "Retail \nStore" << "\t" << " Jan " << "\t" << " Feb " << "\t" << " Mar " << "\t" << " Apr " << "\t" << "May " << "\t" << " Jun \n";
	// 2D array Initialisation [3 rows and 7 columns]
	int table[3][7] = { { 1, 800000, 700000, 750000, 800000, 650000, 700000 },{ 2, 250000, 300000, 350000, 400000, 400000, 420000 },{ 3, 150000, 200000, 180000, 120000, 150000, 200000 } };

	//2D array display
	for (int rows = 0; rows<3; rows++) {
		for (int cols = 0; cols<7; cols++)
			cout << table[rows][cols] << "\t";  //change to cin if you want to read data to 2D array
		cout << endl;
	}
	cin.ignore();
}

float compute(int a, int b, int c)
{
	int ave1 = 0;
	int total1 = 0;
	int ave2 = 0;
	int total2 = 0;
	int ave3 = 0;
	int total3 = 0;
	int x;
	int store1[6] = { 800000, 700000, 750000, 800000, 650000, 700000 };
	int store2[6] = { 250000, 300000, 350000, 400000, 400000, 420000 };
	int store3[6] = { 150000, 200000, 180000, 120000, 150000, 200000 };

	for (x = 0; x < a; x++)
	{
		total1 = total1 + store1[x];
		ave1 = total1 / a;

	}
	for (x = 0; x < b; x++)
	{
		total2 = total2 + store2[x];
		ave2 = total2 / b;
	}
	for (x = 0; x < c; x++)
	{
		total3 = total3 + store3[x];
		ave3 = total3 / c;
	}
	cout << "Average is %d for store 1\nAverage is %d for store 2\nAverage is %d for store 3\n", ave1, ave2, ave3;
	cin.ignore();
	return 0;
}
Last edited on
If you want an output you should call the compute function.
Just as Thomas stated you did not call the function. I also notice the function returns a float but in the program you simply return 0. Did you want to return something else?

Also what is the fun statement for at the end of your function?
Hi Kingkush/ Thomas1965,

How do I call the function? In the beginning head? As for the return, I delete away the 0?
This is your function here:
float compute(int a, int b, int c)

compute is the name of the function and it returns a float data type. It accepts three arguments which are all int data types.

To use your function you would call it like so: compute(firstNum, secondNum, thirdNum);

firstNum, secondNum and thirdNum would be numbers you want your function to manipulate.


However I believe your function is written wrong. I think it should accept an array and it should cycle through the array calculating what is required based off what was written in your original post
Kingkush: Thanks for the advise. Now i got a clearer picture. Yup, what you said is correct. The calculation of the avg is written wrongly. It should extract info from array and display the answers. Noted for the point. Thanks.
Glad i could help! If you face any other issues feel free to post your code and i will help to the best of my abilities.
Kingkush: I try to find the ave sale of each store, however i couldn't get the actual answer. Any suggestion?

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

int main()
{
cout << " Jan " << "\t" << " Feb " << "\t" << " Mar " << "\t" << " Apr " << "\t" << "May " << "\t" << " Jun \n";
// 2D array Initialisation [3 rows and 6 columns]
int table[3][6] = { {800000, 700000, 750000, 800000, 650000, 700000 },{250000, 300000, 350000, 400000, 400000, 420000 },{150000, 200000, 180000, 120000, 150000, 200000 } };

//2D array display
for (int rows = 0; rows < 3; rows++) {
for (int cols = 0; cols < 6; cols++)
cout << table[rows][cols] << "\t"; //change to cin if you want to read data to 2D array
cin.ignore();

int i = 0;
float sum = 0;
for (i = 0; i<6; i++) { //sum of all marks
sum = sum + table[i][0];
}
cout << "avg sale of each store is" << float(sum) / 6 << endl;
}
cin.ignore();
}
This is how to get the sum of each row. From there you should easily get the average.
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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

const int ROW_COUNT = 3;
const int COL_COUNT = 6;

double sum(int data[], int num_cols)
{
  double sum = 0;

  for (int col = 0; col < num_cols; col++)
    sum += data[col];

  return sum;
}

int main()
{
  cout << " Jan " << "\t" << " Feb " << "\t" << " Mar " << "\t" << " Apr " << "\t" << "May " << "\t" << " Jun \n";
  // 2D array Initialisation [3 rows and 6 columns]
  int table[ROW_COUNT][COL_COUNT] =
  {
    { 800000, 700000, 750000, 800000, 650000, 700000 },
    { 250000, 300000, 350000, 400000, 400000, 420000 },
    { 150000, 200000, 180000, 120000, 150000, 200000 }
  };

  for (int rows = 0; rows < ROW_COUNT; rows++) 
  {
    for (int cols = 0; cols < COL_COUNT; cols++)
    {
      cout << table[rows][cols] << "\t"; //change to cin if you want to read data to 2D array
    }
    cout << "\n";
  }
  cout << "\n\nSum of the individual rows\n";
  for (int row = 0; row < ROW_COUNT; row++)
  {
    cout << fixed << "Sum of row [" << row << "] = " << sum(table[row], COL_COUNT) << "\n";
  }
}
Hi thomas,

Thanks for the help. Now, i need to find the minimum and maximum in the table. I tried to code it but there is an error on it. Are you able to help?

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

const int ROW_COUNT = 3;
const int COL_COUNT = 6;

double sum(int data[], int num_cols)
{
double sum = 0;

for (int col = 0; col < num_cols; col++)
sum += data[col];

return sum;
}

int main()
{
cout << " Jan " << "\t" << " Feb " << "\t" << " Mar " << "\t" << " Apr " << "\t" << "May " << "\t" << " Jun \n";
// 2D array Initialisation [3 rows and 6 columns]
int table[ROW_COUNT][COL_COUNT] =
{
{ 800000, 700000, 750000, 800000, 650000, 700000 },
{ 250000, 300000, 350000, 400000, 400000, 420000 },
{ 150000, 200000, 180000, 120000, 150000, 200000 }
};

for (int rows = 0; rows < ROW_COUNT; rows++)
{
for (int cols = 0; cols < COL_COUNT; cols++)
{
cout << table[rows][cols] << "\t"; //change to cin if you want to read data to 2D array
}
cout << "\n";
}
cout << "\n\nSum of the individual rows\n";
cin.ignore();
for (int row = 0; row < ROW_COUNT; row++)
{
cout << fixed << "Sum of row [" << row << "] = " << sum(table[row], COL_COUNT) << "\n";
}
cin.ignore();

int max, min;

min = table[0][0];
max = table[0][0];
for (int rows = 0; rows < ROW_COUNT; rows++)
{
for (int cols = 0; cols < COL_COUNT; cols++)
{
if (min > [rows][cols])
min = [rows][cols];
if (max < [rows][cols])
max = [rows][cols];
cout << endl;
cout << "minimum = " << min << endl;
cout << "maximum = " << max << endl;
}
}
if (min > [rows][cols])You need to reference the table, otherwise how should the compiler know what [rows][cols] are ?
Thomas, thanks for spotting the error. However, when I execute it, It shows me 3 times of the output on Maximum and Minimum.

I only need a single output of the maximum and minimum for the entire count. How should I code it?

example: Maximum is xxxxxx and minimum is xxxxxx
1
2
3
cout << endl;
cout << "minimum = " << min << endl;
cout << "maximum = " << max << endl;

This should be after the for loops.
Please use code tags, when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

As you can see from Thomas' posts, the tags give syntax highlighting and whitespace
(indentation) makes a difference. Furthermore: line numbering. Formatted code is easier
to read and comment.
Topic archived. No new replies allowed.