0

I'm doing a challenge on HackerRank got the method figured out but there's a slight error I cannot figure out. Further information if needed is https://www.hackerrank.com/challenges/sparse-arrays

Basically I only have a problem with arr[0]. It's storing arr[0] as 'aba', then once it hits the first for loop it changes to 'ab'. Why?

Input:

4
aba
baba
aba
xzxb
3
aba
xzxb
ab

Code:

int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];
  char* test[q];
  char* s;
  int counter[q];

  for (i = 0; i < q; i++) {
    counter[i] = 0;
  }

  for (i = 0; i < n; i++) {
    arr[i] = malloc(20);
    scanf("%s", arr[i]);
  }

  scanf("%d", &q);

  for (i = 0; i < q; i++) {
    test[i] = malloc(20);
    scanf("%s", test[i]);
  }

  for (i = 0; i < n; i++) {

    for (j = 0; j < q; j++) {

      if (strcmp(arr[i], test[j]) == 0) {

        counter[j]++;
      } else {
      }
    }
  }
  for (i = 0; i < q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}
Kedar Kodgire
  • 482
  • 6
  • 22
Rand
  • 87
  • 2
  • 9
  • 1
    char*test[q] declared before q was given! – Rand Jan 28 '17 at 17:43
  • why do you declare all your variables at the top of the function? This was necessary in C89, but it's been ... almost 30 years. It's been considered bad practice for a longggg time. – Alexander Jan 28 '17 at 17:45
  • Will keep this in mind, thanks! And as to answer, I'm ~3 week year old coder – Rand Jan 28 '17 at 17:52

2 Answers2

1

You declared test and counter as array of size q before having initialize q. Move there declaration just after scanf("%d",&q);. Also move the initializing loop of counter :

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

int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];
  char* s;

  for(i=0; i<n; i++) {
    arr[i]= malloc(20);
    scanf("%s",arr[i]);
  }

  scanf("%d", &q);
  int counter[q];
  char* test[q]; 

  for(i=0; i<q; i++) {
    counter[i] = 0;
  }
  for(i=0; i<q; i++) {
    test[i]= malloc(20);
    scanf("%s",test[i]);
  }

  for(i=0; i<n; i++) {
    for(j=0; j<q; j++) {
      if (strcmp(arr[i],test[j]) == 0) {
        counter[j]++;
      }
    }
  }
  for(i=0; i<q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Thanks you!!! Argh, can't believe the silly things I do sometimes. Will accept this answer when the minimum acceptance time is over. – Rand Jan 28 '17 at 17:42
0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];


  for (i = 0; i < n; i++) {
    arr[i] = malloc(20);
    scanf("%s", arr[i]);
  }

  scanf("%d", &q);

  char* test[q];
  char* s;
  int counter[q];


  for (i = 0; i < q; i++) {
    counter[i] = 0;
  }
  for (i = 0; i < q; i++) {
    test[i] = malloc(20);
    scanf("%s", test[i]);
  }

  for (i = 0; i < n; i++) {

    for (j = 0; j < q; j++) {

      if (strcmp(arr[i], test[j]) == 0) {

        counter[j]++;
      } else {
      }
    }
  }
  for (i = 0; i < q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}

try this, use varialble after declaration and initialization

Rehan Shikkalgar
  • 1,029
  • 8
  • 16