F
F
fewgesto2021-12-20 23:38:26
C++ / C#
fewgesto, 2021-12-20 23:38:26

Why doesn't it display d values ​​correctly?

#include <stdio.h>
#pragma warning(disable : 4996)
int main() {
  unsigned char a[81], perm[256], g[81], d = 0, n = 0;
  for (int f = 0; f < 81; f++) {
    scanf("%[^\n]d", &a);
  }
  int f = 0;
  while (a[f] != '\0') {
    n++; f++;
  }
  for (int b = 0; b < 256; b++) {
    perm[b] = b;
    for (int c = 0; c < n; c++) {
      if (a[c] == perm[b]) {
        g[c] = perm[b];
      }
    }
  }
  for (int c = 0; c < n; c++) {
    if (g[c] == g[c + 1]) {
      d++;
    }
    else d = 1;
    //printf(" %d ", g[c]);
    printf(" %c=%d\n ", g[c], d);
  }

}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GavriKos, 2021-12-20
@GavriKos

Are you seriously suggesting that we understand what goes wrong in an algorithm that has no input and no description? And even with unreadable code?

C
CityCat4, 2021-12-21
@CityCat4

Not a single comment.
Who will be interested in understanding this mess? The debugger is in the teeth - and trace the program step by step, tracking the counters.

A
Alexander Ananiev, 2021-12-21
@SaNNy32

Apparently it needs to

for (int c = 0; c < n; c++) {
    if (g[c] == g[c + 1]) {
      d++;
      continue;
    }
    else d = 1;
    //printf(" %d ", g[c]);
    printf(" %c=%d\n ", g[c], d);
  }

V
Vladimir Dubrovin, 2021-12-22
@z3apa3a

%d is an int and you are passing unsigned char.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question