2

I'm having this issue while trying to solve this question on HackerRank. On repl.it my code goes well, but on their console i have this problem.

Code:

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

int main() {
  int t; //qtd de viagens
  int m; //dinheiro para os sorvetes
  int n; //qtd de sabores de sorvetes
  int c[n+1]; //preço de cada sorvete
  int r[t];
  int s[t];

  scanf("%d", &t);
  for(int j = 0; j < t; j++){
    scanf("%d", &m);
    scanf("%d", &n);
      for(int i = 1; i <= n; i++){
        scanf("%d", &c[i]);
      }
      for (int i = 1; i < n; i++){
        for(int k =i+1; k <= n; k++){
          if (c[i]+c[k] == m){
            r[j] = i;
            s[j] = k;
          }
        }
      }
  }  
  for(int i = 0; i < t; i++){
    printf("%d %d\n", *&r[i], *&s[i]);
  }
    return 0;
}

Input:

 2
 4
 5
 1 4 5 3 2
 4
 4
 2 2 4 3

Output on repl.it:

1 4
1 2

Output on HackerRank:

~ no response on stdout ~

It also gives me a Segmentation Fault message.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Gabriel Mancini
  • 23
  • 1
  • 1
  • 4

2 Answers2

2

You have 2 problems here:

  1. You are using VLA and its not supported by all compilers. I'm talking about that: int c[n+1] for example. Chage that to dynamic allocation

  2. Initiallize variables with values before you use them (like you did with n and t).

t.elazari
  • 300
  • 2
  • 11
1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
  int t, m, n; 
  //Declaration of dynamic arrays "r", "s" and "c"
  int *c, *r, *s;

  scanf("%d", &t);
  //Allocation of memory for  the int arrays "r" and "s"
  r = malloc(t * sizeof(int));
  s = malloc(t * sizeof(int));
  for(int j=0; j < t; j++){
    scanf("%d", &m);
    scanf("%d", &n);
   //Allocation of memory for the int array "c"
    c = malloc(n+1 * sizeof(int));
      for(int i = 1; i <= n; i++){
        scanf("%d", &c[i]);
      }
      for (int i = 1; i < n; i++){
        for(int k = i+1; k <= n; k++){
          if ((c[i] + c[k]) == m){
            r[j] = i;
            s[j] = k;
          }
        }
      }
   free(c);
  }  
  for(int i = 0; i < t; i++){
    printf("%d %d\n", *&r[i], *&s[i]);
  }
  free(r);
  free(s);

  return 0;
}

I use valgrind to detect memory leaks:

==53541== HEAP SUMMARY:
==53541==     in use at exit: 0 bytes in 0 blocks
==53541==   total heap usage: 6 allocs, 6 frees, 2,081 bytes allocated
==53541== 
==53541== All heap blocks were freed -- no leaks are possible
P.A
  • 741
  • 4
  • 16
  • And how about using `free()` as well? – Tomasz Plaskota Mar 14 '17 at 11:25
  • Please provide explanation (and possible reasoning) about what kind of modification you have made in the original code to write the code you have provided. – Muntasir Mar 14 '17 at 11:26
  • @Muntasir I declared a dynamic arrays and I allocated for it the memory address.I edited my answer you can check it now. – P.A Mar 14 '17 at 11:32
  • Note that it is [redundant and potentially dangerous to cast the result of malloc and friends in C](http://stackoverflow.com/q/605845/253056). – Paul R Mar 14 '17 at 13:01
  • Also your code has a huge memory leak - look at where you are allocating and freeing `c`. – Paul R Mar 14 '17 at 13:04
  • @PaulR I edited the answer. Can you check with me if it still any memory leaks? – P.A Mar 14 '17 at 14:00
  • @HmidiSlim: yes, that looks better - also thanks for removing the redundant casts. Down-vote removed. – Paul R Mar 14 '17 at 14:35
  • Thanks, i'm learning C and I don't know a lot of functions yet. I'm also having some difficulties with memory allocation. – Gabriel Mancini Mar 14 '17 at 14:51
  • @GabrielMancini don't forget to mark the question as answered. – P.A Mar 14 '17 at 14:54