473,802 Members | 2,026 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 #1
32 5894
Protoman wrote:
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?
You need to see an implementation of 'fprintf'. va_args and va_list
are the macros to be used. Search the web for an example of how to
use them.
[..]


V
Sep 14 '05 #2
I want to use vectors though.

Sep 14 '05 #3
Protoman wrote:
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? [..]


'va_start', 'va_end', and 'va_list' are the macros you should read
about. The web is your friend.
Sep 14 '05 #4
Protoman wrote:

I want to use vectors though.


Pass the vector to the function.
Handling user input surely is not the responsibility
of a function which calculates a mean.

long double mean( vector< int > values )
{
// now calculate the mean of the passed
// vector and return it
}

One function - one responsibility

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 14 '05 #5
Here's my rewritten program(it finally works!!!!):

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

long double mean(vector<lon g double>& v)
{
long double ret=(accumulate (v.begin(),v.en d(), 0.0)/v.size());
return ret;
}
int main()
{
for(;;)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
vector<long double> v;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(tem p);
}
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
}
system("PAUSE") ;
return 0;
}

Now, how would I rewrite to make it more efficient and modularized; I'd
like some ideas. Thanks for the help!!!

Sep 15 '05 #6
Protoman wrote:

Here's my rewritten program(it finally works!!!!):

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;

long double mean(vector<lon g double>& v)
{
long double ret=(accumulate (v.begin(),v.en d(), 0.0)/v.size());
return ret;
}

int main()
{
for(;;)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
vector<long double> v;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(tem p);
}
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
}
system("PAUSE") ;
return 0;
}

I don't see, how the outermost for loop is ever terminated.
How about: If the user enters 0 for the number of items, the program
terminates.

<sidenote>
Did you format your code in that way or did that happen during the posting.
If you formatted that code, then work on your formatting style. The above
is awefull to read. If that formatting happend during posting, get a better
newsreader. If your programs get longer, no one here will be willing to wade
through such unformatted text
</sidenote>
Now, how would I rewrite to make it more efficient and modularized; I'd
like some ideas. Thanks for the help!!!


Eg. move the whole input thing into a function of its own.
You might also want to try the following: When the program asks
for a number, enter 'a' and see what happens. I am trying to push you
into the direction of having a function which does just the task of entering
a number, but it does so with handling all possible errors and probably retrying
the input, until a valid input has been received.

Glad to see that you switched your attitude and we can talk normal with each
other. I am pretty sure that with that attitude you made it into some killfiles
and it will take some time to get out of there. You want all the help you can get,
so beeing in other peoples killfile isn't something you want to be.

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 15 '05 #7
OK, here we go, inputting's a seperate function now:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <numeric>
using namespace std;
void input(vector<lo ng double>& v)
{
cout << "Enter the number of items: " << endl;
int num;
cin >> num;
for(int i=0;i<num;++i)
{
cout << "Enter a number: ";
long double temp;
cin >> temp;
v.push_back(tem p);
}
}

long double mean(vector<lon g double>& v)
{
input(v);
long double ret=(accumulate (v.begin(),v.en d(), 0.0)/v.size());
return ret;
}

int main()
{
vector<long double> v;
for(;;)
{
long double ret=mean(v);
cout << "Here's the mean: " << fixed << ret << endl;
bool cont;
cout << "Continue?[1=yes|0=no]: " << endl;
cin >> cont;
if(cont==true)c ontinue;else break;
}
system("PAUSE") ;
return 0;
}

Next time I'll just post snips of my code. Now how to I do the error
checking? Thanks for the help!!!

Sep 15 '05 #8
Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?

Sep 15 '05 #9
Protoman wrote:
Oh, and I think there's a function in cstdlib that terminates the
program; could you tell me what that function is?


exit and abort both do that (in different ways).

john
Sep 15 '05 #10

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

Similar topics

6
4384
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
2337
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
5928
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
2385
by: maadhuu | last post by:
does it make sense to find the size of a function ??? something like sizeof(main) ??? thanking you ranjan.
21
4206
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
8102
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
2450
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
9699
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
9562
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,...
1
10285
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
10063
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
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
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();...
1
4270
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
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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.