c++ variable argument
C++
#include <iostream>
#include <cstdarg>
using namespace std;
double average(int num,...) {
va_list valist; // A place to store the list of arguments (valist)
double sum = 0.0;
int i;
va_start(valist, num); // Initialize valist for num number of arguments
for (i = 0; i < num; i++) { // Access all the arguments assigned to valist
sum += va_arg(valist, int);
}
va_end(valist); // Clean memory reserved for valist
return sum/num;
}
int main() {
cout << "[Average 3 numbers: 44,55,66] -> " << average(3, 44,55,66) << endl;
cout << "[Average 2 numbers: 10,11] -> " << average(2, 10,11) << endl;
cout << "[Average 1 number: 18] -> " << average(1, 18) << endl;
}
/*
NOTE: You will need to use the following 'data_types' within the function
va_list : A place to store the list of arguments (valist)
va_start : Initialize valist for num number of arguments
va_arg : Access all the arguments assigned to valist
va_end : Clean memory reserved for valist
*/
Also in C++:
- Title
- create copy constructor c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- #include
- Category
- C++
- Title
- find vector in c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- switch case sinax c++
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++