C++ Calculations

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

Join Date: Mar 2009
Posts: 5
Reputation: kuru225 is an unknown quantity at this point 
Solved Threads: 0
kuru225 kuru225 is offline Offline
Newbie Poster

C++ Calculations

 
0
  #1
Mar 23rd, 2009
Hello All
I am having some problems with a C++ class that i am taking and after looking around on the web I am still lost.
The assignment that I am working on right now is as follows:

Two numbers are entered in from the keyboard. If the
larger number is odd, the smaller number is added to an accumulator. Then the larger number
is integer divided by two and the smaller number is doubled. Again if the larger number's
division is odd, the smaller number's doubling is added to an accumulator. lf the larger number
is even, nothing is added to the accumulator. This is repeated until the large number
sequence equals zero. The accumulator now holds the multiplication answer.
Write a program using functions, that will accomplish this task. Repeat until the user wishes to stop.
lt is not necessary to display the accumulation work as shown in the examples.
Output is to the screen and printer and should look like:
Your Name Class # Date & Time
XXXX times XXXX by conventional math = XXXXXXX
XXXX times XXXX by Zoo's method = XXXXXXX
Example 1) 75 x 23 = 1725
Larger 			Smaller 			Add to Accumulator
75			 23 				23
37 			46 				46
18 			92 				O
9 			184 				184
4 			368				 O
2 			736 				O
1 			1472 				1472
1725(TOTAL & PRODUCT)


Example 2) 
122 x 251 = 30622
Larger 		Smaller 			Add to Accumulator
251 		122 					122
125 		244 					244
62 		488 					O
31 		976 					976
15 		1952 					1952
7 		3904 					3904
3 		7808 					7808
1 		15616					 15616
30622(TOTAL & PRODUCT)

If anyone could point me in a good direction i would be most great full
Thanx
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Calculations

 
0
  #2
Mar 23rd, 2009
>>If the larger number is odd,

what happens if the larger number is even ?
Last edited by Ancient Dragon; Mar 23rd, 2009 at 3:50 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: Mar 2009
Posts: 5
Reputation: kuru225 is an unknown quantity at this point 
Solved Threads: 0
kuru225 kuru225 is offline Offline
Newbie Poster

Re: C++ Calculations

 
0
  #3
Mar 23rd, 2009
Ancient, thanks for the quick reply.
if the larger number is even then the accumulator is '0'

If you have any ideas on how the logic would go your help would be most appreciated
thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: C++ Calculations

 
0
  #4
Mar 23rd, 2009
Write it out by hand. It might start like this:
  1. enter two numbers.
  2. calculate the product of the two numbers.
  3. determine which number is larger, call it A, and call the other number B.
  4. use an accumulator, initialzed to zero
  5. as long as A is above zero
  6. if A is odd
  7. add B to accumulator
  8. divide A by 2 using integer math (means all remainders are dropped)
  9. //etc
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Calculations

 
0
  #5
Mar 23rd, 2009
and if A is initially even, don't do anything.
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: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: C++ Calculations

 
0
  #6
Mar 23rd, 2009
if A is even at anypoint, whether initially or not, then don't add B to accumulator, but you still need to divide A by 2 using integer math and double B. So if A = 44 and B = 11;
  1. A B Total Accumulated so far
  2. 44 11 0
  3. 22 22 0
  4. 11 44 44
  5. 5 88 132
  6. 2 176 132
  7. 1 352 484
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: C++ Calculations

 
0
  #7
Mar 23rd, 2009
AD: If the (originally) larger number is even you simply don't add the (originally) smaller one to the accumulator. Since "odd" just means that the one's bit is set, it's essentially binary mutiplication using shifts (division by 2) and adds.

kuru: Have you written a program before? Do you have an attempt for this one? Anything?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 5
Reputation: kuru225 is an unknown quantity at this point 
Solved Threads: 0
kuru225 kuru225 is offline Offline
Newbie Poster

Re: C++ Calculations

 
0
  #8
Mar 24th, 2009
Nuc: so if i have you right, i could just write an 'if' statement saying:
if(numA<numB)
   numA / 2.0
else
   accum +=numA

and then i could wrap this in a function for later use
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 5
Reputation: kuru225 is an unknown quantity at this point 
Solved Threads: 0
kuru225 kuru225 is offline Offline
Newbie Poster

Re: C++ Calculations

 
0
  #9
Mar 24th, 2009
Ok
i have been tinkering around with the coding of this, and i think there is a problem with this function i have
could someone give me there input
void getnums()
{
     
  int numA, numB, x, result;
  cout << "Your two numbers to multiply are?"<<endl;
  cin >> numA >> numB;

  if (numB < numA)
  {
    x +=numA;
    numA = numB;
    numB = x;
  }
   
  result = process( numA, numB);

  
  cout << numA << " times " << numB<< " by conventional math = " << (numA * numB) <<endl;
  cout << numA << " times " << numB<< " by Zoo's method = " << result <<endl;
  
}
the function works fine if numA starts smaller then numB but if numB is smaller then numA it sort of goes to the crapper
any help would be welcomed
thanx
Last edited by kuru225; Mar 24th, 2009 at 12:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 979
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 209
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: C++ Calculations

 
0
  #10
Mar 24th, 2009
Problem is that x is not initialized to zero (I take that you simply want to swap the values).

You might have it a bit simpler like ...
  1. if (numB < numA)
  2. {
  3. // use x only inside the if-block
  4. int x = numA;
  5. numA = numB;
  6. numB = x;
  7. }
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC