Hi everybody - I'm taking my first programming class ever......online (which I wouldn't have done if I had it to do over). I'm working on this assignment and I cant get through to my instructor. Here's what he wants:
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. Your program may not have more than one
array. You may use a temp variable in the swapping process.
The program output should be similar to the following:
original array (element 0 first): 10 20 30 40 50 60
array after exchange (element 0 first): 60 50 40 30 20 10
NOTE: you can determine the number of elements in an array with the
following:
int array[] = { 10, 20, 30, 40, 50, 60 };
int x;
x = sizeof(array) / sizeof(array[0]);
// sizeof is a function in iostream.h
Use the sizeof() function to determine the number of elements in any arrays
that are initialized before compilation in all the following assignments. If
the number of elements is determined at run time by console input, then you
may use a counter variable to determine the number of elements in an array.
Here's what I was trying:
#include<iostream>
using namespace std;
int main() {
int array[] = { 10, 20, 30, 40, 50 };
int s = sizeof(array) / sizeof(array[0]);
int n = s - 1;
int x = 0;
while (n > s / 2) {
array[x] = array[n];
n--;
x++;
}
cout << "The inverted array is " << array[s] << endl;
system("PAUSE");
return 0;
}
But it's not giving back what I'd like..... Any insight? - And remember you're dealing with a newbie here ;) Thanks - And sorry this is so long.