Question

Write a function with a variable number of arguments, that takes any number of integers as...

Write a function with a variable number of arguments, that takes any number of integers as arguments and computes the average. Additionally write a main that calls this function at least twice on different numbers of arguments.

Can this be done in command line and c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In this C++ program,

- We have defined a function named Average which accepts a variable number of arguments and then it calculates the average for those numbers and prints it to the terminal.

- In main, we are checking the functionality of the above-defined functions by calling the function with a different number of arguments.

(I believe that I made the code simple and understandable, If you still have any query, Feel free to drop me a comment)

Program:

#include <iostream>

#include <cstdarg>

using namespace std;

/* Defined a function named Average which takes variable number of arguments and

* calculates the average of those numbers and print it onto the console */

void Average(int size, ...)

{

//Defined a va_list type variable

va_list numberslist;

//Initialize all the elements into the numberlist of the type va_list

va_start(numberslist,size);

float average=0; //Defined for storing the average of all the values

//Iterating through all the elements and calculating the sum first

for(int i=0;i<size;i++)

average=average+va_arg(numberslist,int);

//Calculating the average by dividing the sum of elements and number of elements

average=average/size;

//Printing the Average of all the elements

cout<<"The Average of the numbers is "<<average<<endl;

}

int main()

{

//NOTE: You cannot directly pass the values,You need to specify the count as well

//When using the concept of variable length argument

/*Calling the Function Average() with mentioned the count of integers(Mandatory) as

* as the first parameter and then passing that many number of integer value*/

Average(4,1,2,3,4);

Average(10,1,2,3,4,5,6,7,8,9,10);

Average(3,5,10,15);

Average(2,10,20);

}
main.cpp saved 1 #include <iostream> 2 #include <cstdarg> 3 using namespace std; 4 5 6 7 /* Defined a function named Average

Output:

> clang++-7 pthread -std=c++17 -o main main.cpp } ./main The Average of the numbers is 2.5 The Average of the numbers is 5.5

Add a comment
Know the answer?
Add Answer to:
Write a function with a variable number of arguments, that takes any number of integers as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT