Friday 15 January 2010

Reverse Array in C++ -



Reverse Array in C++ -

i have reversed 1-d array when cout contents, prints out contents bunch of other numbers.

4 3 2 1 -858993460 -858993460 4 -858993460 -1021245226 12384668 3697177 1 14484784 14501672 -1021245434 0 press key go on . . .

i can't tell why it's printing out numbers. below source code:

#include <iostream> #include <array> using namespace std; void fliprow(int array[], int row) { int temp; (int = 0; < sizeof(array)/2; i++) { //cout << "hi" << endl; temp = array[i]; array[i] = array[row-1]; array[row-1] = temp; row--; } } int main() { const int row = 4; int array[row] = { 1, 2, 3, 4 }; fliprow(array, row); (int = 0; < sizeof(array); i++) { cout << array[i] << endl; } system("pause"); homecoming 0; }

are addresses? can tell why it's printing them out? give thanks you.

modify for loop in main status part of becomes i < row , modify for loop in fliprow status part there reads i < row/2. sizeof operator returns size of array in bytes. have 4 elements in array , of type integer, on platform 4 bytes, phone call sizeof returning 16.

c++

No comments:

Post a Comment