N
naushil mehta
http://comsc265.yolasite.com/lab-3.php
here is code
#include <iostream>
//Lab 3a
//Programmer:Naushil Mehta
//Compiler used:vc ++ 2010
//Editor: Windows 7 notepad
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 6 test scores is
"<<avg(6,50,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
return total/n;
}
//===================//
{
cout<<"The average of 7 test scores is
"<<avg(7.0,59.86,50.23,81.55,55.92,67.23,84.43,95.43)<<endl;
}
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
"m"
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;
}
//=======================//
int avgx (int,...)
{
int a, aMax, aMin;
a = avgx(aMax, aMin, 8, 23,55,45,67,42,44,70,98);
cout<<"The average of 8 scores is "<<avgx(8,
23,55,45,67,42,44,70,98)<<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;
}
here is code
#include <iostream>
//Lab 3a
//Programmer:Naushil Mehta
//Compiler used:vc ++ 2010
//Editor: Windows 7 notepad
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 6 test scores is
"<<avg(6,50,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
return total/n;
}
//===================//
{
cout<<"The average of 7 test scores is
"<<avg(7.0,59.86,50.23,81.55,55.92,67.23,84.43,95.43)<<endl;
}
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
"m"
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;
}
//=======================//
int avgx (int,...)
{
int a, aMax, aMin;
a = avgx(aMax, aMin, 8, 23,55,45,67,42,44,70,98);
cout<<"The average of 8 scores is "<<avgx(8,
23,55,45,67,42,44,70,98)<<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;
}