what am i doing wrong here?help

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;
}
 
J

Jens Thoms Toerring

naushil mehta said:

It would be useful if you would describe what you actually need help
with. And please, please, use reasonable amounts of white space and
indentation in your code, it's cheap and helps enormously in making
things easier to read. And then stop commenting each and every litt-
le and obvious detail.
#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;
}

This should work, doesn't it? One might argue if returning
an int for the average is a good idea since the way you
wrote that it will return the integer value below the actual
average (unless the sum can be divided exactly by the number
of values the average is calculated from) but that's up to
you.
//===================//
{
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;

I would guess that you intended to call davg() here.
}
double davg(double m, ...)//"m" is the number of numbers in the

It would make much more sense to use an integer for the count
here. Floating point values can behave in unexpected ways since
they might have values slightly different from whhat you expect.
Probably not a problem here but you better get in the habit of
avoiding them as counters whenever pssible.
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++)

Don't use floating point numbers for such things if you can
avoid it!
{
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,...)
{

That's a function declaration (but misses a colon at the
end), a definition would need a named variable. The com-
piler won't accept this.
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;
}//

Now things get rather confusing: you have two functions with
the same name, 'avgx', but which take different kinds of
arguments and do completely different things. Moreover,
you call the function with the references as the first two
arguments but don't use the results at all and then call the
other avgx() function from within itself, resulting in an
infinite recursion. I also don't see any declaration for
the argx() function with the two reference arguments, so the
compiler won't compile this also for that reason. Please try
to post complete, compilable code if possible in the future.

I have added a bit of white space and removed all the
rather superfluous comments from the next function:
//returns average and passes max and min back through argument list

int avgx( int & mx, int & mn, int n, ... )
{
va_list list;
va_start( list, n );

int num;
int total = 0;

for ( int i = 0; i < n; i++ )
{
num = va_arg( list, int );

if ( i == 0 || mn > num )
mn = num;
if ( i = 0 || mx < num )
mx = num;

total = total + num;
}

va_end( list );
return total / n;
}

Take a look if you now can spot what's wrong here more
easily...


If you haven't yet found it here's the offending line:
if ( i = 0 || mx < num )

You don't compare 'i' to 0 here, you assign 0 to it! The
result is that you will never get out of this loop. Try
to find out how to make ypur compiler complain about pos-
sible problems (i.e. use a higher warning level, IIRC you
can use '/warn:eek:ption' with VS, where 'option' is a number
between 0 and 4) and it probably will report this as a po-
tential mistake.
Regards, Jens
 
J

Jens Thoms Toerring

That's a function declaration (but misses a colon at the
end), a definition would need a named variable. The com-
piler won't accept this.

Oops, I was thinking C when I wrote that (have been writing C
the last few days) - in C++ you can have unnamed arguments if
you're not goind to use them within the function, so it's a
valid start for a function definition (but one that makes no
sense for a function with a variable number of arguments since
now you can't get at them because there's nothing you could
pass to va_start() as the second argument).

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top