Copy arrayA into arrayB in reverse order

I've tried and tried some more before I ask the wizards of this forum. I have successfully populated arrayA from my file now I need help to copy that arrayA into arrayB in reverse... Help


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

using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int num = 0;
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	
		
	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i = 0 ; i < num; i++)
		arrayB[num - i] = arrayA[i];
	cout << arrayB << "\t";
	cout << endl;
}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
line 20: num is initialized to 0. It is never set to the number of elements read from the file.

Line 34: Your loop won't execute because num was passed as 0.

Line 35: You're indexing out of bounds (assuming num was correct). Let's assume num is 10. First iteration through the loop will attempt to store into arrayB[10-0]. The elements in your array are 0 - 9. arrayB[10] is out of bounds.

closed account (2AoiNwbp)
num = 0; (line 15)
1
2
3
4
5
6
7
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i = 0 ; i < num; i++)           // <--- false
		arrayB[num - i] = arrayA[i];
	cout << arrayB << "\t";
	cout << endl;
}

Last edited on
When I debug my print works fine on arrayA it goes:
10 15 27 89 90 95 27 27 13 99 33
Now arrayB gives me:
0024FD28
???



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

using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int num=10;
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	
		
	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i=0;i < num; i++)
		arrayB[num-i] = arrayA[i];
	cout << arrayB << "\t";
	cout << endl;
}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
I don't get your point. What do you mean by:
 
// <--- false 

If you look up, he says

num = 0; (line 15)


If he's correct, I have not checked, "i" will never be less than num as I goes up in value and num stays the same.

for (int i = 0 ; i < num; i++)
Last edited on
closed account (2AoiNwbp)
I don't get your point. What do you mean by:

// <--- false

i < 0 is false
Now arrayB gives me:
0024FD28
???

arrayB is never initialized
@ SamuelAdams - Yes, I saw that. So?

bozz51 wrote:
Now arrayB gives me: 0024FD28

That's because you're attempting to print a pointer at line 36. arrayB is the address of the array. That's not going to print the entire array.

If you want to print the entire array, call your Print routine.
1
2
 
  Print (arrayB);



closed account (2AoiNwbp)
arrayB is never initialized

Here, I was pointing to your first code.
In the second code you posted, AbstractionAnon is right.

regards,
Alejandro
Certainly closer something is still off. Thanks so much for the help

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

using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int num);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int num=10;
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	Print(arrayB);
	
		
	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int num)
{
	for (int i=0;i < num; i++)
		arrayB[num-i] = arrayA[i];

}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
I get -858993460 then it prints the array in reverse??
The following comment from above still applies:

Line 35: You're indexing out of bounds. Let's assume num is 10. First iteration through the loop will attempt to store into arrayB[10-0]. The elements in your array are 0 - 9. arrayB[10] is out of bounds.


Thank you!!!


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

using namespace std;

void ReadFromFile(int arrayF[]);
void Print(int arrayA[]);
void Copy(int arrayA[], int arrayB[],int& num);

int main()
{
	int arrayA[10];
	int arrayB[10];
	int num=9;
	
	ReadFromFile(arrayA);
	Print(arrayA);
	Copy(arrayA, arrayB, num);
	Print(arrayB);
	
		
	system("pause");
}
void ReadFromFile(int arrayA[])
{
	ifstream fin;
	fin.open("data.txt");
	for (int i = 0; i < 10; i++)	
		fin >> arrayA[i];			
}
void Copy(int arrayA[], int arrayB[],int& num)
{
	for (int i=0;i <= num; i++)
		arrayB[num-i] = arrayA[i];

}

void Print(int arrayA[])
{
	for (int i = 0; i < 10; i++)	
		cout << arrayA[i]<<"\t";
		cout << endl;	
}
You haven't fixed the problem at line 35. In fact, you've made it worse.

Think about what the subscript to the last element of arrayB should be.
It works but I'm trying to learn you lost me. I have been doing c++ for 3 weeks.
For the first iteration of the loop, you want to put the first element of A into the last element of B. What should the subscript to B be? Hint: elements of B are 0 - 9. First time through the loop, num is 10 and i is 0. 10-0=10.
You don;t want to reference B[10]. That is out of bounds.

You changed the the termination condition of your loop to <=num. That's going to cause problems too. Last iteration through the loop, you're going to reference A[10] which is also out of bounds.

Consider the following:
1
2
3
    for (int i=0; i < num; i++)
        arrayB[num-i-1] = arrayA[i];  // 1st time 10-0-1= B[9]
                                                      // last time 10-9-1 = B[0] 



Thank you... I understand.
Topic archived. No new replies allowed.