How to compute sum for user input

Hi everyone, I need your help by how do I construct the code for prompts the user to enter the start to end of the months and then invokes the compute function and displayed the total value. The program repeats with a while loop and terminates when the program encounters a sentinel value, ā€˜Eā€™.

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

int main() {
	//hardcode the input values... initialisation of 2d array

	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];
		}
	}

	void findMinMax(const int table[][6]);
	int min, max;
		min = table[0][0];
		max = table[0][0];
		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 Row " << rows << " is " << sum[rows] << endl;
		cout << "Avg of Row " << rows << " is " << float(sum[rows]) / 6.0 << endl << endl;
	}
	cout << "minimum = " << min << endl;
	cout << "maximum = " << max << endl;
	system("pause");
	return 0;

}
Last edited on
how do I construct the code for prompts the user to enter the start to end of the months
Could you please clarify. The program has an array of 18 values. Do you want the user to enter a fresh set of 18 values, or something else?
Hi Chervil,

Its count by month. Example;

The user can input Jan to Feb and the calculated total value will be from Jan to Feb.

The user can input Mar to Jun and the calculated total value will be from Mar to Jun.

Hope this clarify.
At the moment, you have a lot of code which has the loop boundaries hard-coded, for example,
 
    for (int cols = 0; cols < 6; cols++)


If you want it to be more flexible, then you'd probably be better using variables such as
1
2
3
    int from = 1;
    int to = 6;
    for (int cols = from - 1; cols < to; cols++)


The it would be possible to ask the user to enter the month numbers in the range January = 1 to June = 6. You'd need to validate the input.

It might also be a good idea to put some or maybe most of the code into separate functions, which you can then call, and pass the required start and end numbers as the function parameters.
Hello Shyanie,

Some things I have noticed looking through your program:

1.
1
2
#include <ctime> 
#include <cstdlib>  

These header file are not needed. "cstdlib" or "stdlib.h" is included at some point through "iostream" which makes "cstdlib" redundant.

2. I will make the noet that using namespace std; does make for less typing, but it WILL get you in trouble in the future. You can do a search here to find many posts discussing this subject. This is also good reading http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
Load this little program and see what you get for an error message:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int count{ 0 };

int main()
{
	count++;

        return 0;
}



3. and then invokes the compute function A good thought, but your program has no functions. There are several blocks of code that would make good functions like:
1
2
3
4
5
6
double GetSum(int table[][6], double sum[], const int start, const int end)
{
	for (int rows = 0; rows < 3; rows++)
		for (int cols = start; cols < end; cols++)
			sum[rows] = sum[rows] + table[rows][cols];
}


My idea of turning the for loops in main into a function. Notice the lack of {} braces in the code. This is because of each for loop only having one line. The indentation helps to know which line goes with what. You could add two extra variables for use in the first for loop something like this:
 
for (int rows = variableForStart; rows < variableForEnd; rows++)


The idea is that the above function could be used at least two different ways. Another option is to use default numbers in the prototype that would print all rows.

how do I construct the code for prompts the user to enter the start to end of the months

After line 46 Add your code for user input. Something like this for an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::cout << "\n Enter start Month (Jan ...): ";
std::getline(std::cin, startMonth);
// or
std::cin >> startMonth;  // <--- startMonth defined as std::string.

for (size_t lc = 0; lc < MAXCOLS; lc++)
{
	if (startMonth == months[lc])  // <--- months defined as an array of std::strings.
	{
		monthStart = lc;
                break;
	}
}


The same concept can be used for the end month and other than diferent variable names the biggest difference would be monthEnd = lc + 1. The "+ 1" is for use in a or loop so you can use the condition "lc < monthEnd" otherwise you would have to use "lc <= monthEnd" or "lc < monthEnd + 1". Personally I prefer to use the first example.

Just a helpful hint. Line 10 cooudl be written this way:
1
2
3
4
5
6
int table[3][6] =
	{
		{ 800000, 700000, 750000, 800000, 650000, 700000 },
		{ 250000, 300000, 350000, 400000, 400000, 420000 },
		{ 150000, 200000, 180000, 120000, 150000, 200000 }
	};

It is not necessary, but it does give a better representation of the 2D array in more of a row and column format. Also it makes it easier to fine errors or to add to the array.

Work on this and let me kow what you come up with.

Hope that helps,

Andy
Hi Andy, Chervil,

What about the output function?

1
2
[for (int cols = from; cols <= to; cols++) {
cout << " The total sale " << cols << " is " << sum[cols] << endl;


Something like this? I tried but couldn't work.
Last edited on
Hello Shyanie

1
2
[for (int cols = from; cols <= to; cols++) {
cout << " The total sale " << cols << " is " << sum[cols] << endl;


This bit of code is out of context. With out the rest of the code or function it is hard to tell what the problem is. Maybe it is a typo, but the opening [ is missing a closing ] and neither is necessary here and the opening { has no closing }. If there is only one line the {} are not needed. And the second line should be indented for better readability. It should look like this:
1
2
for (int cols = from; cols <= to; cols++)
        cout << " The total sale " << cols << " is " << sum[cols] << endl;

Unless there is more than one line to process inside the for block.

You are using new variable names, so post the whole code please so I can see what you are doing. Otherwise I do not see anything wrong with what you have. I just can not see how it is being used.

Andy
Topic archived. No new replies allowed.