0

So I have this code that is meant to print out an array filled with random numbers in a square grid with the outside edges being zeroes (to make a black border).

I thought I had everything programmed correctly but it doesn't print out the arrays, I just get a bunch of random symbols and weird spacing. I feel like there is just a small error I'm not noticing. Here is what I have.

#include <iostream>
#include <string>
#define ROWS 12
#define COLS 10
#define HIGHEST_NUMBER 255
#define LOWEST_NUMBER 0

using namespace std;
void image_print(unsigned char image[][COLS], int nRows){
for (int r = 0; r < nRows; r++){
    for (int c = 0; c < COLS; c++)
        cout << image[r][c] << endl;
  }
}
int main() {

srand(8); //random number seed
unsigned char Original_Image[ROWS][COLS];

int row, col;

for (row = 0; row < ROWS; row++) {    //fills everything with zeroes
    for (col = 0; col < COLS; col++) {
        Original_Image[row][col] = 0;
    }
}

cout << "Original :" << endl;
image_print(Original_Image, ROWS);

}
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Catchenre
  • 3
  • 3
  • `unsigned char` is interpreted as readable character in cout. A trick is adding a plus sign to convert it into `unsigned int` implicitly. Eg: `cout << +image[r][c] << endl;` – Louis Go Sep 22 '20 at 02:35
  • `#define ROWS 12`.... why? Just why? – Aluan Haddad Sep 22 '20 at 02:36
  • I removed the [tag:c] tag as this is obviously C++ and not C. See https://meta.stackoverflow.com/questions/252430/disallow-the-tagging-of-questions-with-both-c-and-c-tags – Nate Eldredge Sep 22 '20 at 03:09
  • @NateEldredge very good point! thank you for the critique – Catchenre Sep 22 '20 at 03:13
  • 1
    Perhaps I missed the setting of values to random values. Current you fill the array with `0` which is the ASCII *nul-character*. So if your intent was to print characters, you are about 32-characters from the first visible ASCII character [ASCII Table and Description](http://www.asciitable.com/) – David C. Rankin Sep 22 '20 at 03:19

0 Answers0