473,830 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function that calculates the arithmetic mean using vectors?

I have a function that calculates the mean of the some numbers; I need
it to accept any number of parameters (one needs to be the number of
the other parameters) Can you help me out here? Here's the function:

const long double& mean(long long x)
{
vector<int> v(x);
for(int i= 1; i <=x; ++i)
{
v.push_back(i);
}
const long double ret=accumulate( v.begin(), v.end(), 0.0) / v.size();
return ret;
}

I want it to calculate the vector size at runtime. If you could help me
out, that'd be great. Thanks!!!

Sep 14 '05
32 5900
OK, how do I test if my variables, num and temp, are characters, like a
or b? I need to make my code idiot proof.

Sep 15 '05 #11
Protoman wrote:
OK, how do I test if my variables, num and temp, are characters, like a
or b? I need to make my code idiot proof.


This question gets asked a lot. If you search the archive you'll get
lots of answers.

The only perfectly idiot proof method is to read a line into string,
check the string is in the correct form, and then convert to a number.

For a very nearly as good method see the 'resurrecting cin' thread by
float_dublin (c.l.c++ only). The full method doesn't come out until the
end so be prepared to read some less than complete posts (including one
by me).

john
Sep 15 '05 #12
John Harrison wrote:
Protoman wrote:
OK, how do I test if my variables, num and temp, are characters, like a
or b? I need to make my code idiot proof.


This question gets asked a lot. If you search the archive you'll get
lots of answers.

The only perfectly idiot proof method is to read a line into string,
check the string is in the correct form, and then convert to a number.

For a very nearly as good method see the 'resurrecting cin' thread by
float_dublin (c.l.c++ only). The full method doesn't come out until the
end so be prepared to read some less than complete posts (including one
by me).

john


For a bonus point, see if you can work out what the incorrect input is
that the 'resurrecting cin' method will accept, but the perfect method
will reject.

john
Sep 15 '05 #13
OK, here's the idiot proof input funct:

template<class T>
void input(vector<T> & v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
abort();
else if(!cin)
abort();
else
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
abort();
else
v.push_back(tem p);
}
}

Any other suggestions for me?

Sep 15 '05 #14
Protoman wrote:
OK, here's the idiot proof input funct:

template<class T>
void input(vector<T> & v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
abort();
else if(!cin)
abort();
else
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
abort();
else
v.push_back(tem p);
}
}

Any other suggestions for me?


It works, but the idiots aren't going to be very happy when the program
aborts. I would have suggested some sort of error recovery, 'Hey idiot!
Please enter the number again', that kind of thing.

john
Sep 15 '05 #15

Protoman wrote in message
<11************ **********@g14g 2000cwa.googleg roups.com>...
Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?


If you can read this NG, you can read the docs for stdlib. <G>

You are thinking of 'exit()', BUT, don't use that for normal program
termination. It's intended as an 'emergency exit' (it's possible to shoot
yourself in the foot - or worse!).
All you need to do in your 'for(;;)' loop is execute 'break;', like you are
doing, and the 'main()' will finish, terminating your program normally.
You might find 'while(conditio n){}' a little cleaner/clearer than 'for(;;)'
(like Mr. Huber showed you.).

For your error checking question; look up 'isalpha' and 'isdigit' for an
idea.

What book are you using?

You might find this site interesting.
www.BruceEckel.com -> Books -> "Thinking in C++, vol. 1"
(since it only costs $(a download))
Two heads are better than one, and so are two books!

alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/

Hope that helps. Good luck.
--
Bob R
POVrookie
--
MinGW (GNU compiler): http://www.mingw.org/
Dev-C++ IDE: http://www.bloodshed.net/
POVray: http://www.povray.org/
Sep 15 '05 #16
OK here's the input function that doesn't abort on bad data:

template<class T>
void input(vector<T> & v)
{
start:
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
{
cout << "Try again, with a number this time ";
goto start;
}
else if(!cin)
{
cout << "Try again, with a number this time ";
goto start;
}
else
for(int i=0;i<num;++i)
{
start2:
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
{
cout << "Try again, with a number this time ";
goto start2;
}
else
v.push_back(tem p);
}
}

But it keeps displaying the same message over and over again; what's
wrong? Thanks for the help!!! BTW, I use Learn C++ in 21 Days and
Standard C++ Bible. And my teacher told me there's a book that tells
you everything you can and can't do in C++. Could you tell me the name
of that book please? Thanks!!!

Sep 16 '05 #17
Protoman wrote:
OK here's the input function that doesn't abort on bad data:

template<class T>
void input(vector<T> & v)
{
start:
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
if(num==0)
{
cout << "Try again, with a number this time ";
goto start;
}
else if(!cin)
{
cout << "Try again, with a number this time ";
goto start;
}
else
for(int i=0;i<num;++i)
{
start2:
cout << "Enter a number: ";
T temp;
cin >> temp;
if(!cin)
{
cout << "Try again, with a number this time ";
goto start2;
}
else
v.push_back(tem p);
}
}

But it keeps displaying the same message over and over again; what's
wrong? Thanks for the help!!! BTW, I use Learn C++ in 21 Days and
Standard C++ Bible. And my teacher told me there's a book that tells
you everything you can and can't do in C++. Could you tell me the name
of that book please? Thanks!!!


There is nothing that cannot be done in C++, it is a general purpose
language. Of course there are somethings that are easy to do and some
that are difficult, compared to other languages.

As already advised read the thread 'resurrecting cin' on c.l.c++. It is
about exactly this topic. If you what to research this issue see if your
books say something about iostreams and error state.

Try and develop the skills to research answers to your own questions, at
the moment you want to be spoon fed.

john
Sep 16 '05 #18
No, that book I was talking about, it tells you what the syntax
permits, not the language itself. I'm just going to print an error
message, then abort. Saves me the trouble. And what would "COBOL style
C++" look like?

Sep 16 '05 #19
Protoman wrote:
No, that book I was talking about, it tells you what the syntax
permits, not the language itself. I'm just going to print an error
message, then abort. Saves me the trouble. And what would "COBOL style
C++" look like?


I have no idea.

john
Sep 16 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4386
by: neo88 | last post by:
hi guys Can anyone please tell me what is wrong with this function: inline int strength(void) { int a; // tests to see what strength value the agent just attacked\defended with for (strength; a < 5; a++) int strength = int power + int effect; // array of strength
2
2339
by: Hennie de Nooijer | last post by:
Because of an error in google or underlying site i can reply on my own issue. Therefore i copied the former entered message in this message. -------------------------------------REPY---------------------------------- Hi Maybe i wasn't clear. I want to dynamically check whether what the lowest date and the highest date is in the calendar table. The presented solutions has fixed dates and i don't want that. If i could store a global...
4
1336
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I want it to calculate the vector size at runtime. Can you help me out here? Here's the function: const long double& mean(long long x) { vector<int> v(x); for(int i= 1; i <=x; ++i) { v.push_back(i);
18
5930
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. More holidays can be added easily. Also, there is a boolean flag for including or excluding Saturdays or Sundays as part of the work week. The only thing I know of that is missing is that some holidays are observed on a Friday or a Monday if they...
38
2389
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
21
4207
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if possible. The thing I am trying to do is to convert a double value to a string with 8 elements. 8 is fixed because of the files I work with. I will change this 8 character string with the one(8 character string) already in the file and so on. But I...
17
8105
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
19
2452
by: Gerry Ford | last post by:
I've been pecking away at writing a program that will calculate the inner product of two double-width four-vectors. Larry thinks I'm well started with the following source to populate a vector: #include <vector> #include <algorithm> #include <iterator> #include <iostream> #include <math.h>
7
1172
by: Kurda Yon | last post by:
Hi, I have a class called "vector". And I would like to define a function "dot" which would return a dot product of any two "vectors". I want to call this function as follow: dot(x,y). Well, I can define a functions "dot" outside the class and it works exactly as I want. However, the problem is that this function is not associated with the class (like methods a method of the class).
0
9780
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9641
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10769
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10520
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10196
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6940
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.