in my code it is reading infinite elements why?

/* c program to find largest subarray whose elements add upto 0
it keep on reading for elements I do not why? for example if i give input
as 4 as number of elements it has to take 4 elements for some elements it 
is working fine for some like 1 2 -3 4 it is again asking to enter
elements even after taking 4 elements */


#include <stdio.h>
#include<stdlib.h>
void popout (int a[100], int q, int n,int j) 
{
    int z; z=n;
    for (int o = q; o < n - 1; o++)
    {
        a[o] = a[o + 1];  //deletes one number
    } 
    int g = j;
    if(g != 1)
    {
        z--;   //function calls recursively till we are 
               // deleting only one number 
        for(int i=0;i<n;i++) {     
            popout(a,a[i],z,g-1); // after deleting one number again 
                                  //again runs a loop to delete other 
                                  //number from already deleted array
        }
        g--;
    }
} 
    
    
    
int main () 
{
    int n;int f;
    int a[100];
    int b[100];
    printf ("enter number of elemnts");
    scanf ("%d",&n);
    printf ("enter elements");
    for (int i = 0; i < n; i++)
        scanf ("%d", &a[i]);
    for (int i = 0; i < n; i++)
        b[i] = a[i];
    for (int j = 1; j < n; j++)
        a[0] = a[0] + a[j];
    if (a[0] == 0)
        printf ("%d", n);
    for (int i = 0; i < n; i++)
        a[i] = b[i];
    /* pop out number by number */ 
    for(int p = 1;p < n; p++) {  //p:number of elements to be deleted at onece
        for(int i=0;i<n;i++) {  
            popout(a,a[i],n,p);
            for(int g=1;g<n-1;g++) {
                a[0]+=a[g];
            }
            if(a[0]==0)
            {
                printf("%d",n-p); exit;
            }
            else {
                for(int i=0;i<n;i++)
                a[i]=b[i];
            }
        }
    }
    return 0;    
}


Read more here: https://stackoverflow.com/questions/67001898/in-my-code-it-is-reading-infinite-elements-why

Content Attribution

This content was originally published by Srujith Chaithanya at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: