How to assign the max & Min into the array's rows

Pages: 12
Hi everyone,

I got a problem assigning the maximum and minimum into the respective array's rows. I've already found the max & mini value but not sure how to put them into the respective rows.

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
#include <iostream> 
using namespace std;
int main() {
	//hardcode the input values... initialisation of 2d array
	char sentinel = 'X', oper;
	cout << " Jan " << "\t" << " Feb "  "   Mar " "   Apr " "   May " "  Jun \n";
	int table[3][6] = { { 800000, 700000, 750000, 800000, 650000, 700000 },
						{ 250000, 300000, 350000, 400000, 400000, 420000 },
						{ 150000, 200000, 180000, 120000, 150000, 200000 } };

	//print the value
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			cout << table[rows][cols] << " ";
		}
		cout << endl;
	}

	//print average of each row
	int sum[3] = { 0,0,0 };
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			sum[rows] = sum[rows] + table[rows][cols];
		}
	}

	int min, max;
	min = table[0][0];
	max = table[0][0];
	int store_1 = table[0][6];
	int store_2 = table[1][6];
	int store_3 = table[2][6];
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			if (min > table[rows][cols])
				min = table[rows][cols];
			if (max < table[rows][cols])
				max = table[rows][cols];
		}
	}

	for (int rows = 0; rows < 3; rows++) {
		cout << "Sum of each store " << rows << " is " << sum[rows] << endl;
		cout << "Average sale of each store " << rows << " is " << float(sum[rows]) / 6.0 << endl << endl;
	}
	cout << "Lowest sale is = " << min << endl;
	cout << "Highest sale is = " << max << endl;
	system("pause");
Hello Shyanie,

You already have a topic for this program. It is better to stick with that question instead of dividing attention between two or more topics.

Not sure what your intention is with lines 30 - 32, but if you want to store the min and max in the table array you will need to increase the second dimension to hold the new values. Since arrays start the index at zero table[0][6] the highest number for the second dimension can only be 5. The 6 that you are using is outside the boundaries of the array and if the program does not crash when it runs you will be using an unknown value for "store_1". The code you want might look something like this:

1
2
3
4
int table[3][8] ...; // <--- Increase second dimension.

table[0][6] = min;
table[0][7] = max;


With lines 3 and 4 coming after you get the final value for min and max.

Hope that helps,

Andy
Hi Andy,

Thanks for the suggestion, appreciated it. However, i'm confuse on for the int table[3][8] ...; // <--- Increase second dimension. . This should be insert into which line?

and as for
1
2
table[0][6] = min;
table[0][7] = max;

these should be insert into which line?

Lastly, how to display the respective rows for the max and min. Example: "the maximum row is 0 and minimum row is 2".

Sorry for the long teaser question.
Last edited on
Hello Shyanie,

Unless I misunderstood your intent the point of int table[3][8] ...; // <--- Increase second dimension. is to change line 7 to add two extra elements to the array to store "min" and "max". The second bit of code was to show you how to store the information.

After working with our program I changed the for loops to figure "min" and "max". I also put these loops in a function outside of main, but it will still work inside of main if that is what you want. This is the code I came up with to store "min" and "max" in the "table" array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void MinMax(int table[][8], size_t MAXROWS, size_t MAXCOLS)
{
	int min{ 0 };
	int max{ 0 };

	for (size_t rows = 0; rows < MAXROWS; rows++)
	{
		min = 1000000;
		max = 0;

		for (size_t cols = 0; cols < (MAXCOLS - 2); cols++)
		{
			if (table[rows][cols] < min)
				min = table[rows][cols];
			if (table[rows][cols] > max)
				max = table[rows][cols];
		}

		table[rows][6] = min;
		table[rows][7] = max;
	}
}


And the function is: MinMax(table, MAXROWS, MAXCOLS);. And at the beginning of main I defined this:
1
2
3
constexpr size_t MAXROWS{ 3 };
constexpr size_t MAXCOLS{ 8 };
constexpr size_t MAXMONTHS{ 12 };


This allows the size of an array to be changed in one place without searching through the entire code.

Putting the code for "min" and "max" in a function is not necessary, but it is something I like to do because of what it does.

Hope that helps,

Andy
Hello Shyanie

Sorry I was interrupted by lunch and lost my thoughts missing your last question.

This may not be exactly what you want your display to look like, but it is something to stat with. Again I put this in a function, but could be easily used in main:

1
2
3
4
5
6
7
8
9
10
void PrintStoreMinMax(int table[][8])
{
	int col{ 6 };

	for (size_t row = 0; row < 3; row++)
	{
		std::cout << "\n Minium sales for store " << (row + 1) << " is: " << table[row][col] << std::endl;
		std::cout << " Maxium sales for store " << (row + 1) << " is: " << table[row][col + 1] << std::endl;
	}
}


The cout statement can be changed to whatever you want to say.

Hope that helps,

Andy
Hi Andy,

I manage to code out the first portion of the program. However there is some problem on the line 64. Why is the && is on red line. The comment mentioned that expected an expression. What is it about?

In line 88 and 89, there is an error message which i not about it. Here is my source code. Hope you can advise how should i resolve it.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
[code]#include <iostream> 
using namespace std;

[code]int main() {
	//hardcode the input values... initialisation of 2d array
	char sentinel = 'X', oper;
	cout << " Jan " << "\t" << " Feb "  "   Mar " "   Apr " "   May " "  Jun \n";
	int table[3][6] = { { 800000, 700000, 750000, 800000, 650000, 700000 },
						{ 250000, 300000, 350000, 400000, 400000, 420000 },
						{ 150000, 200000, 180000, 120000, 150000, 200000 } };

	//print the value
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			cout << table[rows][cols] << " ";
		}
		cout << endl;
	}

	//print average of each row
	int sum[3] = { 0,0,0 };
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			sum[rows] = sum[rows] + table[rows][cols];
		}
	}

	int min, max;
	min = sum[0];
	max = sum[0];
	int most_sale = 0;
	int least_sale = 0;
	for (int rows = 0; rows < 3; rows++) {

		if (min >= sum[rows]) {
			least_sale = rows + 1;
			min = sum[rows];
		}
		if (max <= sum[rows]) {
			most_sale = rows + 1;
			max = sum[rows];
		}
	}
	for (int rows = 0; rows < 3; rows++) {
		cout << "Sum of each store " << rows + 1 << " is " << sum[rows] << endl;
		cout << "Average sale of each store " << rows << " is " << float(sum[rows]) / 6.0 << endl << endl;
	}
	cout << "Lowest sale is store " << least_sale << endl;
	cout << "Highest sale is store " << most_sale << endl;
	system("pause");

	do {
		int StartM = 0;
		int EndM = 0;
		int table[3][6] = { { 800000, 700000, 750000, 800000, 650000, 700000 },
		{ 250000, 300000, 350000, 400000, 400000, 420000 },
		{ 150000, 200000, 180000, 120000, 150000, 200000 } };

		cout << "Enter start of the month for calculation" << endl;
		cin >> StartM;
		cout << "Enter end of the month for calucation" << endl;
		cin >> EndM;

		if ((StartM > 0) && StartM <= 6) && (EndM > 0 && EndM <= 6) && (EndM >= StartM))
		{
		// Store 1 value
		for (int StartM = 0; StartM < EndM; StartM++)
		{
			sum[StartM] = sum[StartM] + table[0][StartM];
		}
		// Store 2 value
		for (int StartM = 0; StartM < EndM; StartM++)
		{
			sum[StartM] = sum[StartM] + table[1][StartM];
		}
		//Store 3 value
		for (int StartM = 0; StartM < EndM; StartM++)
		{
			sum[StartM] = 0; StartM + table[2][StartM];
		}

		for (int cols = 0; cols < 6; cols++) {
			cout << "The total sale is: " << sum[cols] << endl;
			cout << "Press E to exit or any other key to continue" << endl;
		}
		};
		else (
			cout << "Please enter Start & End month" << endl;
		);
	} while (toupper(sentinel) != 'E');
}
	
[/code][/code]
Hello Shyanie,

On line 64 you re missing an opening "(" at the beginning. As the "()" pair up you are missing most of the if condition. It should look like this:

if (((StartM > 0) && StartM <= 6) && (EndM > 0 && EndM <= 6) && (EndM >= StartM))

Notice the three "(" at the beginning. Now I have not teste this yet, but it should solve the problem.

At first look I believe that lines 86 and 89 have a semi-colon that should not be there.

I will load this code bit later and see if there is anything else I can find.

Hope that helps,

Andy

P.S. Watch the code tags.
Last edited on
Hi Andy,

Thanks for the feedback. I manage to resolve the problem for the if (((StartM > 0) && StartM <= 6) && (EndM > 0 && EndM <= 6) && (EndM >= StartM)) as advised by you. However, the
1
2
3
4
else if (
			cout << StartM << "Please enter Start & End month" << endl;
		)
	} while (toupper(sentinel) != 'E');


is giving me problem. The red line appears on the "endl" and "end braces". How do i get raid of it?

rgs
Shyanie
Hello Shyanie,

At the beginning of main you define "table" and initialize it which is fine. Then you redefine it inside the do/while loop. This should be an redefinition error except that it is redeined inside a different scope, so it works. Inside the {} braces of the scope of main anything used in min is visible to any other scope that is defined.

The variable "oper" should be initialized when it is defined. As with all variables in a program they should be initialized when they are defined to avoid any problems. Sometimes my VS will complain when trying to use an uninitialized variable. There are few exceptions to this like a vector does not have to be initialized when defined because there is nothing to initialize until the vector is used.

Everything looks good down to line 28. You could easily write this: int min = sum[0], max = sum[0];. Although what you have works.

Then your for loop falls apart. You assign "min" and "max" equal to the value of "sum[rows]". The is not a minimum or maximum just a total. Then each iteration of the for loop would over write each lhs variable which is not what you want. By the time the for loop ends "min" and "max" will hold the total value of store 3 only.

The "min/max" for loop needs to be redone to work properly. Two thoughts here is to creaate arrays for "min" and "max", but I keep coming back to using a 2D arrayfor "sum" to hold the values of "total sales", "min" and "max". This would store the values for each row / store.

Lines 48 and 49 do not print the correct values that you would want.

The do/while loop is good idea, but does not work.

int table does not need to be defined again here. The first definition at the beginning of main is still viable to the do/while loop. The if statement is missing an opening "(" at the very beginning. That is why you are getting an error. The ")"s do not match up properly.

Defining "StartM" and "EndM" an int will work except the "StartM" Will likely be one larger than you want or the start position os the array. And "EndM" could end up being outside the boundary of the array. So unless you tell the user to use zero for "Jan" and five for "Jun" it will not work the way you want.

In the for loops "sum[StartM]", "StartM" never changes, so everything on the rhs of = always goes into the same array element.

In Line 79 I removed the "0;" to fix an error.

The last for loop tells the user std::cout << "Press E to exit or any other key to continue" << std::endl;, but you have no way of giving "sentinel" a new value, so the while condition will always be true.

The else part of the if statement is surrounded be "()"when it needs to be surrounded by "{}" braces.

Those are the problems I have found for now. I will have to work on the solutions next.

Hope that helps,

Andy
Last edited on
Hi andy,

I think it would be better if i post the question of my assignment here for you to analyze.

(a) Develop a function, compute() that takes a 2D array with the information shown in

Retail
Store......Jan ......Feb .......Mar.......Apr........May.......Jun
1.......800,000 700,000 750,000 800,000 650,000 700,000
2 ......250,000 300,000 350,000 400,000 400,000 420,000
3.......150,000 200,000 180,000 120,000 150,000 200,000

Table 3.1, 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

(b) Demonstrate the ability to test the function written in Part (a) by writing a test driver.

The driver prompts the user to enter the start and end month and invokes the compute
function and displayed the respective sales. The driver program repeats with a while
loop and terminates when the program encounters a sentinel value, ‘E’.

Last edited on
This is the source code for part (a)

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

int main() {
	//hardcode the input values... initialisation of 2d array
	char sentinel = 'X', oper;
	cout << " Jan " << "\t" << " Feb "  "   Mar " "   Apr " "   May " "  Jun \n";
	int table[3][6] = { { 800000, 700000, 750000, 800000, 650000, 700000 },
				{ 250000, 300000, 950000, 400000, 400000, 420000 },
				{ 150000, 200000, 180000, 120000, 150000, 200000 } };

	//print the value
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			cout << table[rows][cols] << " ";
		}
		cout << endl;
	}

	//print average of each row
	int sum[3] = { 0,0,0 };
	for (int rows = 0; rows < 3; rows++) {
		for (int cols = 0; cols < 6; cols++) {
			sum[rows] = sum[rows] + table[rows][cols];
		}
	}

	int min, max;
	min = sum[0];
	max = sum[0];
	int most_sale = 0;
	int least_sale = 0;

	for (int rows = 0; rows < 3; rows++) {

		if (min >= sum[rows]) {
			least_sale = rows + 1;
			min = sum[rows];
		}
		if (max <= sum[rows]) {
			most_sale = rows + 1;
			max = sum[rows];
		}
	}
	for (int rows = 0; rows < 3; rows++) {
		cout << "Sum of each store " << rows + 1 << " is " << sum[rows] << endl;
		cout << "Average sale of each store " << rows << " is " << float(sum[rows]) / 6.0 << endl << endl;
	}
	cout << "Lowest sale is store " << least_sale << endl;
	cout << "Highest sale is store " << most_sale << endl;
	system("pause");
}


I believe this should be the correct source code. Correct me if i'm wrong.
Last edited on
As for part (b). These are my codes. I guess there are some error here and there. When i exclude the program.. The widows just keep running. I just don't understand where went wrong.

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
do {
		int StartM = 0;
		int EndM = 0;
		int table[3][6] = { { 800000, 700000, 750000, 800000, 650000, 700000 },
		{ 250000, 300000, 350000, 400000, 400000, 420000 },
		{ 150000, 200000, 180000, 120000, 150000, 200000 } };

		cout << "Enter start of the month for calculation" << endl;
		cin >> StartM;
		cout << "Enter end of the month for calucation" << endl;
		cin >> EndM;

		if (((StartM > 0) && StartM <= 6) && (EndM > 0 && EndM <= 6) && (EndM >= StartM))
		{
			// Store 1 value
			for (int StartM = 0; StartM < EndM; StartM++)
			{
				sum[StartM] = sum[StartM] + table[0][StartM];
			}
			// Store 2 value
			for (int StartM = 0; StartM < EndM; StartM++)
			{
				sum[StartM] = sum[StartM] + table[1][StartM];
			}
			//Store 3 value
			for (int StartM = 0; StartM < EndM; StartM++)
			{
				sum[StartM] = 0; StartM + table[2][StartM];
			}

			for (int cols = 0; cols < 6; cols++) {
				cout << "The total sale is: " << sum[cols] << endl;
				cout << "Press E to exit or any other key to continue" << endl;
			}

		else cout << StartM << "Please enter Start & End month" << endl;
		} 
	} while (toupper(sentinel) != 'E');
Develop a function, compute() that takes a 2D array with the information shown in

The starting point then would be to decide what other parameters (if any) the function compute() might need, and whether it should return a value of some type.

So far the code posted does not use any functions other than main().

Demonstrate the ability to test the function written in Part (a) by writing a test driver.

This part of the code can properly belong in main(). The other processing should be in compute().

Are you aware of how to use functions?
Develop a function, compute() that takes a 2D array with the information shown in

The starting point then would be to decide what other parameters (if any) the function compute() might need, and whether it should return a value of some type.

So far the code posted does not use any functions other than main().

Me: Are you saying that in the part (a) , i should use compute () instead of main ()?


Demonstrate the ability to test the function written in Part (a) by writing a test driver.

Me: For part (b), i can use main()?


Are you aware of how to use functions?


U mean function declaration, definition, call?
Last edited on
Hello Shyanie,

As your instructions point say, and as Chervil pointed out, you need a compute function. And other functions would be useful.

As I said earlier, and for what you call part a, works down to line 28. Then the for loop does not figure the "min" and "max" correctly.

In part b within the if statement the for loops do not work correctly. They should be more like this:

1
2
3
4
5
6
7
sum[0][0] = sum[1][0] = sum[2][0] = 0;
			
// Store 1 value
for (int start = StartM; start < EndM; start++)
{
	sum[0][0] += table[0][start];
}


Compare this to your code and you can see that the use of "StartM" was used improperly.

I would start with working on part "a" first to get the output to look like your example. Then I would work on the "compute" function and consider putting other pieces of code into separate functions.

With the new information I will need some time to rethink your program.

Hope that helps,

Andy
Shyanie wrote:
Me: Are you saying that in the part (a) , i should use compute () instead of main ()?

Yes, that's how I read the question.


Me: For part (b), i can use main()?

Yes.

U mean function declaration, definition, call?

Yes.


My understanding, from reading the question, it seems the output of the program might look something like this:
Period from Feb to May
Average sales =
1  725000
2  362500
3  162500
Store with highest sales: 1
Store with lowest sales:  3

Of course the layout and formatting and exact text is up to you.

But it doesn't seem entirely clear whether compute() or main() should actually print the output. It would certainly involve fewer function parameters if compute() also prints.
Hello Shyanie,

With the new information about the program you might consider redoing the whole program if you have the time.

I would be glad to show you how to redo the program better. Otherwise I will see what I can do to make what you have work.

Andy
Hi Chervil, Andy

I see where both of you coming from. However, I don't have much time to redo the program now as I need to submit the assignment by tonight. You able to help to come out the source code? Appreciate if able to help. Thank you so much.
It seems time is running short - that is unfortunate.

As I see it the function declaration might look like this:
 
void compute(int sales[][COLS], int start, int finish, double average[], int& highstore, int& lowstore);


That's if it is to do the computation only, and pass the results back so main() can print them. On the other hand if compute() will also print the results (which is probably better in this case) then the declaration might look like this:
 
void compute(int sales[][COLS], int start, int finish);


Of course you don't have to use those exact variable names, but that sort of thing.
By the way, in my code I used these constants at the start:
1
2
const int ROWS = 3;
const int COLS = 6; 

It is useful to use those for defining the size of arrays and setting loop conditions rather than having lots of 'magic numbers' scattered throughout the code.

Hello Shyanie,

When you write the "compute function consider this:
void Compute(int table[][6], int sum[][3], size_t start, size_t end)

I used 2D array for "sum", although a better name would help like "sumMinMax", to store the total sales, min and max for each row. "size_t" is another name for unsigned int and start and end are variables to determine where the columns will start and end. This way the function can be used where-ever it is needed.

In part B the variables "StartM" and "EndM" would be better defined above the do/while. And the definition for "table" is nor needed here. My personal preference is to define all variables at the beginning of main so I know where they are.

I noticed a couple of problems in the if statement:
if (((StartM > 0) && StartM <= 6) && (EndM > 0 && EndM <= 6) && (EndM >= StartM))

(StartM > 0) First the () are not really needed here and > 0 will not work because in "table" the figures for Jan are in element 0 which you would never get to. Then with StartM <= 6 if "StartM" is equal to six this will be outside the boundaries of the array and have unpredictable results when the program runs. The same hold true for "EndM". Just making a point here in case you keep the if/else.

Now inside the if statement the three for loops are not needed. This can all be done with one call to Compute(table, sum, StartM, EndM);. Then I would follow this with a call to a MinMax() function to calculate on the narrowed portion of the "table".

I keep noticing that the for loop to figure min and max is wrong and does not work the way you want. This is my idea for a "MinMax" function

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
void MinMax(int table[][6], int sum[][3], size_t start, size_t end, int MAXROWS)
{
	int min{ 0 };
	int max{ 0 };

	for (int rows = 0; rows < MAXROWS; rows++)
	{
		min = 1000000;
		max = 0;

		for (int cols = start; cols < end; cols++)
		{
			if (table[rows][cols] <= min)
			{
				//least_sale = rows + 1;
				sum[rows][1] = table[rows][cols];
				min = table[rows][cols];
			}
			if (table[rows][cols] >= max)
			{
				//most_sale = rows + 1;
				sum[rows][2] = table[rows][cols];
				max = table[rows][cols];
			}
		}

	}
}


This is why I made sum a 2D array and why the function parameters are what they are. Also notice I removed "least_sale" and "most_sale" from this function because it did not work here. What I did was to create a separate function using the "sum" array to figure out which store had the least sales and most sales. With the function definition being:
void LeastMostSales(int table[][6], int sum[][3], size_t start, size_t end, int& least_sales, int& most_sales, size_t MAXROWS).

The code I used is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void LeastMostSales(int table[][6], int sum[][3], size_t start, size_t end, int& least_sales, int& most_sales, size_t MAXROWS)
{
	int min{1000000};
	int max{ 0 };

	for (size_t row = 0; row < MAXROWS; row++)
	{
		if (sum[row][1] < min)
		{
			least_sales = row + 1;
			min = sum[row][1];
		}

		if (sum[row][2] > max)
		{
			most_sales = row + 1;
			max = sum[row][2];
		}
	}

}


Dealing with the "sum" array the way I did it is easier to show you.

In the do/while loop after the last for loop I added this code to make it work:

1
2
std::cout << "\n Press E to exit or any other key to continue ";
std::cin >> sentinel;


I need to do more checking on the else statement as I think it might be redundant.

Hope that helps,

Andy
Pages: 12