### syntax problem+arithmatic in c language

here is problem in c language,,
Two integers are stored in the arrays a 1 and a 2, respectively, and the product is calculated by the same procedure as the calculation, but it does not output the correct result.
question is : want to produce 312*321 = 1 0 0 1 5 2 but this first program produce
? 0 9 9 11 5 2
to produce right result 1 0 0 1 5 2, call function name func(c,N*2) in second program

 ``12345678910111213141516171819202122232425262728293031`` `````` #include #include #define N 3 int main() { int a1[N]={1,2,3}; int a2[N]={2,1,3}; int b[N][N]; int c[N*2]; int i,j; for(i=0;i=0;i--) { printf("%d ",c[i]); } printf("\n"); return 0; }``````

|0|１|2| ->A1
----------------
| 0|2|４|6|
| 1|1|2|3|
| 2|3|6|9|

this array is the same as 321*312 calculate using hand in paper

Problem: Define the function func () to output the correct result, call func (c, N * 2); below i post the code with the call function func() in bold. any idea?? and also what the logic behind func()? trial and error? is there algorithm behind this?
 ``123456789101112131415161718192021222324252627282930313233343536373839`` `````` #include #include #define N 3 int main() { int a1[N]={1,2,3}; int a2[N]={2,1,3}; int b[N][N]; int c[N*2]; int i,j; for(i=0;i=0;i--) { printf("%d ",c[i]); } printf("\n"); return 0; } **void func(int a[],int digit) { here no idea.... }** ``````
Last edited on
Your code is carrying out multiplication, with the elements of c[] being the multiples of successive powers of 10. The only thing wrong with your original calculation is that you end up with an 11, which doesn't fit in a single digit (in decimal). Doing it by hand you would leave 1 in this place and carry 10 to the next column (where it would contribute 1, as this is a higher power of 10).

Your func() function is presumably to undertake any "carry" operations. Working from 0 to last index, if a digit of c[i] is 10 or greater then leave the last digit (i.e. c[i]%10) in c[i] and add the whole-number multiples of 10 (i.e. c[i]/10) to c[i+1].

I should also mention that hard-coding the c[i] initial calculations in lines 19-23 means your code will only be useful when N=3. You should code a (nested) loop structure here to make it work for any value of N.
Last edited on
@lastchance thankyou for answering!! here what do you mean by multiples of successive powers of 10? and also what is the function of the array table here?
and how to code nested structure to produce result greater than N?
i understood the carry here. but didnt understand the relation between carry and the array table...
and in here actually i dont understand the use of 19-23 array here.. is this to find the calculation? can you find the carry pattern from the array?
Last edited on
I think the array `int b[N][N]` is not really needed, except to help with understanding how the calculation would be done by hand using pen and paper.

You can go directly from a1 and a2 to array c like this:

 ``12345678910`` `````` int a1[N] = {1,2,3}; int a2[N] = {2,1,3}; int c[N*2] = {0}; for (int i=0; i

Notice array c is initialised to zeros, because values are added to its existing contents in the for loop, if it started out containing something other than zeros, the result would be incorrect.

If you do that, lines 19-23 in the original code are not needed.

After that, you still have to solve the original problem, making sure each digit is in the range 0 to 9 (use the % operator) and propagate the carry digit into the next column.
hello thanks!!!!!!! i mean this is so hard to find especially to think c[i+j]
did you find this by trial and error?
i mean i could see the pattern but to think to write it in code is so hard,
to think c and initialize it as zero also hard,,,
to change line 19-23 to loop also hard..
 ``12345`` `````` for (int i=0; i

can you give me an advice how to find pattern and solve problem like this?
Well, you could call it trial and error, but that plays a minor role. It is more intuition, and lots of practice. Sometimes I try an idea and I may be off by 1, for example putting something in column 4 when it should go in column 3, that sort of thing.

But really, if you work through it methodically, there are no guesses, it is just a matter of following a set of rules, and identifying a pattern in those rules. Finding the pattern is just practice and experience.

In my mind I was picturing how I would multiply two numbers using pencil and paper, without a calculator. There are a set of rules one follows when doing that, it isn't trial and error. The important part is translating what you would do by hand into something a computer can understand.
@chervil thankyou!!!
i understood the logic to find the carry and just showing the last digit, but
still change line 19-23
is hard, i mean did you guess to find the relation between i and j??
is this related to the array table too?
can you explain the logic to find relation between i and j and to translate it?
is there another way beside intitialize c with 0?

 ``12345`` `````` for (int i=0; i

and where to practice problem like this?
thankyou!!!!!!

The logic in getting to the equation for c[i+j] is not magical c++, but simply how we do multiplication in a decimal (or other) system.

Your example was 312 * 321. With our decimal placement system this is
( 3 hundreds + 1 ten + 2 ones ) * ( 3 hundreds + 2 tens + 1 one )
If you multiplied out the brackets you would get 9 separate products (N*N, in fact). Each one will be of the form
(a1 * jth power of 10) * ( a2 * ith power of 10) = a1.a2 * (i+j power of 10)
Example:
3 hundreds * 3 hundreds = 9 * 104, so adding 9 to c[4].

This type of arithmetic is also how you would multiply two polynomials, for example. There they would be powers of some unknown x, whereas here we have powers of 10.

For your other question there are plenty of other ways of initialising c to zero, such as having a loop over i in which we explicitly set c[i] = 0; however, @Chervil's approach is by far the easiest for this type of array.
Last edited on
@lastchance
thankyou for explaining really detailed and giving the biggest hint!!!!!!!!!!!
wow i just realized, the array was as the same as when doing multiplication by hand,

but still realizing that this is the multiplication power of 10 is not easy,
at first i thought the array is not used at all, besides only showing the multiplication sequence by hand
.i also thought at first that i+j was found by trial and error and i thought c[] was some random array to store the multiplication result..

but here did you realized that it was the multiplication power of 10 by first see the array and combining the result with the multiplication by hand so you can see the pattern?
i mean for the first time it is hard..
really requires lot of practice and deep understand math
but really appreciate, otherwise i still see this problem as magic
Topic archived. No new replies allowed.