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.
#include<stdarg.h>#include<stdio.h>#include<stdlib.h>#include<time.h>#define MIN_ELEMENT 1#define MAX_ELEMENT 1000000intsum(intcount,...){va_listargs;va_start(args,count);intresult=0;for(inti=0;i<count;i++){result+=va_arg(args,int);}va_end(args);returnresult;}intmin(intcount,...){va_listargs;va_start(args,count);intresult=va_arg(args,int);for(inti=1;i<count;i++){intcurrent=va_arg(args,int);if(current<result){result=current;}}va_end(args);returnresult;}intmax(intcount,...){va_listargs;va_start(args,count);intresult=va_arg(args,int);for(inti=1;i<count;i++){intcurrent=va_arg(args,int);if(current>result){result=current;}}va_end(args);returnresult;}inttest_implementations_by_sending_three_elements(){srand(time(NULL));intelements[3];elements[0]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[1]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[2]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;fprintf(stderr,"Sending following three elements:\n");for(inti=0;i<3;i++){fprintf(stderr,"%d\n",elements[i]);}intelements_sum=sum(3,elements[0],elements[1],elements[2]);intminimum_element=min(3,elements[0],elements[1],elements[2]);intmaximum_element=max(3,elements[0],elements[1],elements[2]);fprintf(stderr,"Your output is:\n");fprintf(stderr,"Elements sum is %d\n",elements_sum);fprintf(stderr,"Minimum element is %d\n",minimum_element);fprintf(stderr,"Maximum element is %d\n\n",maximum_element);intexpected_elements_sum=0;for(inti=0;i<3;i++){if(elements[i]<minimum_element){return0;}if(elements[i]>maximum_element){return0;}expected_elements_sum+=elements[i];}returnelements_sum==expected_elements_sum;}inttest_implementations_by_sending_five_elements(){srand(time(NULL));intelements[5];elements[0]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[1]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[2]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[3]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[4]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;fprintf(stderr,"Sending following five elements:\n");for(inti=0;i<5;i++){fprintf(stderr,"%d\n",elements[i]);}intelements_sum=sum(5,elements[0],elements[1],elements[2],elements[3],elements[4]);intminimum_element=min(5,elements[0],elements[1],elements[2],elements[3],elements[4]);intmaximum_element=max(5,elements[0],elements[1],elements[2],elements[3],elements[4]);fprintf(stderr,"Your output is:\n");fprintf(stderr,"Elements sum is %d\n",elements_sum);fprintf(stderr,"Minimum element is %d\n",minimum_element);fprintf(stderr,"Maximum element is %d\n\n",maximum_element);intexpected_elements_sum=0;for(inti=0;i<5;i++){if(elements[i]<minimum_element){return0;}if(elements[i]>maximum_element){return0;}expected_elements_sum+=elements[i];}returnelements_sum==expected_elements_sum;}inttest_implementations_by_sending_ten_elements(){srand(time(NULL));intelements[10];elements[0]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[1]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[2]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[3]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[4]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[5]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[6]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[7]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[8]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;elements[9]=rand()%(MAX_ELEMENT-MIN_ELEMENT+1)+MIN_ELEMENT;fprintf(stderr,"Sending following ten elements:\n");for(inti=0;i<10;i++){fprintf(stderr,"%d\n",elements[i]);}intelements_sum=sum(10,elements[0],elements[1],elements[2],elements[3],elements[4],elements[5],elements[6],elements[7],elements[8],elements[9]);intminimum_element=min(10,elements[0],elements[1],elements[2],elements[3],elements[4],elements[5],elements[6],elements[7],elements[8],elements[9]);intmaximum_element=max(10,elements[0],elements[1],elements[2],elements[3],elements[4],elements[5],elements[6],elements[7],elements[8],elements[9]);fprintf(stderr,"Your output is:\n");fprintf(stderr,"Elements sum is %d\n",elements_sum);fprintf(stderr,"Minimum element is %d\n",minimum_element);fprintf(stderr,"Maximum element is %d\n\n",maximum_element);intexpected_elements_sum=0;for(inti=0;i<10;i++){if(elements[i]<minimum_element){return0;}if(elements[i]>maximum_element){return0;}expected_elements_sum+=elements[i];}returnelements_sum==expected_elements_sum;}intmain(){intnumber_of_test_cases;scanf("%d",&number_of_test_cases);while(number_of_test_cases--){if(test_implementations_by_sending_three_elements()){printf("Correct Answer\n");}else{printf("Wrong Answer\n");}if(test_implementations_by_sending_five_elements()){printf("Correct Answer\n");}else{printf("Wrong Answer\n");}if(test_implementations_by_sending_ten_elements()){printf("Correct Answer\n");}else{printf("Wrong Answer\n");}}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Variadic functions in C
You are viewing a single comment's thread. Return to all comments →