Reputation: 61
I have successfully reversed a 1-D array but when I cout the contents, it prints out the contents then bunch of other numbers.
4
3
2
1
-858993460
-858993460
4
-858993460
-1021245226
12384668
3697177
1
14484784
14501672
-1021245434
0
Press any key to continue . . .
I can't tell why it's printing out those extra numbers. Below is my source code:
#include <iostream>
#include <array>
using namespace std;
void flipRow(int array[], int row) {
int temp;
for (int i = 0; i < 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);
for (int i = 0; i < sizeof(array); i++) {
cout << array[i] << endl;
}
system("pause");
return 0;
}
Are those addresses? Can someone tell why it's printing them out? Thank you.
Upvotes: 0
Views: 168
Reputation: 1565
Modify your for
loop in main
so the condition part of it becomes i < ROW
and modify the for
loop in flipRow
so the condition part there reads i < row/2
. sizeof
operator returns the size of your array in bytes. You have four elements in your array and they are of type integer, which on your platform is 4 bytes, so your call to sizeof
is returning 16.
Upvotes: 1