Divide and Conquer rule for multiplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int firstNumber = 0 , duplicateFirst = 0;
int secondNumber = 0 , duplicateSecond = 0;
int *total = NULL;
total = new int;
int arraytotal = 0;
int firstLength = 0;
int secondLength = 0;
cout << "Enter first number : ";
cin >> firstNumber;
duplicateFirst = firstNumber;
//Do validation to prevent character input
while( !cin.good() ){
cin.clear();
cin.ignore(100,'\n');
cout << "Cannot enter character ! Please re-enter ! " << endl;
cout << "Enter first number : ";
cin >> firstNumber;
}
cout << "Enter second number : ";
cin >> secondNumber;
duplicateSecond = secondNumber;
//Do validation to prevent character input
while( !cin.good() ){
cin.clear();
cin.ignore(100,'\n');
cout << "Cannot enter character ! Please re-enter ! " << endl;
cout << "Enter second number : ";
cin >> secondNumber;
}
while( duplicateFirst > 0 ){
duplicateFirst = duplicateFirst / 10;
firstLength++;
}
if(firstLength%2 != 0){
firstLength++;
}
cout << "Length : " << firstLength << endl;
while( duplicateSecond > 0 ){
duplicateSecond = duplicateSecond / 10;
secondLength++;
}
if(secondLength%2 != 0){
secondLength++;
}
cout << "Length : " << secondLength << endl;
int firstLeft = 0;
int firstRight = 0;
int secondLeft = 0;
int secondRight = 0;
int sizeValue = 0;
int powerValue = 0;
int tmp = 0;
powerValue = pow( 10.0 , (firstLength / 2) );
firstLeft = firstNumber / powerValue;
firstRight = firstNumber % powerValue;
cout << "First Left : " << firstLeft << endl << "First Right : " << firstRight << endl;
secondLeft = secondNumber / powerValue;
secondRight = secondNumber % powerValue;
cout << "Second Left : " << secondLeft << endl << "Second Right : " << secondRight << endl;
total[arraytotal] = firstLeft * secondLeft;
if( total[arraytotal] == firstLeft*secondLeft ){
tmp = pow( 10.0 , firstLength );
total[arraytotal] = total[arraytotal]*tmp;
arraytotal++;
}
total[arraytotal] = firstLeft * secondRight;
if( total[arraytotal] == firstLeft * secondRight ){
tmp = pow( 10.0 , firstLength / 2 );
total[arraytotal] *= tmp;
arraytotal++;
}
total[arraytotal] = firstRight * secondLeft;
if( total[arraytotal] == firstRight * secondLeft ){
tmp = pow( 10.0 , secondLength / 2 );
total[arraytotal] *= tmp;
arraytotal++;
}
total[arraytotal] = firstRight * secondRight;
if( total[arraytotal] == firstRight * secondRight ){
arraytotal++;
}
int sumTotal = 0;
for( int i = 0 ; i < arraytotal ; i++ ){
sumTotal += total[i];
}
cout << "Total : " << sumTotal << endl;
system( "pause" );//Pause window
return 0;
}
|
i know i miss out the rule for
divide and conquer but for me i work until so far.
can anyone try to help me>
Topic archived. No new replies allowed.