473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

function problem

Greetings,

I’m trying to write a function to compute and return the following value for integer x.


f(x) =
x + 2 if x < 0
0 if x = 0
1 + 2 + ... + x if x > 0


Can anyone help me set it up?
Oct 22 '06 #1
3 1045
arne
315 Expert 100+
Greetings,

I’m trying to write a function to compute and return the following value for integer x.


f(x) =
x + 2 if x < 0
0 if x = 0
1 + 2 + ... + x if x > 0


Can anyone help me set it up?
Sure. Why don't you post what you have so far? This could be used as a starting point, and it would be much more instructive for you than if someone else does your assignment ;-)
Oct 22 '06 #2
The following implementation should solve your problem

#include <cstdlib>
#include <iostream>

using namespace std;

double f(double x);

int main(int argc, char *argv[])
{
double z;
cout<<"Enter a value for z\n";
cin>>z;
cout<<f(z)<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
double f(double x)
{
if(x>0){
double sum=0;
for(int i=1;i<=(int)(x);i++){
sum+=i;
}
return sum;
}
else if(x==0){
return 0;
}
else{
return x+2;
}
}

If you are looking for an excellent( and cheap) book on numerical computing I highly reccomend
"Introducing C++ for Scientist's, Engineers and Mathematicians"
by Derek Capper

It is really an excellent book. I use it all the time. It costs roughly €30 on amazon.co.uk. Well worth it.

emaghero
Oct 23 '06 #3
Thanxs for the aid and the tip. i'm ordering a copy from amazon this week.

Just one more question. how would I be able to make the program loop so that it allows me to repeatedly test the function during the output instead of running it over and over each time for different values of Z?

regards
Oct 24 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
4
by: andy | last post by:
i am coding a dll which contains a function take a const wchar_t* as parameter, call it form a exe, the wchar_t is passed to dll function, but the function in dll will then call another function...
3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
2
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....
18
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. ...
4
by: Andy_Khosravi | last post by:
Hello, I'm having a problem with the MID function within Access 97. I have been trying to build a function to check to make sure that a field on a form does not have any spaces or dashes. This...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.