0

I want to display the calendar data in ascending order.

If the date appears several times I need to display it once. The code works if the date appears five or less times in the input, if the date appears more than five times at the output it will show up twice.

I don't see the error.

#include <stdio.h>
#include <stdlib.h>

struct date {
  int zi;
  int luna;
};
int main() {

  int n, i, j, k = 0, l;
  char c;
  scanf("%d", &n);
  struct date v[100];
  for (i = 0; i < n; i++) {
    scanf("%d %c %d", &v[i].zi, &c, &v[i].luna);
  }

  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (v[i].luna > v[j].luna) {
        struct date temp = v[i];
        v[i] = v[j];
        v[j] = temp;
      } else if (v[i].luna == v[j].luna && v[i].zi > v[j].zi) {
        struct date temp = v[i];
        v[i] = v[j];
        v[j] = temp;
      }
    }
  }

  printf("\n");
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (v[i].luna == v[j].luna && v[i].zi == v[j].zi && i != j) {
        printf("\nOK\n");
        for (l = j; l < n - 1; l++) {
          //    v[l].luna=v[l+1].luna;
          //    v[l].zi=v[l+1].zi;
          v[l] = v[l + 1];
        }
        n--;
      }
    }
  }
  for (i = 0; i < n; i++) {

    if (v[i].luna < 10 && v[i].zi >= 10) {

      printf("%d-0%d\n", v[i].zi, v[i].luna);
    } else if (v[i].zi < 10 && v[i].luna >= 10) {

      printf("0%d-%d\n", v[i].zi, v[i].luna);
    } else if (v[i].zi < 10 && v[i].luna < 10) {

      printf("0%d-0%d\n", v[i].zi, v[i].luna);
    } else
      printf("%d-%d\n", v[i].zi, v[i].luna);
  }
}
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
ally
  • 11
  • 3
  • 1
    What did you find when you debugged your code using the simplest possible input that gives unexpected output? – aschepler Jul 23 '18 at 10:37
  • 1
    Time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), especially how to use a debugger to step through your code line by line. – Some programmer dude Jul 23 '18 at 10:37
  • Related: [How to read / parse input in C? The FAQ.](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) (Especially with regards to using `scanf()` on potentially malformed user input *without checking the return code*.) Also, not a [mcve]. – DevSolar Jul 23 '18 at 10:39
  • 1
    And, please choose a better title which describes the actual problem. – Sourav Ghosh Jul 23 '18 at 10:44
  • 1
    @jan The code review site is for code that works correctly. This code doesn't. – Support Ukraine Jul 23 '18 at 10:54
  • Thanks for the info. Anyway this question is about the community debugging his code. That's not what stack exchange is for. – Jan Jul 23 '18 at 12:59

1 Answers1

0

The problem seems to be this block:

  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (v[i].luna == v[j].luna && v[i].zi == v[j].zi && i != j) {
        printf("\nOK\n");
        for (l = j; l < n - 1; l++) {
          //    v[l].luna=v[l+1].luna;
          //    v[l].zi=v[l+1].zi;
          v[l] = v[l + 1];
        }
        n--;
      }
    }
  }

When you remove a duplicate (i.e. by shifting all elements towards the start and decrementing n) you still increment j. Consequently, your loop will skip one element and you may end up with duplicates.

The solution could be as simple as decrementing j at the same time as you decrement n.

BTW: It seems strange that the j-loop start from zero. I would expect it to start from i+1

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63