We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I haven't found a C solution that actually uses the code as it is given. I wanted something that doesn't change any of the code but still returns the result in the format the author intended. If you follow my comments you can see how to pass values between functions in C. I hope this helps people who were also unsatisfied with solutions that were written entirely in the main function or those who've wondered what to do about result_size in the C starter code of so many of these early exercises.
#define HOWMANYRESULTS 2int*getRecord(ints_size,int*s,int*result_size){// set best and worst to first gameintworst=s[0];intbest=s[0];// let main know how many results you'll print*result_size=HOWMANYRESULTS;// allocate heap space for the results and initialize to 0int*result=malloc(sizeof(int)**result_size);result[0]=0;result[1]=0;// start loop from the second gamefor(inti=1;i<s_size;i++){if(s[i]>best){// increase counter for best and reset bestresult[0]++;best=s[i];}if(s[i]<worst){// increase counter for worst and reset worstresult[1]++;worst=s[i];}}// return the address of the results on heap to mainreturnresult;}
I'm guessing that other people have also struggled with the common format for all C starter code so I have added some comments to the part of main that I found tricky:
// how many things will be printedintresult_size;// call helper function, expect implementer to return// an int pointer (to the results) and set result_sizeint*result=getRecord(n,s,&result_size);for(inti=0;i<result_size;i++){// when i = 0 this evaluates to 0 which is same as// false and we don't print a spaceif(i){printf(" ");}// on every round through the loop print another resultprintf("%d",result[i]);}
I know that allocating space on the heap is not as efficient/fast in the real world as just doing it all in main or printing in getRecord, my focus was on using the starter code and practicing with passing values between functions in C. Solution is O(n) runtime and O(1) space.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breaking the Records
You are viewing a single comment's thread. Return to all comments →
I haven't found a C solution that actually uses the code as it is given. I wanted something that doesn't change any of the code but still returns the result in the format the author intended. If you follow my comments you can see how to pass values between functions in C. I hope this helps people who were also unsatisfied with solutions that were written entirely in the main function or those who've wondered what to do about result_size in the C starter code of so many of these early exercises.
I'm guessing that other people have also struggled with the common format for all C starter code so I have added some comments to the part of main that I found tricky:
I know that allocating space on the heap is not as efficient/fast in the real world as just doing it all in main or printing in getRecord, my focus was on using the starter code and practicing with passing values between functions in C. Solution is O(n) runtime and O(1) space.