Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this program right?

Reply
Thread Tools

Is this program right?

 
 
Greg
Guest
Posts: n/a
 
      09-11-2005
Protoman wrote:
> And, John, here's my program updated to your advice:
>
> #include <iostream>
> #include <cstdlib>
> #include <cmath>
> using namespace std;
>
> ...
> template <class T>
> T harmonic_mean(T& num,T& num2,T& num3,T& num4,T& num5)
> {
> const static T ret=(5/(1/num+1/num2+1/num3+1/num4+1/num5));
> return ret;
> }
>
> int main()
> {
> for(;
> {
> long double num;
> long double num2;
> long double num3;
> long double num4;
> long double num5;
> cout << "Enter five numbers: " << endl;
> cin >> num >> num2 >> num3 >> num4 >> num5;
> cout << "Here's the average: " << fixed << mean<long
> double>(num,num2,num3,num4,num5) << endl;
> cout << "Here's the quadratic mean: " << fixed << harmonic_mean<long
> double>(num,num2,num3,num4,num5) << endl;
> cout << "Here's the harmonic mean: " << fixed << quadratic_mean<long
> double>(num,num2,num3,num4,num5) << endl;
> cout << "Here's the standard deviation: " << fixed << std_dev<long
> double>(mean<long double>(num,num2,num3,num4,num5)
> ,num,num2,num3,num4,num5) << endl;
> }
> system ("PAUSE");
> return 0;
> }
>
> Is there anyway I can make it clearer und more precise und concise, not
> to mention more efficient?


The program should validate its inputs. For example, if the user enters
a 0 as one of the numbers that the program asks for, what is likely to
happen when the program calculates their harmonic mean?

Also, I don't see any benefit from using template functions. On the
contrary, by having to instantiate these routines for a float, when the
program already has instantiated them for a double, provides no added
benefit and simply bloats the code. A set of non-template functions
accepting doubles would seem to make a lot more sense.

Greg

 
Reply With Quote
 
 
 
 
Protoman
Guest
Posts: n/a
 
      09-11-2005
When I say "generalize", I mean, for all the different types of means;
I'm not very good at STL.

 
Reply With Quote
 
 
 
 
Protoman
Guest
Posts: n/a
 
      09-11-2005
OK, I tried it, but it doesn't compile; here are the errors:

C:\Dev-Cpp\average3.cpp In function `T Mean(const std::vector<T,
std::allocator<_CharT> >&)': expected `;' before "i"
11 C:\Dev-Cpp\average3.cpp `i' undeclared (first use this function)
C:\Dev-Cpp\average3.cpp In function `T Mean(const std::vector<T,
std::allocator<_CharT> >&) [with T = float]': instantiated from here
11 C:\Dev-Cpp\average3.cpp dependent-name `
std::vector<T,std::allocator<_CharT> >::const_iterator' is parsed as a
non-type, but instantiation yields a type
11 C:\Dev-Cpp\average3.cpp say `typename
std::vector<T,std::allocator<_CharT> >::const_iterator' if a type is
meant

I wonder what's wrong? Hope this helps!

 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      09-11-2005
"Protoman" <> wrote in message
news: oups.com...
> Hey, is this program right? It calculates the average, harmonic mean,
> quadratic mean, and the standard deviation for two numbers. Can you
> proofread it for me?
> Here it is:
>
> #include <iostream>
> #include <cstdlib>
> #include <cmath>
> using namespace std;
>
> template <class T>
> class avg
> {
> public:
> T operator()(const T& num, const T& num2)const
> {
> T ret=((num+num2)/2);
> return ret;
> }
> T operator()(const T& num, const T& num2, int x)const
> {
> T ret=(sqrt(pow(num,2)+pow(num,2)));
> return ret;
> }
> T operator()(const T& num, const T& num2,int x,int y)const
> {
> T ret=(2/(1/num+1/num2));
> return ret;
> }
> operator T()const{return static_cast<T>(value);}
> private:
> T value;
> };
>
> template <class T>
> class Stat
> {
> public:
> T operator()(T avg,const T& num,const T& num2)const
> {
> T ret=num-avg;
> T ret2=num2-avg;
> T ret3=(pow(ret,2)+pow(ret2,2)/2);
> T ret4=sqrt(ret3);
> return ret4;
> }
> operator T()const{return static_cast<T>(value);}
> private:
> T value;
> };
>
> int main()
> {
> for(;
> {
> long double num;
> long double num2;
> avg<long double> Avg;
> Stat<long double> StdDev;
> cout << "Enter two numbers: " << endl;
> cin >> num >> num2;
> cout << "Here's the average: " << fixed << Avg(num,num2) << endl;
> cout << "Here's the quadratic mean: " << fixed << Avg(num,num2,0) <<
> endl;
> cout << "Here's the harmonic mean: " << fixed << Avg(num,num2,0,0) <<
> endl;
> cout << "Here's the standard deviation: " << fixed <<
> StdDev(Avg(num,num2),num,num2)
> << endl;
> }
> system ("PAUSE");
> return 0;
> }
>
> Also, is there anyway I can make it so StdDev can use Avg's arguments
> so I don't need StdDev to have copies of Avg's arguments? And is there
> anyway I can make Avg and StdDev have variable numbers of paramaters; I
> don't want to recompile everytime I want to do more (or less) numbers?
> Thanks a lot.
>


You can use classes to make your statistic work with any number of samples.
For instance (unchecked):

class sample_mean
{
private:
unsigned m_count; // number of samples
double m_sum_x1; // sum of samples
public:
sample_mean() : m_count(0), m_sum_x1(0.0) {}
unsigned n() const {return m_count;}
double xbar() const // no check for 0 samples
{
return m_sum_x1 / m_count;
}
template <typename T>
sample_mean &operator () (T x)
{
++m_count;
m_sum_x1 += static_cast<double>(x);
return *this;
}
void clear()
{
m_count = 0;
m_sum_x1 = 0.0;
}
};

You use it like this:

sample_mean m;
m(1.2)(3.6)(-0.4);
std::cout << m.xbar() << '\n';

--
Cycho{HHR}
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      09-11-2005
I want to do it w/ functions and STL.

 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      09-12-2005
I'm always getting an error on the function's for loop; keeps saying i
is undeclared.

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-12-2005
Protoman wrote:
> I want to do it w/ functions and STL.


I'd suggest getting a good book on STL, then. Stroustrup's _C++PL_
gives an overview of the STL, and _The C++ Standard Library : A
Tutorial and Reference_ by Nicolai M. Josuttis goes into greater depth.

Cheers! --M

 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      09-12-2005
Where can I buy that; at Borders?

 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      09-12-2005
"mlimber" wrote:
>I'd suggest getting a good book on STL, then. Stroustrup's _C++PL_
>gives an overview of the STL, and _The C++ Standard Library : A
>Tutorial and Reference_ by Nicolai M. Josuttis goes into greater depth.


And then "Protoman" wrote:
>Where can I buy that; at Borders?


Somebody who claims to be "a bloody Cal State professor!!!!" (sic) and
"the asst. head of systems programming in the R&D Dept." (sic) where
"We're calculating thruster angles for a prototype ion drive engine."
needs to ask where to buy a well known book from a well known
publisher? The plot thickens ...

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      09-12-2005
Why are you doing this to me?!!!!!!!!!!!? I'm just asking if you know
if Borders *stocks* that book?!!!!? Let's go back to the original
topic. Please, I beseech you!!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
System program/ Application program ?? Parvsandhu Java 2 07-11-2005 09:08 AM
how to convert a java program to an exe program ola Java 3 02-16-2004 09:42 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57