This is what I've got so far:

``````#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;

int main()
{
double numA=0;
double numB=0;
double productB;
double accumulator = 0;
cout<<"Your two numbers to multiply are?"<<endl;
cin>>numA>>numB;

double productA = (numA * numB);

if (numA>numB||numA%2==1)//check if larger and odd???
{
accumulator+=accumulator;//how to add the smallest # to the accumulator???
}
then (numA/2) + (2*numB);//something to make the program continue
{
accumulator+=accumulator;//if the larger number's
division is odd, the smaller number's doubling is added to an accumulator
}//after all this assign the value to productB

Any assistance is appreciated.
cout<<numA<<" times "<<numB<<" by conventional math = "<<productA<<endl;
cout<<numA<<" times "<<numB<<" by Brown's method = "<<productB<<endl;

return 0;
}``````

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
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
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)

[B]First[/B]: Only include stuff you need. Go ahead and get rid of everything except [B]iostream[/B]. (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 [B]std::string[/B]s.)

[B]Second[/B]: This point probably isn't obvious at first glance, …

## All 14 Replies

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.

>>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;

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!

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.

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. :\$

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:

``````#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;

int main()
{
int numA=0;
int numB=0;
int productB;
int accumulator = 0;

void(getinput)
{
cout<<"Your two numbers to multiply are?"<<endl;
cin>>numA>>numB;
}

void (IsLarger)
{
{
if (numA>numB)
numA==larger;
}
else numA==smaller;
}

void(IsOdd)
{
while (larger%2==1)
accumulator+=smaller;//how to add the smallest # to the accumulator???
}

void (processdata)
{
{
(larger/2)(smaller*2)

}

int productA = (numA * numB);
cout<<numA<<" times "<<numB<<" by conventional math = "<<productA<<endl;
cout<<numA<<" times "<<numB<<" by Brown's method = "<<productB<<endl;

return 0;
}``````

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.

Er, C languages don't permit subfunctions. Try this:

``````#include <iostream>

void run();
int process( int larger, int smaller );
bool isOdd( int number );

void main() {
char c;
do {
run;
for (c = 'x'; (c != 'Y') && (c != 'y') && (c != 'N') && (c != 'n');) {
std::cout << "Would you like to run again (yes/no)?" << std::endl;
std::cin >> c;
}
} while ((c != 'N') && (c != 'n'));
}

void run() {
int numA, numB, x, result;

// Get the two integer numbers from the user
std::cout << "Please enter two integers to multiply:" << std::endl;
std::cin >> numA >> numB;

// Force numA to be the larger number:
if (numB > numA) {
x = numA;
numA = numB;
numB = x;
}

result = process( numA, numb );

std::cout >> "ZanDiego  Class 12  Oct 16, 2007 2:30pm\n";
std::cout << numA << " times " << numB
<< " by conventional math = " << (numA * numB) << std::endl;
std::cout << numA << " times " << numB
<< " by Brown's method = " << result << std::endl;
}

int process( int larger, int smaller ) {
}

bool isOdd( int number ) {
if ((number % 2) == 1) return true;
return false;
}``````

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.

I had modified the program a bit:

``````#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;

void run();
int process(int larger, int smaller);
bool isOdd(int number);

int main()
{
char again = 'y';

cout << "Would you like to run again (yes/no)?" <<endl;
cin >> again;
while (again=='y')

{
void run()
{
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 << "Zan Diago  Oct 24, 2007 2:30pm\n";
cout << numA << " times " << numB<< " by conventional math = " << (numA * numB) <<endl;
cout << numA << " times " << numB<< " by Brown's method = " << result <<endl;
}

int process( int larger, int smaller )
{
int result=0;
if (larger%2==0)
{
result+=smaller;
smaller*=2;
larger/=2;
}
return result;

}

bool isOdd( int number )
{
if ((number % 2) == 1) return true;
else
return false;
}
}``````

Any help is appreciated. As of now, the program only prints out :"Go again?".

>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:

``````#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>

using namespace std;

void run();
int process(int larger, int smaller);
bool isOdd(int number);

int main()
{
char again = 'y';

while (again=='y')
{
run();
cout << "Would you like to run again (yes/no)?" <<endl;
cin >> again;
}
}

void run()
{
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 << "Zan Diago  Oct 24, 2007 2:30pm\n";
cout << numA << " times " << numB<< " by conventional math = " << (numA * numB) <<endl;
cout << numA << " times " << numB<< " by Brown's method = " << result <<endl;
}

int process( int larger, int smaller )
{
int result=0;
if (larger%2==0)
{
result+=smaller;
smaller*=2;
larger/=2;
}
return result;

}

bool isOdd( int number )
{
if ((number % 2) == 1) return true;
else
return false;
}``````

Ptolemy-welcome aboard and thanks for your input. The program doesn't work wen using "Brown's method". It actually shows the last #, that the user entered. I think there may be something wrong with my process function.

That's a logic error. What is "Brown's method" supposed to be doing?

Brown's 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
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
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)``````

That's not a good description of the algorithm. :( You want to loop as long as larger is greater than zero. You want to always double smaller and halve larger, and only add smaller to the accumulator if larger is odd:

``````int process( int larger, int smaller )
{
int accumulator = 0;

while ( larger > 0 ) {
if ( larger % 2 != 0 )
accumulator += smaller;

larger /= 2;
smaller *= 2;
}

return accumulator;
}``````

Ok, problem solved. Thanks again, time to move on to my next thread. see you around.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.