Print the subsequences of an array of numbers

Viewed 74

Im a beginner programmer and i was struggling to understand why this solution doesn't work. When i run the program it prints nothing

So the exercise goes like this:

We are supposed to enter an array of numbers(but i was lazy to keep entering them again and again so i wrote a sequence in a file) and what we are supposed to do is we have to find the largest sequence of non-zero numbers and print every sequence that is as big as that one. Meaning for example if we have

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

We would only print

1 1 1 

and

2 4 5

for now i am just trying to print the array without zeros or where there are zeroes i would like to print a new line but i dont get why its not working. If possible can you give me an idea on how to do it?

This is my program so far:

#include <stdio.h>
#include <stdlib.h>
#define N 100
int main()
{
int v[N];
int i=0;
int j;
int n=0;
FILE *file;
file=fopen("file","r");
if(file==NULL)
{
    printf("Error in the file.....");
    exit(1);
}
while(fscanf(file,"%d",&v[n])!=EOF)
{
    n++;
}
printf("Original Matrix: ");
for(i=0;i<n;i++)
{
    printf("%d ",v[i]);
}
printf("\n\n\n");

while(i<n)
{
    if(v[i]==0)
    {
        i++;  \\i assume that the problem lies here but i dont understand why. Im just 
                trying to tell it that if its a 0 you should skip it. Maybe im doing it 
                wrong but i dont see how yet
    }
    else
       {
           printf("%d",v[i]);
       }

    i++;
}

return 0;
}
1 Answers

You said about subsequences but given example relates to subarray(contiguous subsequences)

[if file is a text file and it contains valid number]

#include <stdio.h>
#include <stdlib.h>
#define N 100
int main()
{
    int v[N];
    int i=0;
    int j;
    int n=0;
    FILE *file;
    file=fopen("file.txt","r");
    if(file==NULL)
    {
        printf("Error in the file.....");
        exit(1);
    }
    while(fscanf(file,"%d",&v[n])!=EOF)
    {
        n++;
    }
    printf("Original Matrix: ");
    for(i=0; i<n; i++)
    {
        printf("%d ",v[i]);
    }
    printf("\n\n\n");

    i=0;
    int mx=0,cnt=0;
    // finding maximum subarray length
    while(i<n)
    {
        if(v[i]!=0)
        {
            cnt++;
        }
        else
        {
            // keeping max
            if(mx<cnt)
               mx=cnt;
            cnt=0;
        }
        i++;
    }
    // keeping max out of the loop [test case: 3 4 5 6]
    if(mx<cnt)
       mx=cnt;

    i=0,cnt=0;
    // printing maximum subarray if exists
    while(i<n)
    {
        if(v[i]!=0)
        {
            cnt++;
        }
        else
        {
            // section of printing subarray
            if(mx>0&&mx==cnt)
            {
                for(int j=i-mx; j<i; j++)
                {
                    printf("%d ",v[j]);
                }
                printf("\n");
            }
            cnt=0;
        }
        i++;
   }
   // printing max subarray out of the loop [test case: 3 4 5 6]
   if(mx>0&&mx==cnt)
   {
       for(int j=i-mx; j<i; j++)
       {
           printf("%d ",v[j]);
       }
       printf("\n");
   }


   return 0;
}
Related