c++ variable arguments
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
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- unsorted array to bst
- Category
- C++
- Title
- c++ write string
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- c++ template function
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- How to traverse in a tree iterative C++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- elseif c++
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++