3

I am trying to print results of my sudoku solving program into terminal like this:

  +-------+-------+-------+
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  +-------+-------+-------+
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  +-------+-------+-------+
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  | 1 2 3 | 4 5 6 | 7 8 9 |
  +-------+-------+-------+

I store my solution in one dimensional array and I cannot find a way to print it. This is what I came up with so far:

printf("| %c %c %c | %c %c %c | %c %c %c |\n", test[0],test[1],test[2],test[3],test[4],test[5],test[6],test[7],test[8]);

I cannot use any cycles because I need to draw the "walls" around the numbers. Is there any better way to do it? And why does

char test[] = {'1','2','3','4','5','6','7','8','9'};
int i = 0;
printf("| %c %c %c | %c %c %c | %c %c %c |\n",   test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++]);

returns | 9 8 7 | 6 5 4 | 3 2 1 | Thanks.

5
  • What about considering using loops than doing manually ? Commented Aug 29, 2015 at 14:56
  • is it a multidimensional array? which part you feel it's difficult for you? Commented Aug 29, 2015 at 14:59
  • i can tell you are using gcc by reading the evaluation order. why do you say you can't use a loop? think it twice. that's not true. Commented Aug 29, 2015 at 15:01
  • It is one dimensional array. I don't know how can I use loops since I print three numbers and then the | character. And there are the +-----+-----+ lines. I can print the picture above with hard coded numbers but I don't know how to put numbers from my array there Commented Aug 29, 2015 at 15:07
  • 1
    replace this kind of line: printf("| %c %c %c | %c %c %c | %c %c %c |\n", test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++],test[i++]); with printf("| %c %c %c | %c %c %c | %c %c %c |\n", test[i],test[i+1],test[i+2],test[i+3],test[i+4],test[i+5],test[i+6],test[i+7],test[i+8]); i+=9; Commented Aug 30, 2015 at 15:14

4 Answers 4

5

Here's one way to decompose this into (nested) loops:

void print_data_row(const char *p) {
    printf("|");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf(" %c", p[i*3 + j]);
        }
        printf(" |");
    }
    printf("\n");
}

void print_separator(void) {
    printf("+-------+-------+-------+\n");
}

...
print_separator();
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        print_data_row(&board[(i*3 + j) * 9]);
    }
    print_separator();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is exactly what I was looking for. I tried a similar approach but couldn't get the incrementation right and when I did, it seemed too complicated so I just tried the non-loop version. Does that mean that there isn't any simpler way to do this in C? Something like printf("%c %c %c", array of elements to print); ?
@Milda You can't automatically "expand" an array to a list of function arguments. You can of course pass each element separately: printf("%c %c %c", array[i], array[i + 1], array[i + 2]);
That was basically why I asked this question. I fixed my nested-loops version (forgot to add j in one loop) and it works perfectly now. Thanks
1

Try this:

for(int i = 0; i < 13; i++){ // 13 because 9 numbers + 4 separators
  for(int j = 0; i < 13; i++){
    if((i % 4 == 0) && (j % 4 == 0)){
      printf("+"); // separator crossing
    } else if (i % 4 == 0){
      printf("-"); // vertical separator
    } else if (j % 4 == 0){
      printf("|"); // horizontal separator
    } else {
      printf("%d",sudoku[row(i)][row(j)]); // sudoku is the 2d result array
    }
  printf("\n"); // end of line
}

int row(j){ // row (or column) without counting separators
  int nOfSeparator = (i - (i % 4))/4;
  return (13 - nOfSeparator);
}

Comments

1

No loops? Ok. I'd be inclined to say this is even a superior solution compared to a single or nested loop. It can be seen to do what it is supposed to do.

char *t = test;
printf("+-------+-------+-------+\n");
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("+-------+-------+-------+\n");
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("+-------+-------+-------+\n");
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
t += 9;
printf("| %c %c %c | %c %c %c | %c %c %c |\n", t[0],t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]);
printf("+-------+-------+-------+\n");

1 Comment

Thanks, this is something I ended up with.I was just looking for something like printf("%c %c %c", array of elements to print) so I wouldn't have to use nested loops since it seemed too complicated (and therefore wrong) to me.
1

Q. Is there any better way to do it?
A. Try using loops to achieve the same.

For your second question, the answer is that the order of evaluation of function parameters is not certain in C. Hence, you never know, which of those nine test[i++] gets evaluated first. Hence, the unknown result (undefined behavior actually). More here.

1 Comment

It's not just an unknown result. Modifying the same variable twice (without a sequence point in between) has undefined behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.