[Solved] What am I doing wrong? The loop runs infinitely….P.S.- I am a newbie [closed]


There are basically two mistakes in your logic

First is when you are finding smallest number

if(a[k]<small)
            {
            small=a[k];
        }

This is wrong as there will some condition arise when a[k] will be 0. So each time when the loop runs small will always be zero.
See for example

see example

So it should be replace with

if(a[k]<small **&& a[k]!=0**)
            {
            small=a[k];
        }

Another problem is when you are calculating sum

if(sum==0)
        break;
    printf("%d\n",sum);
    sum=0;

Here, a condition will arise when there will be some negative elements, so result may either be 0 or negative so it should be replaced with

if(sum==0**||sum<0**)
        break;
    printf("%d\n",sum);
    sum=0;

Due to this your code is in infinite loop

See this image also for better explanation

15

solved What am I doing wrong? The loop runs infinitely….P.S.- I am a newbie [closed]