Hack Forums
[C] why this code isn't working - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] why this code isn't working (/showthread.php?tid=5889046)



why this code isn't working - Quality_k1nG - Sep 26, 2018

Code
#include<stdio.h>

int main(){
    
    int a[10],test,k,n,j,i;
    scanf("%d",&test);
    
    for (i=0;i<test;i++){
    
     scanf("%d",a[i]);
    }
    
    for(i=0;i<test;i++){
        for(j=0;j<a[1];j++){
            
            printf("hello msg\t");
        }
    }
}

I am trying to create a test cased , like if we put 2 then loop will work twice then whatever we put twice like 5 and 7 , each loop work like 5 times n and 7 times and so on


RE: why this code isn't working - 0x45 - Sep 26, 2018

Probably what you need instead is

Code
for(i=0;i<test;i++){
            printf("hello msg\t");        
}

for(j=0;j<a[1];j++){            
            printf("hello msg\t");
}

Not sure If i understood your question


RE: why this code isn't working - Quality_k1nG - Sep 27, 2018

(Sep 26, 2018 10:24 PM)0x45 Wrote:  Probably what you need instead is

Code
for(i=0;i<test;i++){
           printf("hello msg\t");        
}

for(j=0;j<a[1];j++){            
           printf("hello msg\t");
}

Not sure If i understood your question

Hey thank you for the reply. what i want is I was doing some problem on hackerearth.com there they given some test cases (suppose input is 2) then I need to go through the 2 input and again loop inside the 2

like this
https://i.imgur.com/W08pxZT.png


RE: why this code isn't working - walt3r - Sep 27, 2018

Try this one, actually I couldn't see your screenshot but I think this is the correct answer.

Code
#include<stdio.h>

int main(){
   
   int MAX_SIZE_OF_ARRAY = 10;


   int a[MAX_SIZE_OF_ARRAY],test,k,n,j,i;
   scanf("%d",&test);
   
   for (i=0;i<test;i++){
   
    scanf("%d",a[i]);
   }
   
   for(i=0;i<MAX_SIZE_OF_ARRAY;i++){
       for(j=0;j<a[i];j++){
           
           printf("hello msg\t");
       }
   }
}


Maybe this part is better with this:

http://prntscr.com/kz9ieu


RE: why this code isn't working - Legionhoster - Sep 27, 2018

Code should be like this:

Code
#include<stdio.h>
#include<conio.h>

int main()
{
  
  

    int value;
   int a[value],k,n,j,i;
  
  
  
    printf("Enter the Number of times you want to print the lines : ");
    scanf("%d", &a[value]);
    
  
       for(j=0; j<a[value]; j++){
          
           printf("hello msg\t \n");
       }

  
   return 0;
}

I hope this helps.


RE: why this code isn't working - 0xDEAD10CC - Sep 27, 2018

It would be much better if you showed the dataset rather than your poor paint drawing. Sounds similar to the codejam type of problems.

Code
void process_case(FILE *fp)
{
    // read N for iteration count ?
    // read data
}

int main(void)
{
    FILE *fp = stdin;

#if defined (__TEST_MODE__)
    fp = fopen("input.dat", "rb");
#endif

    int T, t = 1;
    fscanf(fp, "%d", &T);

    while (T--) { printf("Case #%d: ", t++); process_case(fp); }

#if defined (__TEST_MODE__)
    if (fp) fclose(fp);
#endif

    return 0;
}

You'd set it up something like this.


RE: why this code isn't working - Quality_k1nG - Sep 27, 2018

(Sep 27, 2018 06:10 PM)0xDEAD10CC Wrote:  It would be much better if you showed the dataset rather than your poor paint drawing. Sounds similar to the codejam type of problems.

Code
void process_case(FILE *fp)
{
   // read N for iteration count ?
   // read data
}

int main(void)
{
   FILE *fp = stdin;

#if defined (__TEST_MODE__)
   fp = fopen("input.dat", "rb");
#endif

   int T, t = 1;
   fscanf(fp, "%d", &T);

   while (T--) { printf("Case #%d: ", t++); process_case(fp); }

#if defined (__TEST_MODE__)
   if (fp) fclose(fp);
#endif

   return 0;
}

You'd set it up something like this.

through diagram, it would be much easier to follow idk:D anyway thank for helping