-1
#include <stdio.h>
int main()
{
        int test_no ,count=1,i,n,j;
        scanf("%d",&test_no);
        fflush(stdin);
        int arr1[test_no];
        for(i=0;i<test_no;i++)
        {
                scanf("%d",&n);
                printf("\n");
                int arr[n];
                for(j=0;j<n;j++)
                {
                        fflush(stdin);
                        scanf("%d",&arr[i]);
                }
                for(j=1;j<=n-1;j++)
                {
                        if(arr[j-1]>arr[j])
                        {
                                count++;
                        }
                }
                if(n==1)
                {
                        arr1[i]=1;
                }
                else
                {
                        arr1[i]=count;
                }
                count=1;
        }
        for(i=0;i<test_no;i++)
        {
                printf("%d\n",arr1[i]) ;
        }
        return 0;
}

This solution is to this problem.

I am not getting the desired output for the third case , it's giving me output as 3 or 4 depending on whether I place fflush(stdin) before scanf("%d",arr[i]) or after scanf("%d",arr[i]) , please tell the problem with this code .

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 4
    It looks quite funny, but please: format your code properly. – pzaenger Jul 12 '16 at 10:13
  • I'm reopening the question, as the marked dupe is very much relevant but __not__ a correct dupe. Other than the issue discussed in marked dupe, there __are__ other issues which needs to be addressed. – Sourav Ghosh Jul 12 '16 at 11:15
  • For Reference: The marked dupe: http://stackoverflow.com/questions/22901901/what-does-fflushstdin-do-in-c-programing – Sourav Ghosh Jul 12 '16 at 11:15

1 Answers1

5

In no some magical way.

First of all, fflush(stdin); invokes undefined behavior. Don't use that.

Quoting C11, chapter §7.21.5.2, The fflush function (emphasis mine)

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

That said,

for(j=0;j<n;j++)
{
    fflush(stdin);
    scanf("%d",&arr[i]);
}

looks pretty wrong to me, arr[i] is not guaranteed to be within bounds. It should rather be

scanf("%d",&arr[j]);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yes , i just missed that point , thanks , but this undefined behaviour which you mentioned regarding fflush(stdin) , that I could not relate it properly , can you please explain this once again – RADHA GOGIA Jul 12 '16 at 10:26
  • @RADHAGOGIA: Is google unreachble from your site? You are expected to show at least a bit of effort on your own. If not for `fflush(stdin)`, the at least for "undefined behaviour"! – too honest for this site Jul 12 '16 at 10:29
  • @RADHAGOGIA Which part you did not understand? All it says is, use `fflush` with a stream which is associated with output, like `stdout`. Don;t use it on input streams, like `stdin`. – Sourav Ghosh Jul 12 '16 at 13:26