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;
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.
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.
//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.)
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/
#include <iostream> //preprocessor directive
usingnamespace 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;
}
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
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;
}
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."