Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14971
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: what am i doing wrong here?help |
| Date | 2012-03-18 12:25 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <9sm2idFbr3U1@mid.uni-berlin.de> (permalink) |
| References | <68aa0d2b-6078-495d-abf3-c7dd02148eed@o3g2000pbt.googlegroups.com> |
naushil mehta <naushil9@gmail.com> wrote:
> http://comsc265.yolasite.com/lab-3.php
> here is code
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:option' with VS, where 'option' is a number
between 0 and 4) and it probably will report this as a po-
tential mistake.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
what am i doing wrong here?help naushil mehta <naushil9@gmail.com> - 2012-03-17 20:18 -0700
Re: what am i doing wrong here?help jt@toerring.de (Jens Thoms Toerring) - 2012-03-18 12:25 +0000
Re: what am i doing wrong here?help jt@toerring.de (Jens Thoms Toerring) - 2012-03-18 21:04 +0000
csiph-web