samedi 25 mai 2019

How to fix unsigned char array returning the same value twice?

I am trying to solve UVa problem 11956 (Brainf**k) and my solution works but only when I debug it. When I run the solution, I get the same result for both cases.

To summarise what I am trying to solve:

  • You start with 100 bytes of memory initialized to 0s and a pointer to the first element of this memory
    • Each byte is circular (0 - 1 = 255)
    • The pointer is circular (0 -1 = 99)
  • The first line of input tells you the number of test cases that follow
  • The following lines contain Brainf*** code that means the following
    • ">" Increment the pointer (to point to the next cell to the right).
    • "<" Decrement the pointer (to point to the next cell to the left).
    • "+" Increment (increase by one) the byte at the pointer.
    • "-" Decrement (decrease by one) the byte at the pointer.
    • "." Output the value of the byte at the pointer.
  • Output the 100 bytes of memory as hex

As I said, I stepped through my program in CLion and I got the correct output but my final answer when run returns the same output twice.

I am using C++11


#include <cstdio>
#include <cstring>

using namespace std;

int main() {
  int tc, Case = 0;
  scanf("%d", &tc);
  while (tc--) {
    char line[100001] = {};
    int pointer = 0;
    unsigned char mem[100] = {0};
    scanf("%s", &line);
    for (int j = 0; j < strlen(line);j++) {
      switch (line[j]) {
        case '+': mem[pointer]++; break;
        case '-': mem[pointer]--; break;
        case '>': pointer++     ; break;
        case '<': pointer--     ; break;
      }
      pointer = (pointer + 100) % 100;
    }
    printf("Case %d:", ++Case);
    for (unsigned char &hex : mem) {
      printf(" %02X", hex);
    }
    printf("\n");
  }
  return 0;
}


The sample input is quite large so I left a paste

https://pastebin.com/EWpa6cFC

The correct output is:

Case 1: 2B 2E 31 34 37 3A 3D 40 43 46 49 4C 4F 52 55 58 5B 5E 61 64 67 6A 6D 70 73 76 79 7C 7F 82 85 88 8B 8E 91 31 72 99 9C 9F A2 A5 A8 AB AE B1 B4 B7 BA BD C0 C3 C6 C9 CC CF D2 D5 D7 D9 DB DD DF E1 E3 E5 E7 E9 EB ED EF F1 F3 F5 F7 F9 FB FD FF 01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F 21 23 25 27 29
Case 2: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63

I am getting:

Case 1: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63
Case 2: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63

Aucun commentaire:

Enregistrer un commentaire