Problem with 'for' loop in a function

closed account (N1Co216C)
Hi, for some reason this program won't compile. There appears to be something wrong with my 'for' loop in the void revCopyArray function. Could someone please tell me what I'm doing wrong?


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
 /*
Suppose you have declared two arrays, A and B in a program. 
Array A already has values in it, and the number of values is stored in N. 
Write a function to copy the N values in A to B. Write the calling statement also.
Store the values from A to B in reverse order.*/


#include <iostream>
#include <iomanip>

using namespace std;

void revCopyArray (int [], int [], int);

int main()
{
int A [10] = { 14, 35, 8, 10, 99, -1, 654, 12, 0, 33 };
int B [10];

revCopyArray (B, A, 10);

for ( int sub = 0; sub< 10; sub++)
{
cout << B [sub] <<endl;
}

return 0;
}

void revCopyArray (int copyIntoArray[], int copyFromArray[], int numValues )
{
for (int sub = 0; sub2 = numValues -1; sub < numValues; sub++, sub2--)
	{
	copyIntoArray[sub] = copyFromArray[sub2];
	}
}
Count your semicolons on line 32.
closed account (N1Co216C)
This is probably a stupid question, but does having more than 2 semicolons in the for statement cause an error?
There are only 2 semicolons possible.

The compiler expects the code between '(' and the first ';' to be things that are done only once, and done before entering the loop. The code between the first ';' and second ';' are the conditions that are checked every time before running the code in the loop. The code after the second ';' and before the ')' is executed after every time the code in the loop is run.
closed account (N1Co216C)
Got it. Thanks for your help!
Topic archived. No new replies allowed.