Ok so I have this code as part of a project to print an array:
#include <iostream>
#include <string>
#define ROWS 12
#define COLS 10
using namespace std;
void image_print(unsigned char image[ROWS][COLS]) {
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
cout << +image[r][c] << " ";
}
cout << endl;
}
}
The output looks like this:
0 0 0 0 0 0 0 0 0 0
0 64 10 55 127 235 203 241 163 0
0 207 115 62 37 77 43 186 1 0
0 1 119 234 125 172 90 206 103 0
0 113 193 181 158 69 31 85 212 0
0 161 84 27 145 121 211 192 123 0
0 114 4 29 214 80 231 64 71 0
0 173 251 191 221 241 94 76 81 0
0 126 53 138 141 43 78 32 108 0
0 244 4 11 193 240 99 56 154 0
0 131 141 82 198 212 92 217 148 0
0 0 0 0 0 0 0 0 0 0
But I want it to look evenly spaced like this:
0 0 0 0 0 0 0 0 0 0
0 64 10 55 127 235 203 241 163 0
0 207 115 62 37 77 43 186 1 0
0 1 119 234 125 172 90 206 103 0
0 113 193 181 158 69 31 85 212 0
0 161 84 27 145 121 211 192 123 0
0 114 4 29 214 80 231 64 71 0
0 173 251 191 221 241 94 76 81 0
0 126 53 138 141 43 78 32 108 0
0 244 4 11 193 240 99 56 154 0
0 131 141 82 198 212 92 217 148 0
0 0 0 0 0 0 0 0 0 0
Are there any clever ways I can do that?
#include <iomanip>
cout << setw(3) << +image[r][c] << " ";
setw
is an I/O manipulator which sets the field width for the next item to be output.