adding array variables columns and rows

closed account (o35DwA7f)
is there a better way(more efficient) of doing 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
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
#include <iostream>

int addition3x3(int array[3][3]);
int additionrows(int array[3][3]);
int additioncolumns(int array[3][3]);

int main()
{
	int array[3][3]{0};
	array[0][0] = 50;
	array[0][1] = 100;
	array[0][2] = 200;
	array[1][0] = 10;
	array[1][1] = 20;
	array[1][2] = 30;
	array[2][0] = 4000;
	array[2][1] = 3000;
	
	/* 50 100 200
	10 20  30
	4000 3000*/


	std::cout << addition3x3(array) << std::endl;
	std::cout << additionrows(array) << std::endl;
	std::cout << additioncolumns(array) << std::endl;
	return 0;
}
int additioncolumns(int array[3][3] = { 0 })
{
	int sum = 0;


	for (int j = 0, i = 0; i < 3; i++)
	{
		sum += array[i][j];
	}

	return sum;
}

int additionrows(int array[3][3] = { 0 })
{
	int sumrows = 0;
	
	
		for (int j=0,i = 0; j < 3; j++)
		{
			sumrows += array[i][j];
		}
	
	return sumrows;
}

int addition3x3(int array[3][3] = { 0 })
{
	int sum3x3 = 0;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			sum3x3 += array[i][j];
		}
	}
	return sum3x3;
}

/*This is a Sudoku kind of thing.
I don’t want you to tell me what is the best way to create a Sudoku , I just want to understand how to manipulate the arrays at my will. This is my understanding so far.
I want to add rows and columns of an uninitialized array.
I can’t add them before initializing them to zero (array[3][3]={0})because it will add the addresses of the uninitialized array, so I am initializing them to zero.
I will initialize each element of the array in main, maybe with cin>>, not important for the moment.
After i set a value on the array I want to call the addition functions.
Do I have to create 3 functions for each row and 3 for each column?
This indicates to me that I am doing something wrong.
Thanks you.*/
Last edited on
Use code tags when posting code:

[code]
your code here
[/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
#include <iostream>

const int Size = 3;

int sumColumn(int block[][Size], int col);
int sumRow(int block[][Size], int row);
int sumBlock(int block[][Size]);

int main()
{
    int block[Size][Size]
    {
        {   50,  100,  200 },
        {   10,   20,   30 },
        { 4000, 3000,    0 }
    };
    using std::cout;

    cout << "sumBlock: " << sumBlock(block) << '\n';
    cout << "Columns:\n";
    for (int col = 0; col < Size; ++col)
        cout << col << ": " << sumColumn(block, col) << '\n';
    cout << "Rows:\n";
    for (int row = 0; row < Size; ++row)
        cout << row << ": " << sumRow(block, row) << '\n';
    return 0;
}

int sumColumn(int block[][Size], int col)
{
    int sum = 0;
    for (int row = 0; row < Size; ++row)
        sum += block[row][col];
    return sum;
}

int sumRow(int block[][Size], int row)
{
    int sum = 0;
    for (int col = 0; col < Size; ++col)
        sum += block[row][col];
    return sum;
}

int sumBlock(int block[][Size])
{
    int sum = 0;
    for (int row = 0; row < Size; ++row)
        for (int col = 0; col < Size; ++col)
            sum += block[row][col];
    return sum;
}

closed account (o35DwA7f)
code tags done.sorry about that.
closed account (o35DwA7f)
Thank you very much for your answer, dutch.
I didn't reply sooner because i still try to understand all the differences
and the implications.
Last edited on
Topic archived. No new replies allowed.