Need Help. Exchange Values in an Array

Jul 7, 2014 at 1:41am
I am having trouble with . . . The assignment: Write a program to declare and initialize the following array: int array[] = { 10, 20, 30, 40, 50, 60 };

Now add code to the program that uses a loop to exchange the outermost pair of element values, then the next interior pair, and so on until the innermost pair of values is exchanged. Write the program so that it is not sensitive to the number of elements in the array and test it on an integer array that contains an odd number of elements.

Here is my code so far:

#include <iostream>

using namespace std;

int main ()
{
int array[] = { 10, 20, 30, 40, 50, 60};
int x = sizeof(array) / sizeof (array[0]);

int y;
int i;

for (i = 0; i < x/2; ++i)

int tmp = array[x];
array[x] = array[y];
array[y] = int tmp;

return 0;
}


I keep getting an error message on "array[y] = int tmp;" that says
""error: expected primary-expression before 'int'|

Can someone tell me what I am doing wrong? Thank you!
Last edited on Jul 7, 2014 at 2:54pm
Jul 7, 2014 at 9:22am
bump
Jul 7, 2014 at 9:35am
Please, use the code tags.

What lines do you think to be in your loop?

The error:
1
2
3
int a = 7;
int b = 42;
a = int b; // this is your error line. What should it do? 
Jul 7, 2014 at 10:50am
I'm not understanding what you mean.
Jul 7, 2014 at 11:02am
It is you, who wrote: array[y] = int tmp;
What do you mean with that?

I can guess, but the compiler does not understand you. It does hint about the int. There is a syntax error.


Or did you refer to the other thing? What is the difference between this:
1
2
3
for (i = 0; i < x/2; ++i)
int tmp = array[x];
std::cout << "Hello\n";

and this:
1
2
3
4
5
for (i = 0; i < x/2; ++i)
{
  int tmp = array[x];
}
std::cout << "Hello\n";
Jul 7, 2014 at 2:37pm
My teacher is the one who suggested: array[y] = int temp. I am lost as to what he means. I have asked him, but he keeps explaining in the same way. I have added braces, but i am still getting the same error massage. I don't know if I need to change int tmp to something else in that line. I'm been doing ok in the class until this assignment:

#include <iostream>

using namespace std;

int main ()
{
int array[] = { 10, 20, 30, 40, 50, 60};
int x = sizeof(array) / sizeof (array[0]);

int y;
int i;

for (i = 0; i < x/2; ++i)
{
int tmp = array[x];
}

array[x] = array[y];
array[y] = int tmp;

return 0;
}
Jul 7, 2014 at 3:20pm
Ok, lets take the syntax error:
1
2
3
4
int C = A; // declaration of an integer variable with name 'C'
// The variable C is initialized with the value of variable A
A = B; // The variable A has now same value as variable B
B = int C; // syntax error 

What does the int do there? Nothing sensible.

C is already a variable that has a value. The int C looks like a declaration of a new variable. However, that would:
1. Mask the existing C that holds a copy of A.
2. Is a syntax error.

How to do it more correctly:
1
2
3
int C = A; // store value of A
A = B; // move B to A
B = C; // restore original value of A to B 

I bet that someone did not pay attention to detail.


What was the difference between with and without braces? Nothing. They are identical. In both cases the loop executes only one statement, the int tmp = array[x];
Only after the loop has ended, the std::cout << "Hello\n"; (and all the other following statements will execute.

If you want to execute more than one statement on each iteration, then you have to put all those statements within the block. Like this:
1
2
3
4
5
6
for (i = 0; i < x/2; ++i)
{
  int tmp = array[x];
  array[x] = array[y];
  array[y] = tmp;
}

Do you feel lucky now?
Don't.

Lets look your code again:
1
2
3
4
5
6
7
8
9
10
int array[] = {10, 20, 30, 40, 50, 60};
int x = sizeof(array) / sizeof (array[0]); // x == 6
int y;  // undefined value
int i;
for (i = 0; i < x/2; ++i)
{
  int tmp = array[x];   // error: there is no array[6]
  array[x] = array[y]; // error: there probably is no array[something]
  array[y] = tmp;
}

Those are invalid memory accesses. Repeated.

What you should do there is:
use a loop to exchange the outermost pair of element values, then the next interior pair, and so on until the innermost pair of values is exchanged.
Jul 7, 2014 at 3:27pm
Ahh . . . ok, that makes perfect sense. I just wasn't getting it, but you explained it great. Thank you for going in to so much detail and breaking it down.
Last edited on Jul 7, 2014 at 3:27pm
Topic archived. No new replies allowed.