-1

I came across a Question i.e passing Variable number of arguments to a function in C. The Question is in this way:

Write a function that takes a variable number of arguments representing student marks in english and returns the number of students who scored > 90 marks. E.g.: variableArguments(3, 20, 90, 98) returns 1. One value (98) is greater than 90.

First argument is number (arg_count) of student marks followed by "arg_count" number of arguments. E.g.: If first argument to function is 5, total number of arguments to function will be 6 (1 + 5).

#include<stdarg.h>
.....
.....
int variableArguments(int arg_count, ...){
 //TODO 
  .....
  .....
  return 0;
}

eg:int result=variableArguments(4,87,90,98,67);

 number of arguments =4
arguments=(87,90,98,67)      

result value should be '1' because number of arguments >90 is one

In the prototype of the function first argument is arg_count remaining is arguments are represented as ... What could be the meaning of that ... ?

Here in this function it is given that arg_count gives the number of arguments passed that we need to pass to the function while calling. If such arguments are passed to the function, How to access the list of arguments in the definition of function ?

Madhusudan chowdary
  • 543
  • 1
  • 12
  • 20

3 Answers3

3
int ff(int num, ...)
{
  va_list arguments;
  int sum = 0;
  va_start(arguments, num);
  for (int x = 0; x < num; x++)
  {
    sum += va_arg(arguments, int);
  }
  va_end(arguments);

return sum;
}

//call 
printf("%d\n", ff(3, 1, 2, 3));

va_list contains list of arguments in ..., in loop you get access one by one va_arg(arguments, int);

Ivan Sheihets
  • 802
  • 8
  • 17
  • 2
    Please avoid giving code only answers, explain solution instead. Also `cout` is C++ construct and this is C question. – user694733 Jun 21 '17 at 08:57
1

you can write like this, this programme will return max values from function which accepts variable no. of arguments:

#include <stdarg.h>
#include <stdio.h>

int max(int arg_count, ...)
{
  int i;
  int max, a;

  va_list ap;
  va_start(ap, arg_count);

  max = va_arg(ap, int);

  for(i = 2; i <= arg_count; i++) {
    if((a = va_arg(ap, int)) > max)
      max = a;
  }

  va_end(ap);
  return max;
}

int main()
{
   int count = 5;


   printf("Max value is %d", max(count, 12, 67, 6, 7, 100));
   return 0;
}
suraj
  • 403
  • 4
  • 15
-3
void f(int count_arg, int *arg)
{
   for (int i = 0; i < count_arg; i++)
       cout << arg[i] << endl;
}

//call
int a[] = { 1,2,3,4,5 };
f(5, a);
Ivan Sheihets
  • 802
  • 8
  • 17
  • That's not a *variable argument* function. It's a function which takes *exactly* two arguments. – Some programmer dude Jun 21 '17 at 08:44
  • The question specifically asks for var args! And is also tagged C not C++. – Ajay Brahmakshatriya Jun 21 '17 at 08:44
  • Plus, this is C++ not C – Gam Jun 21 '17 at 08:44
  • You can can use #include and va_list arguments; – Ivan Sheihets Jun 21 '17 at 08:48
  • Thanks for your answer @IvanSheigets I know this kind of passing arrays in C and in C++. But here in the question I want to know the meaning of '...' and how to access if directly numbers are passed not an array. – Madhusudan chowdary Jun 21 '17 at 08:49
  • 2
    Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jun 21 '17 at 10:58