HackerRank C- Variadic functions in C




In this problem, you will implement three variadic functions named sum(), min() and max() to calculate sums, minima, maxima of a variable number of arguments. The first argument passed to the variadic function is the count of the number of arguments, which is followed by the arguments themselves.



  1. int sum (int count,...) {
  2. int sum=0;
  3. va_list values;
  4. va_start(values,count);
  5. for(int i=0;i<count;i++){
  6. sum+=va_arg(values,int);
  7. }
  8. va_end(values);
  9. return sum;
  10. }
  11. int min(int count,...) {
  12. int min=MAX_ELEMENT,test;
  13. va_list values;
  14. va_start(values,count);
  15. for(int i=0;i<count;i++){
  16. test=va_arg(values,int);
  17. if(min>test){
  18. min=test;
  19. }
  20. }
  21. va_end(values);
  22. return min;
  23. }
  24. int max(int count,...) {
  25. int max=MIN_ELEMENT,test;
  26. va_list values;
  27. va_start(values,count);
  28. for(int i=0;i<count;i++){
  29. test=va_arg(values,int);
  30. if(max<test){
  31. max=test;
  32. }
  33. }
  34. va_end(values);
  35. return max;
  36. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube