From: Kenneth C. <ken...@ho...> - 2008-04-03 04:47:50
|
Hi All, Does anyone know whether Bloodshed Dev-C++ version 4.01 is C-99 standard or not? Thank you. regards, Kenneth _________________________________________________________________ It's simple! Sell your car for just $30 at CarPoint.com.au http://a.ninemsn.com.au/b.aspx?URL=https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fsecure%2Dau%2Eimrworldwide%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801459%2Fpi%5F1004813%2Fai%5F859641&_t=762955845&_r=tig_OCT07&_m=EXT |
From: Per W. <pw...@ia...> - 2008-04-03 04:51:25
|
Dev-C++ is an IDE, not a compiler. The Dev-C++ IDE is (normally) using the MinGW compiler. But you have the choice to select which version of MinGW you want to compile with. Your question should be: Does the MinGW version I'm using support C-99 or not? In short: google gcc for C-99 support. /pwm On Thu, 3 Apr 2008, Kenneth Chung wrote: > > Hi All, > > Does anyone know whether Bloodshed Dev-C++ version 4.01 is C-99 standard or not? Thank you. > > regards, > Kenneth > _________________________________________________________________ > It's simple! Sell your car for just $30 at CarPoint.com.au > http://a.ninemsn.com.au/b.aspx?URL=https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fsecure%2Dau%2Eimrworldwide%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801459%2Fpi%5F1004813%2Fai%5F859641&_t=762955845&_r=tig_OCT07&_m=EXT |
From: Kenneth C. <ken...@ho...> - 2008-04-04 10:01:19
|
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 |
From: Per W. <pw...@ia...> - 2008-04-04 10:41:14
|
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 |
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 |
From: Per W. <pw...@ia...> - 2008-04-04 19:25:42
|
You wrote: "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." Please post normal ASCII mails to this mailing list. Your original mail was multi-part. One part contained quoted-printable without any line breaks (!), i.e. everything on a single line. Try to read your header text (and the "table" to the right) if it's on a single line... The other part was quoted-printable HTML, i.e. looked something like: /**************************************************************************= ******************************************************<BR> * Des= cription: Each star stands for a digit, with the leftmost star on eac= h &n= bsp;  = ; &n= bsp;  = ; &n= bsp;  = Note that every space in your text got converted to 6 characters in HTML encoding. That is quite wasteful of bandwidth. Your mail many times larger than the real information it contained. Further: Your original source code must have contained tab characters (and most probably very short tab characters too) that got converted to non-break spaces. But not to the correct number of spaces since the mail program did not know your true tab length. Now, you expect people to spend any time trying to read your description? Do post ASCII mails if you care about people spending time to read them. And make sure that your mails does not contain any tab characters that may get the indentation totally mangled! You write: "This is not an assignment." If this is not a school assignment, then what is it? This type of code does not produce any really useful result. This is a coding project to let you exercice algorithms and logic thinking. It is then reasonable to think that this is either school work or an Internet coding contest, such as Project Euler. School work is expected to be done on your own. Coding contests looses most of their meaning unless the problems are solved individually... About your for-loop functions: I said that your for loops didn't work. A reasonable thing to do then would be to take a closer look at them. If you did that but didn't see any problem with them, the next thing would be to assume that I was wrong, or to write a little test program and try them. Calling them 10 times in a row and printing their return value would have made it obvious that the loops did not perform any loop usable to your program. About your code: Before your loops, your comment says: "for() loops are used to randomly assign a digit to each of the [...]". What is random about a for loop? You have two three-digit numbers, i.e. the first digit in each number must be non-zero. You have implemented 6 loops to iterate through all combinations. But if only the first digit in each number must be non-zero, why are all loops iterating from 9 down to 1? Shouldn't four of the six loops include the digit zero? And why do you have two variables for each loop? Why not iterate a1 directly, instead of iterating on i, and assign i to a1? Note that you can convert the six loops into just two with: for (a = 100; a <= 999; a++) { a1 = a/100; a2 = (a/10)%10; a3 = a%10; for (b = 100; b <= 999; b++) { b1 = b/100; b2 = (b/10)%10; b3 = b%10; // Do all work here. } } And since you are only using a1, a2 and a3 to form mltply_1 (which the above code has as loop variable) you can skip evaluating a1, a2 and a3 totally. And if you swap places of the a and b loops, you don't need to evaluate b1, b2, b3 in the inner loop. You have a comment: "The result is assigned by multiplying both variable mltply_1 and mltply_2". Don't you expect anyone looking at your code to recognize that from the following source line? result = mltply_1 * mltply_2; Do not spend time writing comments that tells what individual source lines does. With correct variable and function names, that should be obvious from just looking at the code line. In some situations it may be needed to document _why_ you are doing something, since the reason behind isn't directly visible. Only when you have larger blocks of non-obvious code will it be needed to write a 'summary' to describe what the code does - or is expected to do. Back to your code again. Why are you performing the test: if ((mltply_1 * b3 < 1000) || ... before evaluating partial_1, partial_2, partial_3? If you assign your variables first, you don't have to evaluate the same thing twice. A selective - but very fast - test in your code is the requirement: if (partial_3 == (partial_1 + partial_2)) ... Why do you do that just before the printout? Wouldn't it be better to do it before you spend a huge amount of work to split partial_1, partial_2, partial_3 into 12 digits? Your test of digits verifies that all digits in partial_1 are represented in partial_2. But no test to verify that all digits in partial_2 are represented in partial_1. Why do you need 9 temporary variables just to split the partials into digits? You do not make use of the intermediate values anywhere... mltply_1, mltply_2, result, partial_1, partial_2 and partial_3 are unsigned integers. You have used the formatting character %d which is for signed integers. My suggestions should allow you to remove between half and two thirds of the variables, code lines and indentation levels and probably cut the running time with a factor 10. If the goal is to just get an answer, this doesn't matter. But then, this isn't the type of problem that you solve just to get an asnwer. It is a problem you solve to train your thinking, so the above improvements are important. The goal isn't to find a solution, but to find a good solution. Even more can be done to improve the code, but I must leave a bit for you to think about too :) An important rule: As soon as the code starts to look really messy, it is time to stop what you are doing and start pondering if you are doing something wrong. Most of the time, you are! Most programming problems are intended to have a clever solution. /pwm |
From: Michal M. <mi...@mo...> - 2008-04-04 10:35:14
|
Kenneth Chung napsal(a): > /* 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; > } These loops are useless, the code is equivalent to: int loopb1() { b1 = 0; return b1; } int loopb2() { b2 = 0; return b2; } int loopb3() { b3 = 0; return b3; } |
From: Kenneth C. <ken...@ho...> - 2008-04-04 11:41:47
|
why are they useless? why equal to 0? can i have more explanation?> Date: Fri, 4 Apr 2008 12:35:02 +0200> From: mi...@mo...> To: ken...@ho...> CC: dev...@li...> Subject: Re: [Dev-C++] info> > Kenneth Chung napsal(a):> > > /* 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;> > }> > These loops are useless, the code is equivalent to:> > int loopb1() {> b1 = 0;> return b1;> }> int loopb2() {> b2 = 0;> return b2;> }> int loopb3() {> b3 = 0;> return b3;> } _________________________________________________________________ Find the job of your dreams before someone else does http://mycareer.com.au/?s_cid=596064 |