Need Help with Array Loop Exchange.

Hi y'all. I'm new here and new to C++. I've been doing ok with C++ but i'm struggling with this assignment. I am supposed to write a program that declares ans initializes an array and then use a loop to exchange the outermost values. The program can't be sensitive to the number of elements in the array. I think I am on the right track. The program runs but nothing is returned. I know I need to set a value for Y, but I really don't know what the value should be. I really would appreciate any help! Thanks in advance.

#include <iostream> //preprocessor directive

using namespace std;

int main()

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

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

{
temp = x;
x = y;
y = temp;
}


system ("pause");

return 0;
}
I'm not sure why you need a loop to swap the outermost values. I'm assuming that the outer most values of an array A that has n elements are A[0] and A[n-1]. You should just be able to swap those two spots. The trick becomes finding the value of n, but it looks like you do that with the int x = sizeof (array) / sizeof (array[0]); line.
I am supposed to use a loop to continue exchanging the values until all have been switched. The output should be 60, 50, 40, 30, 20, 10
You need something to print the contents of the array. Use another for loop.
1
2
3
4
for (int j = 0; j < x; ++j)
{
    cout << array[j] << ' '; //print a space after the number so the output isn't all jammed together
}
@booradley60

Thank you for suggesting the second loop. I can now see the printout to the screen. The only problem is that the program is not running correctly. It is printing out the original array, 10 20 30 40 50 60, but not the exchanged values.
I think I need an equation for y (int y = ?) but I don't know what it should be.
Show your updated code. You'll have to run that print loop again AFTER you've performed your swaps to see an update.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    //print the array before we mess with it
    cout << "Before: ";
    for (int j = 0; j < x; ++j)
    {
        cout << array[j] << ' ';
    }

    //rearrange the array
    for ( i = 0; i < x/2; ++i)
    {
        temp = x;
        x = y;
        y = temp;
    }

    //print the array after we rearrange it
    cout << endl << "After: ";
    for (int j = 0; j < x; ++j)
    {
        cout << array[j] << ' ';
    }


What you should be thinking now is "why should I have the exact same code in 2 different places? If I have to update the logic of that loop, now I have to make that change in 2 places instead of one." This attitude will motivate learning about functions soon.

For now, just use the same loop before and after your swaps to show that your swap logic works. (At the moment, it does not.)
Last edited on
Line 17: You assign x (the size of the array) to temp. This isn;t what you want to swap.

Line 18: y is an uninitialized variable.

Where do you reference your array in the loop?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/

This is my current updated code:

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
#include <iostream> //preprocessor directive

using namespace std;

int main()

{
int array[] = { 10, 20, 30, 40, 50, 60}; //declare and initialize array
int x = sizeof (array) / sizeof (array[0]); //find the value of x
int y;
int i;
int temp;

    for (int i = 0; i < x; ++i) //prints the array before exchange
        {
        cout << array[i] << ' '; //prints a space after last number
        }

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

        {
        temp = x;
        x = y;
        y = temp;
        }

    for (int i = 0; i < x; ++i) //prints the array after exchange
        {
        cout << array[i] << ' ';
        }


return 0;
}


When I run it, it prints: "10 20 30 40 50 60"
Last edited on
You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
I did not know how to format the code until you just told me :S
I did not know how to format the code until you just told me

I gave you a link to the instructions in my first post, which you ignored.

The problems I pointed out in my earlier response still exist.

Line 22: Why are you setting temp to the size of the array?
Line 23: y is still uninitialized the first time through the loop. And why are you referencing y?

You're still not referencing your array inside the loop (there are no references to array[i]). How are you going to swap members of the array if you don't reference it?

Consider the following:
1
2
3
4
5
6
    for ( i = 0; i < x/2; ++i) //swap elements
    {   y = x - i - 1;  // Compute index of other element to swap
        temp = array[i];
        array[i] = array[y];
        array[y] = temp;
    }

Your swap elements loops is equivalent to:

1
2
    for (i=0; i < x/2; ++i)
        swap(x,y);


Where is array modified in this loop? What values should you be swapping in the loop?
I gave you a link to the instructions in my first post, which you ignored.


I didn't ignore you. I'm a slow learner. I looked at the link but didn't quite get it. It takes me awhile to pick things up, but once I know them, I know them well.

Line 22: I thought I need to store x in a temporary location and then have x take on the value of y.

Line 23: I am only referencing Y because, when I don't have it listed under main, I get an error message when the program is ran "."error: 'y' was not declared in this scope."
The size of the array doesn't change. x should be declared as a constant;
 
const int x = sizeof (array) / sizeof (array[0]);

and of course that means there is no possibility of accidentally changing x.
Topic archived. No new replies allowed.