Why my code is not working?

I am solving ATM2 (Beginner) problem but my code is not further proceeding after taking inputs

The source code is as follows:-

#include <stdio.h>
int main()
{
int T;
scanf("%d",&T);
int N[T], K[T];
int numbers[T][100];
for (int i =0; i<T; i++)
{
scanf("%d %d",&N[i],&K[i]);
for (int k=0; k<N ; k++)
{
scanf("%d",&numbers[i][k]);
}
}

    int money;
  for (int z=0; z<T; z++)
    {
        money=K[z];
        for (int j=0; j<N[z];j++)
            {
                
                if (numbers[z][j]==money)
                    {
                        printf("1");
                        money = money - numbers[z][j];
                        
                    }
                else if (numbers[z][j]<money)
                { printf("1");
                money = money - numbers[z][j];
                    
                }
                else if ( numbers[z][j]>money)
                {
                   printf("0");  
                   
                }
            }
    }
    
}

Thanks!

Please format your source code - use the “preformatted text” ( </> ) in the post editor :slight_smile:

I actually can’t get this to compile:

janitlodha-ATM2.cpp: In function ‘int main()’:
janitlodha-ATM2.cpp:11:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
         for (int k=0; k<N ; k++)

Edit:

Oh, I see - it’s a case of C being overly-permissive compared to C++ - gcc merely issues a warning.

Nevertheless, the warning is a crucially important one: try adding a printf("k: %d\n", k); on the line above scanf("%d",&numbers[i][k]); and running locally with the sample input:

2
5 10
3 5 3 2 1
4 6
10 8 6 4

and that should shed some light on the matter :slight_smile:

There are certain mistakes in your code

  1. Change for (int k=0; k<N ; k++) to for (int k=0; k<N[i] ; k++)
  2. Add a printf("\n"); in the end after a test case ends, for a newline.
  3. Closing Braces aren’t properly placed in the end.

Your corrected code finally looks like:

   #include <stdio.h>
   int main()
 {   int T;
    scanf("%d",&T);

int N[T], K[T];
int numbers[T][100];

for (int i =0; i<T; i++)
{   scanf("%d %d",&N[i],&K[i]);
        for (int k=0; k<N[i] ; k++)
        {   scanf("%d",&numbers[i][k]);}
}

  int money;
  for (int z=0; z<T; z++)
    {
        money=K[z];
        for (int j=0; j<N[z];j++)
            {
                
                if (numbers[z][j]==money)
                    {   printf("1");
                        money = money - numbers[z][j]; }
                        
                else if (numbers[z][j]<money)
                { printf("1");
                money = money - numbers[z][j];}
                
                else if ( numbers[z][j]>money)
                {
                   printf("0");  
                }
            }
            printf("\n");
    }
}

General Tip: Improve upon your code writing style.