GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2005, 12:54
sssalsa sssalsa is offline
New Member
 
Join Date: Jan 2005
Posts: 4
sssalsa is an unknown quantity at this point

changing the value of x in c++


iv written a program 2 calculate the value of a function f(x), and then written one so that the computer calculates it a different way for negative x.
how do i get c++ 2 do the following:
if the user inputs a negative number, c++ ignores the negative sign and reads the value of x and puts this into the program iv used to calculate f(x).
hope you understant what i mean.
this is my program so far. i just need 2 change the second part but i dont kno how:

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>

int main()
{
	double x;
	cout << "Enter x: " << endl;
	cin >> x;

	if (x>=0) {
		cout << "f(x)= "
			<< x + (sqrt((x*x)+1))
			<<endl;
	}
	else {
		cout << "f(x)= "
			<< 1/((sqrt((x*x)+1))+x)
			<< endl;
		return 0;
	}
}
Last edited by LuciWiz : 02-Feb-2005 at 14:27. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 02-Feb-2005, 13:08
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
C++ doesn't require '.h' in the #include statements, so you can lose those extensions if you want. You'd only use it if you wanted to force the use of the original C headers.

One way to do it is use the absolute value function to get the absolute value of 'x', then continue on w/ the 'else' statement. Alternatively, you could use parenthesis to force the program to multiply the negative values, thereby creating a positive.

Example: Assuming 'x' is a negative number, ((x)*(x)) should give you a positive x^2 value. I don't remember offhand, but I believe C++ also has a 'power' function, allowing you to not have to manually multiply x*x.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 02-Feb-2005, 14:36
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by crystalattice
I don't remember offhand, but I believe C++ also has a 'power' function, allowing you to not have to manually multiply x*x.

Yes, it's called pow and it is located in the math header.

Regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 02-Feb-2005, 15:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by sssalsa
how do i get c++ 2 do the following:
if the user inputs a negative number, c++ ignores the negative sign and reads the value of x and puts this into the program.

For variables of type double, the function is fabs. Try the following with negative values for x and for positive values of x:

CPP / C++ / C Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  double x;
  double y;

  cout << "Enter x: " << endl;
  cin >> x;
  y = fabs(x);
  cout << "x  = " << x << ", y = " << y << endl;
  return 0;
}

Regards,

Dave
  #5  
Old 02-Feb-2005, 15:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
... you could use parenthesis to force the program to multiply the negative values, thereby creating a positive.

Example: Assuming 'x' is a negative number, ((x)*(x)) should give you a positive x^2 value. I don't remember offhand, but I believe C++ also has a 'power' function, allowing you to not have to manually multiply x*x.


Parentheses do not change the results unless they are used to override the built-in operator precedence:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

int main()
{
  double x, y, z;

  x = -12.34;
  y = x * x + 1;
  z = (x)*(x) + 1; /* same as y, I'll bet */

  printf("x = %f, y = %f, z = %f\n", x, y, z);

  y = sqrt(x*x + 1);
  z = sqrt((x)*(x) + 1); /* same as y, I'll bet */

  printf("x = %f, y = %f,  z = %f\n", x, y, z);

  return 0;
}

And yes, you could use pow(x, 2) in place of x*x, but why would you want to do that?

Regards,

Dave
  #6  
Old 02-Feb-2005, 18:28
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Ha ha, you got me. I remember now why I suggested the parenthesis idea: I was thinking of my calculator that multiplies just the numbers unless you enclose the negative sign and number in parenthesis. If you didn't, the answer would always be negative. I forgot that computers don't have that issue. Sorry

I suggested the power function to make the program more portable. Just thought it might be easier. <shrug>
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #7  
Old 02-Feb-2005, 20:57
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
for integer data type,use abs

CPP / C++ / C Code:
int x=-11;
z=abs(x);

for floating number,use fabs

CPP / C++ / C Code:

double x=-11;
float y=-12;

z=fabs(x);
z=fabs(y);
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #8  
Old 05-Feb-2005, 15:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
I suggested the power function to make the program more portable. Just thought it might be easier. <shrug>
Nothing is more portable than not using a pre-defined function. x*x is much more portable because it will work in any general computer language. ;-)
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #9  
Old 05-Feb-2005, 16:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by WaltP
Nothing is more portable than not using a pre-defined function. x*x is much more portable because it will work in any general computer language. ;-)

I agree with your sentiment (I think), but my head is spinning:

I'll buy this:
"Anything is more portable than not using..."

I'll buy this:
"Nothing is more portable than using..."


But:
"Nothing is more portable than not using..."

Oh, well...

By the way, the function double pow(double, double) is in the C standard library (and, therefore the C++ standard library), so its use is portable between different platforms and/or different compilers for C and/or C++ (the only portability I am concerned with on this forum).

I have always been a little leery of using this with negative values for the first argument, since there is a domain error if the first argument is negative and the second argument does not have an integer value. Since both arguments are floating point numbers (doubles), I always have roundoff error in the back of my mind. (Try it with something like pow(-2.0. 2.000001), for example).

Since there is no roundoff error in the second argument of the expression pow(x, 2), it's technically OK in this application.

My question of, "why would anyone want to do this instead of just using x*x," was based on my defensive approach to floating point numbers where roundoff error could be disastrous, and also to laziness (fewer keystrokes in "x*x"). Why do something (using pow() for squaring a number) that will probably be OK most of the time and requires more typing?

Regards,

Dave
  #10  
Old 11-Feb-2005, 14:48
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Well, I was thinking that if the program needed to be modified for use w/ powers beyond 2 or 3, then maybe using the power function would be easier to use. That way you don't have to start counting and keeping track of "x's" and running into logic errors.

I suppose for a quick and dirty program that probably won't be used for anything besides practice it doesn't matter. But isn't it wise to think ahead for problems like that? Don't want another 64k barrier or Y2K fiasco. ;-)
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing monitors on a Linux machine JdS Computer Software Forum - Linux 10 23-Dec-2004 10:49
Problems with changing display resolution vsseym Computer Software Forum - Windows 2 27-Jul-2004 11:01
Pointer values changing unexpectedly spudtheimpaler C Programming Language 11 04-Mar-2004 17:37
Changing a line in a file Allowee Computer Software Forum - Linux 1 10-Jan-2004 04:09
Changing font while using getstr BobbyDouglas MySQL / PHP Forum 1 03-Dec-2003 23:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.