Incrementing the multidimensional array in a loop.

Hello,
so I tried to complete one challenge on the internet, and I seem to cannot figure out, how to (or if it's even possible) increment specific space in multi-dimensional arrays. I'll paste here a code, so you'll better understand me.

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
#include<iostream>
#include<vector>
#include<numeric>

using namespace std;

int main() {

	long int nums[12][2] = {
	{477571, 726031},
	{423883, 19763},
	{140010, 88061},
	{854401, 981371},
	{885130, 695704},
	{807712, 452323},
	{636109, 721097},
	{125421, 259239},
	{618719, 503819},
	{712765, 852159},
	{321707, 538620},
	{560820, 943477}
	};
	
	int x = nums[0][0];
	int y = nums[0][1];
	int sum = x + y;
	
	for (size_t i{}; i <= 12; i++) {
		cout << sum << ' ' << endl;
		x++;
		y++;
	}

	return 0;
}


And basically I tried to increment the number inside the square brackets by doing x++ and y++. I wanted to make a loop that'll make a sum of the opposite numbers in the array. (477571 + 726031, 423883 + 19763, etc.). I cannot find how tho. Now the loop prints out just the first row and first column of the array 12 times, but I need all rows and all columns (in total 12 sums).
Last edited on
Alright, I found a way around. I'll paste a code here if someone would like see it.

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

using namespace std;

int main() {

	vector<int> nums1 { 477571, 423883, 140010, 854401, 885130, 807712, 636109, 125421, 618719, 712765, 321707, 560820 };
	vector<int> nums2 { 726031, 19763, 88061, 981371, 695704, 452323, 721097, 259239, 503819, 852159, 538620, 943477 };
	
	for (size_t i{}; i < nums1.size(); i++) {
		cout << nums1[i] + nums2[i] << ' ' << endl;
	}

	return 0;
}


However if you'd like, you could tell me about the multi-dimensional arrays (if there's a way to increment it) :)
Mean you doing like this?
1
2
3
4
long int result[12];
for( int i = 0; i < 12; ++i ) result[i] = nums[i][0] + nums[i][1];

for( auto & number : result) std::cout << number << '\n';
Last edited on
Oh wow! That actually did work. Thanks for letting me know! :)
I'll save this in case I'll struggle in the future :D (even tho the vectors were pretty simple too, I just didn't think of them before).
Thanks again!
For a 2-dimensional array of unspecified size, consider:

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
#include <iostream>
#include <iterator>

int main()
{
	const long nums[][2] = {
		{477571, 726031},
		{423883, 19763},
		{140010, 88061},
		{854401, 981371},
		{885130, 695704},
		{807712, 452323},
		{636109, 721097},
		{125421, 259239},
		{618719, 503819},
		{712765, 852159},
		{321707, 538620},
		{560820, 943477}
	};

	long sum[std::size(nums)] {};

	for (size_t i = 0; i < std::size(nums); ++i)
		sum[i] = nums[i][0] + nums[i][1];

	for (const auto& n : sum)
		std::cout << n << "  ";
}

Topic archived. No new replies allowed.