C++ calculations

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

C++ calculations

 
0
  #1
Oct 18th, 2007
This is what I've got so far:
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12. double numA=0;
  13. double numB=0;
  14. double productB;
  15. double accumulator = 0;
  16. cout<<"Your two numbers to multiply are?"<<endl;
  17. cin>>numA>>numB;
  18.  
  19. double productA = (numA * numB);
  20.  
  21. if (numA>numB||numA%2==1)//check if larger and odd???
  22. {
  23. accumulator+=accumulator;//how to add the smallest # to the accumulator???
  24. }
  25. then (numA/2) + (2*numB);//something to make the program continue
  26. {
  27. accumulator+=accumulator;//if the larger number's
  28. division is odd, the smaller number's doubling is added to an accumulator
  29. }//after all this assign the value to productB
  30.  
  31. Any assistance is appreciated.
  32. cout<<numA<<" times "<<numB<<" by conventional math = "<<productA<<endl;
  33. cout<<numA<<" times "<<numB<<" by Brown's method = "<<productB<<endl;
  34.  
  35.  
  36. return 0;
  37. }
  38.  
The assignment is as follows:
Program "multiplication"
With too much time on his hands, Professor Brown has devised a new system of multiplication
called the "half and double" method. 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)
Run the program three times with the following:
1. 82 x 122 (Entered in that order) 2. 124 x 463 (Entered in that order)
3. 1219 x 1641 (Entered in that order)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: C++ calculations

 
1
  #2
Oct 18th, 2007
First: Only include stuff you need. Go ahead and get rid of everything except iostream. (You don't use any I/O manipulators, you don't use any C math functions, you don't read or write to files, and you don't use any std::strings.)

Second: This point probably isn't obvious at first glance, but you should be using int instead of double. All the math taking place is integer arithmetic and all the example output lacks fractional parts.

Third: "Write a program using functions". You are missing functions (well, besides main). Try to divide your program up into simple functional parts:

main(): in a loop, call a function that asks for two numbers and does the assignment stuff, then ask the user if he wants to do it again. If not, quit the loop and quit the program.

functionA(): ask two numbers from the user. In a loop, complete the assignment's 'accumulator' stuff. Print the results.

functionB(): do the stuff that actually modifies the accumulator...

Make sure to come up with better function names than i've given you here. Also, don't use global variables. Make the functions take arguments and return values.

Finally: You need to get out a piece of paper and clearly write down the exact actions that the functionB() takes for each possibility. The accumulator will be modified one of two ways based on whether or not the larger number is odd or even. Do this and you'll breeze through this assignment.

Hints:
1. Make a function to determine whether a number is odd or even. You can tell it is odd if you divide by two and have a remainder. That is 12 % 2 == 0: even. 13 % 2 == 1: odd.
2. Which of the numbers is larger may change during the calculation. Make sure to check which is larger every time through the loop (every time functionB() is called).

Good luck.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
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
  #3
Oct 18th, 2007
>>2. Which of the numbers is larger may change during the calculation. Make sure to check which is larger every time through the loop (every time functionB() is called).

This isn't necessary. The determination of largest and smallest only needs to be made once for each set of inputs as the numbers need to stay in the same relative position during processing irrespective of how big the smaller input gets relative to the larger input as the larger input heads towards 1 (or 0).

I'd use funtions called something like:
run;
obtainInput;
isLarger;
isOdd;
process;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: C++ calculations

 
0
  #4
Oct 18th, 2007
Hmm, if you say so. The program specification appears to read the other way to me. You might want to check with your professor to be sure.

It looks like you have got good function names. All the interesting stuff happens in process, so concentrate there.

Good luck!
Last edited by Duoas; Oct 18th, 2007 at 6:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
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
  #5
Oct 18th, 2007
I base my response on the examples where the smaller number is doubled each time the larger number is halved using integer math until the larger number equals one. With each step (halving and doubling) , the incremented value of the initially smaller number is added to the accumulator if the present value of the integer representing the largest initial number is odd.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: C++ calculations

 
0
  #6
Oct 18th, 2007
Ah, yes, you're right. I didn't crunch the examples like I should have. The assignment should have said something like "the initially larger number" or some such. Heh.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: C++ calculations

 
0
  #7
Oct 19th, 2007
Your input is appreciated. As required by my professor, i had taken your advice. I've revised my code with the use of functions...it doesn't work..this is where i'm stuck.here goes:
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12. int numA=0;
  13. int numB=0;
  14. int productB;
  15. int accumulator = 0;
  16.  
  17. void(getinput)
  18. {
  19. cout<<"Your two numbers to multiply are?"<<endl;
  20. cin>>numA>>numB;
  21. }
  22.  
  23. void (IsLarger)
  24. {
  25. {
  26. if (numA>numB)
  27. numA==larger;
  28. }
  29. else numA==smaller;
  30. }
  31.  
  32.  
  33. void(IsOdd)
  34. {
  35. while (larger%2==1)
  36. accumulator+=smaller;//how to add the smallest # to the accumulator???
  37. }
  38.  
  39. void (processdata)
  40. {
  41. {
  42. (larger/2)(smaller*2)
  43.  
  44.  
  45.  
  46. }
  47.  
  48.  
  49. int productA = (numA * numB);
  50. cout<<numA<<" times "<<numB<<" by conventional math = "<<productA<<endl;
  51. cout<<numA<<" times "<<numB<<" by Brown's method = "<<productB<<endl;
  52.  
  53.  
  54. return 0;
  55. }
I'm not sure on how to carry out this part of the assignment:
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.
Thanks for your assistance.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: C++ calculations

 
0
  #8
Oct 19th, 2007
Er, C languages don't permit subfunctions. Try this:
  1. #include <iostream>
  2.  
  3. void run();
  4. int process( int larger, int smaller );
  5. bool isOdd( int number );
  6.  
  7. void main() {
  8. char c;
  9. do {
  10. run;
  11. for (c = 'x'; (c != 'Y') && (c != 'y') && (c != 'N') && (c != 'n');) {
  12. std::cout << "Would you like to run again (yes/no)?" << std::endl;
  13. std::cin >> c;
  14. }
  15. } while ((c != 'N') && (c != 'n'));
  16. }
  17.  
  18. void run() {
  19. int numA, numB, x, result;
  20.  
  21. // Get the two integer numbers from the user
  22. std::cout << "Please enter two integers to multiply:" << std::endl;
  23. std::cin >> numA >> numB;
  24.  
  25. // Force numA to be the larger number:
  26. if (numB > numA) {
  27. x = numA;
  28. numA = numB;
  29. numB = x;
  30. }
  31.  
  32. result = process( numA, numb );
  33.  
  34. std::cout >> "ZanDiego Class 12 Oct 16, 2007 2:30pm\n";
  35. std::cout << numA << " times " << numB
  36. << " by conventional math = " << (numA * numB) << std::endl;
  37. std::cout << numA << " times " << numB
  38. << " by Brown's method = " << result << std::endl;
  39. }
  40.  
  41. int process( int larger, int smaller ) {
  42. // your code here
  43. }
  44.  
  45. bool isOdd( int number ) {
  46. if ((number % 2) == 1) return true;
  47. return false;
  48. }

You'll notice I've only used code that you have supplied except for:
- I've improved a few of the prompts to better language
- The main() function
- process() gets two arguments, properly sorted into larger and smaller
- eliminated extra variables and includes
Try to avoid using namespace std;. It gets you into trouble later.

Good luck.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: C++ calculations

 
0
  #9
Oct 19th, 2007
I had modified the program a bit:
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. void run();
  10. int process(int larger, int smaller);
  11. bool isOdd(int number);
  12.  
  13. int main()
  14. {
  15. char again = 'y';
  16.  
  17. cout << "Would you like to run again (yes/no)?" <<endl;
  18. cin >> again;
  19. while (again=='y')
  20.  
  21. {
  22. void run()
  23. {
  24. int numA, numB, x, result;
  25. cout<<"Your two numbers to multiply are?"<<endl;
  26. cin>>numA>>numB;
  27.  
  28. if (numB>numA)
  29. {
  30. x=numA;
  31. numA=numB;
  32. numB=x;
  33. }
  34.  
  35. result = process( numA, numB);
  36.  
  37. cout << "Zan Diago Oct 24, 2007 2:30pm\n";
  38. cout << numA << " times " << numB<< " by conventional math = " << (numA * numB) <<endl;
  39. cout << numA << " times " << numB<< " by Brown's method = " << result <<endl;
  40. }
  41.  
  42.  
  43. int process( int larger, int smaller )
  44. {
  45. int result=0;
  46. if (larger%2==0)
  47. {
  48. result+=smaller;
  49. smaller*=2;
  50. larger/=2;
  51. }
  52. return result;
  53.  
  54. }
  55.  
  56.  
  57. bool isOdd( int number )
  58. {
  59. if ((number % 2) == 1) return true;
  60. else
  61. return false;
  62. }
  63. }
Any help is appreciated. As of now, the program only prints out :"Go again?".
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: C++ calculations

 
1
  #10
Oct 19th, 2007
>As of now, the program only prints out :"Go again?".
It shouldn't print anything. That code won't compile because the braces are mismatched and functions don't nest inside of each other. This works better:
  1. #include <iomanip>
  2. #include <cmath>
  3. #include <fstream>
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. void run();
  10. int process(int larger, int smaller);
  11. bool isOdd(int number);
  12.  
  13. int main()
  14. {
  15. char again = 'y';
  16.  
  17. while (again=='y')
  18. {
  19. run();
  20. cout << "Would you like to run again (yes/no)?" <<endl;
  21. cin >> again;
  22. }
  23. }
  24.  
  25. void run()
  26. {
  27. int numA, numB, x, result;
  28. cout<<"Your two numbers to multiply are?"<<endl;
  29. cin>>numA>>numB;
  30.  
  31. if (numB>numA)
  32. {
  33. x=numA;
  34. numA=numB;
  35. numB=x;
  36. }
  37.  
  38. result = process( numA, numB);
  39.  
  40. cout << "Zan Diago Oct 24, 2007 2:30pm\n";
  41. cout << numA << " times " << numB<< " by conventional math = " << (numA * numB) <<endl;
  42. cout << numA << " times " << numB<< " by Brown's method = " << result <<endl;
  43. }
  44.  
  45. int process( int larger, int smaller )
  46. {
  47. int result=0;
  48. if (larger%2==0)
  49. {
  50. result+=smaller;
  51. smaller*=2;
  52. larger/=2;
  53. }
  54. return result;
  55.  
  56. }
  57.  
  58. bool isOdd( int number )
  59. {
  60. if ((number % 2) == 1) return true;
  61. else
  62. return false;
  63. }
Last edited by Ptolemy; Oct 19th, 2007 at 3:31 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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