|
From: Kenneth C. <ken...@ho...> - 2008-04-04 12:11:11
|
This is not an assignment. As I have included some information and comments within the code. With these information and short email messages should be able to know what the intention was.
why it doesn't contain any loop? why it just returns a same value? is there some where i have used wrongly in the code?> Date: Fri, 4 Apr 2008 12:40:58 +0200> From: pw...@ia...> To: ken...@ho...> CC: dev...@li...> Subject: Re: [Dev-C++] info> > 1) That code is hurtful to see. How would it look if you had more numbers> with more digts? Think again, and simplify the code. It's a question of> algorithms. A good algorithm shouldd try to support varying limits without> changing the code - just the input parameters. Your code has hard-coded> expansions for all combinations.> > 2) What do you think your functions with for loops do?> int loopb1() {> for (l = 9; l >= 1; l--)> b1 = l;> return b1;> }> In reality it doesn't contain any loop, and will just return a fixed value> - the same value everytime the function is called.> > By the way: This is a school assignment, which means that you are expected> to spend your time trying to figure out a good solution to the problem.> You will not learn unless you spend the time.> > On the other hand, we don't even know what the assignment is. We may be> able to guess based on the code. But if your code solves the wrong thing,> we will not be able to know, since we don't know what the intention was.> All we can see is if the code performs it's job in a good way. In this> case, you have too much code expansion for that to be true. The code> should be simplified.> > /pwm> > On Fri, 4 Apr 2008, Kenneth Chung wrote:> > >> > hi All,> >> > multiplication.c is working fine as the output solution is correct.> >> > However, what is the problem with multiplication1.c? From there i have made some changes, i have included functions to shorten the for loops. Unfortunately there is no output solution. What is the problem? Is there an alternative to shorten the big bunch of if statement(at the bottom) and for loops(after main) to make it work?> >> > Please find below.> >> > regards,> > Kenneth> >> >> >> >> > Program: multiplication.c> >> > /******************************************************************************************************************************** * Description: Each star stands for a digit, with the leftmost star on each * * line with stars standing for a nonzero digit. This multiplication * * has 3 partial products (the three lines between the two * * * a1 a2 a3 * * horizontal bars). x * * * x b1 b2 b3 * * - all partial products are made up of the same digits (a given ----- -------- * * digit occurs in every partial product or in none of them); * * * * c1 c2 c3 c4 * * - the first and second partial products are different; * * * * d1 d2 d3 d4 * * - the orderings of digits in the second and third partial * * * * e1 e2 e3 e4 * * products are inverse of each other (such as 1424 and 4241); ----------- ----------------- * * - the third partial product is the sum of the first two * * * * * * * * * * * * * * partial products. * ********************************************************************************************************************************/> > #include <stdio.h>#include <stdlib.h>> > /* Define variables for use in the main() functions. */int a1, a2, a3, b1, b2, b3, c1, c2, c3, c4, d1, d2, d3, d4, e1, e2, e3, e4;int i, j, k, l, m, n, o, p, q, r, s, t, x, y, z;unsigned int mltply_1, mltply_2, partial_1, partial_2, partial_3, result;> > int main(void) { /* for() loops are being used to randomly assign a digit * * to each of the stars which is represented by variable * * a1, a2, a3, b1, b2 and b3. */ for (i = 9; i >= 1; i--) { a1 = i; for (j = 9; j >= 1; j--) { a2 = j; for (k = 9; k >= 1; k--) { a3 = k; for (l = 9; l >= 1; l--) { b1 = l; for (m = 9; m >= 1; m--) { b2 = m; for (n = 9; n >= 1; n--) { b3 = n; /* To calculate the value of mltply_1 and mltply_2 by * * using number in a1, a2, a3, b1, b2, b3. The result * * is assigned by multiplying both variable mltply_1 * * and mltply_2. */ mltply_1 = (a1 * 100) + (a2 * 10) + a3; mltply_2 = (b1 * 100) + (b2 * 10) + b3; result = mltply_1 * mltply_2; /* To compute the value of partial_1, partial_2 and partial_3. * * Each partial is strictly not less than 1000 as the leftmost * * star on each line with stars standing for a nonzero digit. * * If the value >= 1000 then it is assigned into variable * * partial_1, partial_2 and partial_3 */ if ((mltply_1 * b3 < 1000) || (mltply_1 * b2 < 1000) || (mltply_1 * b1 < 1000)) continue; partial_1 = mltply_1 * b3; partial_2 = mltply_1 * b2; partial_3 = mltply_1 * b1; /* Upon the values of 3 partial products have been computed * * into 4 digits, each partial may break into single digit * * from the 4 digits number for comparison check later. */ c1 = partial_1 / 1000; o = partial_1 % 1000; c2 = o / 100; p = o % 100; c3 = p / 10; q = p % 10; c4 = q / 1; d1 = partial_2 / 1000; r = partial_2 % 1000; d2 = r / 100; s = r % 100; d3 = s / 10; t = s % 10; d4 = t / 1; e1 = partial_3 / 1000; x = partial_3 % 1000; e2 = x / 100; y = x % 100; e3 = y / 10; z = y % 10; e4 = z / 1; /* All partial products are made up of the same digits. * * The first and second partial products are different. */ if ((c1 == d1) || (c1 == d2) || (c1 == d3) || (c1 == d4)) if ((c2 == d1) || (c2 == d2) || (c2 == d3) || (c2 == d4)) if ((c3 == d1) || (c3 == d2) || (c3 == d3) || (c3 == d4)) if ((c4 == d1) || (c4 == d2) || (c4 == d3) || (c4 == d4)) /* The orderings of digits in the second and third partial products * * are inverse of each other (such as 1424 and 4241). */ if ((d1 == e4) && (d2 == e3) && (d3 == e2) && (d4 == e1)) /* The third partial product is the sum of the first two * * partial products. */ if (partial_3 == (partial_1 + partial_2)) printf("%d x %d = %d, with %d, %d and %d as partial products, is a solution.", mltply_1, mltply_2, result, partial_1, partial_2, partial_3); } } } } } } system("PAUSE"); return EXIT_SUCCESS;}> >> >> >> > Program: Multiplication1.c> >> > /******************************************************************************************************************************** * Description: Each star stands for a digit, with the leftmost star on each * * line with stars standing for a nonzero digit. This multiplication * * has 3 partial products (the three lines between the two * * * a1 a2 a3 * * horizontal bars). x * * * x b1 b2 b3 * * - all partial products are made up of the same digits (a given ------ -------- * * digit occurs in every partial product or in none of them); * * * * c1 c2 c3 c4 * * - the first and second partial products are different; * * * * d1 d2 d3 d4 * * - the orderings of digits in the second and third partial * * * * e1 e2 e3 e4 * * products are inverse of each other (such as 1424 and 4241); ----------- ----------------- * * - the third partial product is the sum of the first two * * * * * * * * * * * * * * partial products. * * * ********************************************************************************************************************************/> > #include <stdio.h>#include <stdlib.h>> > /* Define variables for use in the main(), loopb1(), loopb2() and loopb3 functions. */int a1, a2, a3, b1, b2, b3, c1, c2, c3, c4, d1, d2, d3, d4, e1, e2, e3, e4;int i, j, k, l, m, n, o, p, q, r, s, t, x, y, z;unsigned int mltply_1, mltply_2, partial_1, partial_2, partial_3, result;> > /* Define Function Prototype */int loopb1();int loopb2();int loopb3();> > int main(void) { /* for() loops and function are being used to randomly assign a digit * * to each of the stars which is represented by variable a1, a2, a3, * * b1, b2 and b3. */ for (i = 9; i >= 1; i--) { a1 = i; for (j = 9; j >= 1; j--) { a2 = j; for (k = 9; k >= 1; k--) { a3 = k; /* Calling Functions */ b1 = loopb1(); b2 = loopb2(); b3 = loopb3(); /* To calculate the value of mltply_1 and mltply_2 by * * using number in a1, a2, a3, b1, b2, b3. The result * * is assigned by multiplying both variable mltply_1 * * and mltply_2. */ mltply_1 = (a1 * 100) + (a2 * 10) + a3; mltply_2 = (b1 * 100) + (b2 * 10) + b3; result = mltply_1 * mltply_2; /* To compute the value of partial_1, partial_2 and partial_3. * * Each partial is strictly not less than 1000 as the leftmost * * star on each line with stars standing for a nonzero digit. * * If the value >= 1000 then it is assigned into variable * * partial_1, partial_2 and partial_3 */ if ((mltply_1 * b3 < 1000) || (mltply_1 * b2 < 1000) || (mltply_1 * b1 < 1000)) continue; partial_1 = mltply_1 * b3; partial_2 = mltply_1 * b2; partial_3 = mltply_1 * b1; /* Upon the values of 3 partial products have been computed * * into 4 digits, each partial may break into single digit * * from the 4 digits number for comparison check later. */ c1 = partial_1 / 1000; o = partial_1 % 1000; c2 = o / 100; p = o % 100; c3 = p / 10; q = p % 10; c4 = q / 1; d1 = partial_2 / 1000; r = partial_2 % 1000; d2 = r / 100; s = r % 100; d3 = s / 10; t = s % 10; d4 = t / 1; e1 = partial_3 / 1000; x = partial_3 % 1000; e2 = x / 100; y = x % 100; e3 = y / 10; z = y % 10; e4 = z / 1; /* All partial products are made up of the same digits. * * The first and second partial products are different. */ if ((c1 == d1) || (c1 == d2) || (c1 == d3) || (c1 == d4)) if ((c2 == d1) || (c2 == d2) || (c2 == d3) || (c2 == d4)) if ((c3 == d1) || (c3 == d2) || (c3 == d3) || (c3 == d4)) if ((c4 == d1) || (c4 == d2) || (c4 == d3) || (c4 == d4)) /* The orderings of digits in the second and third partial products * * are inverse of each other (such as 1424 and 4241). */ if ((d1 == e4) && (d2 == e3) && (d3 == e2) && (d4 == e1)) /* The third partial product is the sum of the first two * * partial products. */ if (partial_3 == (partial_1 + partial_2)) printf("%d x %d = %d, with %d, %d and %d as partial products, is a solution.", mltply_1, mltply_2, result, partial_1, partial_2, partial_3); } } } system("PAUSE"); return EXIT_SUCCESS;}> > /* Function Definition */int loopb1() { for (l = 9; l >= 1; l--) b1 = l; return b1;}> > int loopb2() { for (m = 9; m >= 1; m--) b2 = m; return b2;}> > int loopb3() { for (n = 9; n >= 1; n--) b3 = n; return b3;}> > _________________________________________________________________> > Find the job of your dreams before someone else does> > http://mycareer.com.au/?s_cid=596064> >
_________________________________________________________________
Find the job of your dreams before someone else does
http://mycareer.com.au/?s_cid=596064 |