//im required to do as instructed here
//===========
http://comsc265.yolasite.com/lab-3.php ===========//
//==============my code which doesnt work=============//
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include<cstdarg>
//function prototype(s)
int avg(int,...);
int main()
{
// identifying output statements
cout << "Programmer: Naushil Mehta" << endl;
cout << "Description: This program gives you the average of floating point numbers." << endl;
cout<<"The average of 5 test scores is "<<avg(5,81,92,73,84,95)<<endl;
return 0;
}
//return average of a variable length list of integers
int avg(int n, ...)//"n" is the number of numbers in the list;"..."is the list
{
va_list list; // assign the name "list" to the variable length list of integers
va_start(list,n);//tell c++ that the list begins after the argument "n"
int num;//store the numbers from the list in "num" as they are "read"
//create the total of "n" numbers in the list
int total=0;//track the total of the numbers in the list
for(int i=0;i<n;i++)
{
num=va_arg(list,int);//set num equal to the next number in the list,as an int
total=total+num;//increment the total
}
va_end(list);//close the list--REQUIRED
// compute and return the average
double davg(double m, ...);
int avgx (int,...);
return total/n;
}
//==============================================================================================//
//function prototypes
//return average of a variable length list of integers
double davg(double m, ...)//"m" is the number of numbers in the list;"..."is the list
{
va_list list; // assign the name "list" to the variable length list of integers
va_start(list,m);//tell c++ that the list begins after the argument "n"
double num;//store the numbers from the list in "num" as they are "read"
//create the total of "m" numbers in the list
double total=0;//track the total of the numbers in the list
for(double i=0;i<m;i++)
{
num=va_arg(list,double);//set num equal to the next number in the list,as a double
total=total+num;//increment the total
}
va_end(list);//close the list--REQUIRED
// compute and return the average
return total/m;
}
//==============================================================================================//
//function prototype(s)
//==============================================================================================//
int avgx (int,...)
{
int a, aMax, aMin;
a = avgx(aMax, aMin, 5, 23, 42, 44, 70, 98);
cout<<"The average of 5 scores is "<<avgx<<endl;
return 0;
}//
//returns average and passes max and min back through argument list
int avgx(int& mx,int& mn,int n,...)//n is the number of numbers in the list
{
va_list list;//assign the name "list" to the variable length list of integers
va_start(list,n);//tell c++ that the list begins after the argument "n"
int num;//store the numbers from the list in "num" as they are "read"
//create the total of "n" numbers in the list
int total=0;//track the total of the numbers in the list
for (int i=0;i<n; i++)
{
num=va_arg(list,int);//set num equal to the next number in the list,as an int
if((i==0) ||(mn>num))//update min value
mn=num;
if((i=0)||(mx<num))//update max value
mx= num;
total=total+num;//increment the toal
}
va_end(list);//close the list--required
//compute and return the average
return total/n;
}