working with arrays backwards

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
//lesson 6 exercise 2
#include <iostream>
using namespace std;

int main()
{
	

	int MyInts1[3] = {1, 2, 3};
	int MyInts2[2] = {4, 5};

	cout << "Going to add elements of arrays in reverse order" << endl;

	for(int index = 3; index > 0; index--)
	{
		for(int index2 = 2; index2 > 0; index2--)
		{
			cout << MyInts1[index]<< " + " << MyInts2[index2]
			 << " = " << MyInts1[index] + MyInts2[index2] << endl;
		}
	}






system("pause");
return 0;
}




ok guys once again this is not homework in anyway this is life enrichment for the hurt fat guy. Basically what I am trying to do is get each element of the 1st array to add itself to each element of the 2nd array. I have achieved this but I am getting more output than I need. This is what I am getting and it is driving me nuts I have been working on it all day even going back and examining the fundamentals of for loops with my father...although his expertise is basic.

3+1=4
3+5=8
3+4=7
3+1=4
3+5=8
3+4=7
2+1=3
2+5=7
2+4=6
1+1=2
1+5=6
1+4=5

that is the output I am getting in codeblocks. It is starting correctly with the last element of 3 then going to 2 and finally 1, but the 3+1 shouldn't be there, the 2nd set of the 3's shouldn't be there and the 2+1 and 1+1 shouldn't be there. Any input good bad or nasty is appreciated.

Dan
Valid indices for MyInts1 are 0-2. You're using 1-3.

Valid indices for MyInts2 are 0-1. You're using 1-2.
Last edited on
@pcworx

You're out of range in index and index2. Remember, arrays start at 0, so MyInts[0] = 1, MyInts[1] = 2 and MyInts[2] = 3, yet you are trying to access MyInts[3] with index = 3. Pretty much the same problem with MyInts2[2] and index2 = 2. MyInts2[0] = 4 and MyInts2[1] = 5. There is no MyInts[2].
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
//lesson 6 exercise 2
#include <iostream>
using namespace std;

int main()
{


	int MyInts1[2];
	MyInts1[0] = 1;
	MyInts1[1] = 2;
	MyInts1[2] = 3;

    int MyInts2[1];
    MyInts2[0] = 4;
    MyInts2[1] = 5;

	cout << "Going to add elements of arrays in reverse order" << endl;

	for(int index = 2; index >= 0; --index)
	{
		for(int index2 = 1; index2 >= 0; --index2)

			cout << MyInts1[index]<< " + " << MyInts2[index2]
			 << " = " << MyInts1[index] + MyInts2[index2] << endl;

	}

return 0;
}




cleaned up the arrays like they should be now I need to work on the loops a little bit...thanks for the responses also fellas...I have been down 2 days and still not right...I took a header into a treadmill and had to have my lip sewed back together with a few stitches but I will try to figure out the loops here today or tomorrow and may need some help but thanks again.
@pcworx

You still have to increase your array indices. Remember, an array that is created, as you did with, int MyInts1[2];, has two place holders only, MyInts1[0] and MyInts1[1]. There is no MyInts1[2]. Increase it to int MyInts1[3]; and int MyInts2[2]; to accommodate MyInts2[0] and MyInts2[1].
wow I must be off my rocker completely lol let me go back and work with it somemore. thanks again.
Topic archived. No new replies allowed.