0

Here's description for code below. For the input sequence of numbers from 1 to 3000 my code stops at 1040 input. I can't understand why it is happening. Please help!!

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

void search(int x,int *array,int a,int b,int *c)
{
  for(int i=b+1;*(array+i)<=x;i++)
  {
      if(*(array+i)==x)
      {
        *c=1;
        printf("%d %d %d\n",*(array+b),x,(*(array+b)+x));
        return;
      }
  }
}

int main()
{
  int t,n,i=1,j,sum,flag;
  scanf("%d",&t);
  getchar();
  while(i<=t)
  {
    flag=0;
    scanf("%d",&n);
    printf("%d\n",n);
    getchar();
    int *array=(int *)malloc(n*sizeof(int));
    for(j=0;j<n;j++)
    {
        scanf("%d",(array+j));
        getchar();
        printf("%d\n",*(array+j));
    }
    scanf("%d",&sum);
    getchar();
    for(j=0;*(array+j)<=(sum/2);j++)
    {
        search((sum-*(array+j)),array,n,j,&flag);
    }
    if(flag==0)
    {
        printf("-1\n");
    }
    i++;
  }
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
  • How do you know `scanf()` stops working? You're not checking the return value to see if it worked or not. – Andrew Henle Dec 10 '18 at 13:50
  • i should probably mention that casting the result of `malloc()`, or any of the allocation methods, is [unnecessary](https://stackoverflow.com/a/605858/8420233) and causes code noise. – absoluteAquarian Dec 10 '18 at 14:12
  • Andrew Henle actually i tried printing all the values just after scanf() instrucion...Now when i gave 3000 numbers say (1,2,3.....3000) then it is printing upto 1040 element and stopping...i am unable to understand – Sai Sreenivas Dec 12 '18 at 04:31
  • @Tau...I tried without using malloc() even then the problem persists...:( – Sai Sreenivas Dec 12 '18 at 04:39

0 Answers0