Function to make a variable equal to other variables

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Function to make a variable equal to other variables

 
0
  #1
Jun 20th, 2008
very simple question: what is the most easiest way to do the following

I have two variables

a=1;
b=2;

how do I make it that

c= a,b

so that c=12

I dont want to write
  1. c=12;
because a and b change according to user input.

Thanks, this should be solved fast I dont really get what I should "search" for otherwise I would search instead of asking. (also the title is probably pretty bad but not sure what to call this )


Thanks
Last edited by salman213; Jun 20th, 2008 at 7:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function to make a variable equal to other variables

 
0
  #2
Jun 20th, 2008
int c = (a * 10) + b;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Function to make a variable equal to other variables

 
0
  #3
Jun 20th, 2008
Hmm I guess that is the way to do it if you want to go mathematically .. I should have thought of that... so dumb..lol

Thanks Ancient


I will use that but just to check if anyone knows of another way to do it please do post.
Last edited by salman213; Jun 20th, 2008 at 8:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function to make a variable equal to other variables

 
0
  #4
Jun 20th, 2008
What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way:
int c = (a << 3) + (a << 1) + b;
Last edited by Ancient Dragon; Jun 20th, 2008 at 8:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Function to make a variable equal to other variables

 
0
  #5
Jun 20th, 2008
Originally Posted by Ancient Dragon View Post
What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way:
int c = (a << 3) + (a << 1) + b;
Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.
Last edited by Alex Edwards; Jun 20th, 2008 at 8:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,364
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Function to make a variable equal to other variables

 
0
  #6
Jun 20th, 2008
Originally Posted by Alex Edwards View Post
Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.
Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Function to make a variable equal to other variables

 
0
  #7
Jun 20th, 2008
which way is "obfuscatory?"

Is there a specific command that has not been mentioned yet which is now available that will do the same thing?


Not that I have any problem with the way described
Last edited by salman213; Jun 20th, 2008 at 10:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Function to make a variable equal to other variables

 
1
  #8
Jun 20th, 2008
Originally Posted by salman213 View Post
which way is "obfuscatory?"
Well, you are given two ways of doing the same thing:
  1. int c = (a * 10) + b;
  2. int c = (a << 3) + (a << 1) + b;

One of them is straight-forward, one is not. The one that is not is "obfuscatory" (i.e. more complicated and harder to understand for no reason other than to add confusion).

Originally Posted by salman213 View Post
Is there a specific command that has not been mentioned yet which is now available that will do the same thing?
You're missing the point. When AD posted this:

  1. int c = (a << 3) + (a << 1) + b;

his point was along the lines of "I've given you a perfectly good solution, yet you ask for another one with no explanation of why the first solution doesn't meet your needs." Then he gave you a solution that does the exact same thing as the first solution, but in an overly-complicated way that no one would ever use instead of the first solution. Why don't you like this?

  1. int c = (a * 10) + b;

I can't think of a command that will do this any better.

Originally Posted by salman213 View Post
Not that I have any problem with the way described
Why are you asking for a different way then? I'm sure we could come up with even more confusing mathematical formulas that do the exact same thing, but that's probably not what you are looking for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Function to make a variable equal to other variables

 
1
  #9
Jun 21st, 2008
Originally Posted by Dave Sinkula View Post
Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.
I laughed so hard when I read this...

winner
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC