Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

why this code isn't working

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
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
Hack the World!
(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
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
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.
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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by F.A.S.TJul 22, 2018 10:57 AM 1 119 Jul 22, 2018 10:57 AM
Last Post: aeiro
[Java] by txkhaarOct 26, 2017 08:19 AM 1 631 Oct 26, 2017 08:19 AM
Last Post: XMR
[C] by JASOct 2, 2017 03:35 PM 33 1,142 Oct 2, 2017 03:35 PM
Last Post: HiddenEyE