c++ - Swap elements in array to reverse an array -
i got assignment reverse dynamic array in c++. far, logic, thinking of loop thru array reverse it. , here comes code :
int main () { const int size = 10; int num_array[size]; srand (time(null)); (int count = 0; count< sizeof(num_array)/sizeof(num_array[0]) ; count++){ /* generate secret number between 1 , 100: */ num_array[count] = rand() % 100 + 1; cout << num_array[count] << " " ; } reverse(num_array[size],size); cout << endl; system("pause"); return 0; } void reverse(int num_array[], int size) { (int count =0; count< sizeof(num_array)/sizeof(num_array[0]); count++){ cout << num_array[sizeof(num_array)/sizeof(num_array[0])-1-count] << " " ; } return; } somehow think logic there code doesn't works, there's error. however, teacher told me isn't way question wants. , here question :
write function reverse reverses sequence of elements in array. example, if reverse called array containing 1 4 9 16 9 7 4 9 11, array changed 11 9 4 7 9 16 9 4 1. so far, told in reverse method, need swap array element. here's question how swap array element array entered reversed?
thanks in advance.
updated portion
int main () { const int size = 10; int num_array[size]; srand (time(null)); (int count = 0; count< size ; count++){ /* generate secret number between 1 , 100: */ num_array[count] = rand() % 100 + 1; cout << num_array[count] << " " ; } reverse(num_array,size); cout << endl; system("pause"); return 0; }
void reverse(int num_array[], const int& size) { (int count =0; count< size/2; count++){ int first = num_array[0]; int last = num_array[count-1]; int temp = first; first = last; last = temp; } }
you reverse function should this:
void reverse(int* array, const size_t size) { (size_t = 0; < size / 2; i++) { // stuff... } } and call like:
reverse(num_array, size);
Comments
Post a Comment