Thread: urgent help!!arrays and float-double types...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    25

    urgent help!!arrays and float-double types...

    hi everyone!my problem is :i have a homework but i dunno what to do...
    problem to solved is:you will be given two numbers 1 to 250 digits and you are expected to multipicilate them, but the issue is it's forbidden to use words: "float" and "double" in your code. in worksheet it says "you need not to use two dimensional arrays"
    i think we aresupposed to use arrays...
    could you give me a hand? at least an idea for where to start???

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Are you sure it says "you need not to use two dimensional arrays"? That's terrible english.

    I'd suggest you use arrays of char to store the digits of each number, and go back to elementary school arithmetic and write a function to perform long multiplication, same as if you were working it out on paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    it says you do not need to use....
    we've learned only types, while-if-switch-for staff...so i dunno how to write or a call for a function...we have to do it with what we've learned so far...

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The thing that's confusing me is where you say "1 to 250 digits". Are you sure you mean digits? Do you mean the numbers 1 - 250 or do you actually mean 250 digits?

    This is 1 digit

    5

    This is 10 digits

    1267485479

    This is 30 digits

    124544594860485063850689374596

    I think you can imagine 250 digits.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    i'm sure it says digits...that's the trick:check out the following outputs:

    bash$lab1
    2131423 *281980
    601018657540

    bash$lab1
    2987532532535325432543254321545432*213019102784973 509218303143553532
    63516719149794235319304380897326655532843276453275 42664893495999

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Assuming you know the algorithm for long multiplication, you could do this
    Code:
      12
    * 34
    ------
      48    <- result of 4 * 12
     360    <- result of 30 * 12
    ------
     408    <- added together
    Longer numbers are just more iterations of the loop.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    could you explain it a little more???i know algorithm but there are still question marks in my mind!!!
    how will I manage to store big numbers??in chars?
    I cannot figure out how to start!!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    char num1[] = "12";
    char num2[] = "34";
    char result[8];

    Just make your char arrays big enough to store your numbers.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    but what about the time c compiler processing my code???
    an overflow could happen , if I store the result in type "int" as it's forbidden to use float and double....an overflow is likely to happen in the compiling process , how should I use the multiplication algorithm??

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I've already explained how to do long multiplication.
    I've already explained how to store a long string of digits.
    You figure something out for yourself.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    i've realised I need a simple, fine algorithm to apply to c.the algoritms i need are ok for mathematics but not for computers.could you recommend me a fine and simple algorithm???

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by philae
    could you recommend me a fine and simple algorithm???
    I recommend the one that you write yourself. There is no standard function that contains an algorithm to do you whole program.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    25

    Talking

    oh, no!!i didn't mean the whole programming i asked for a multiplication algorithm.

  14. #14
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    oh, no!!i didn't mean the whole programming i asked for a multiplication algorithm.
    Refer to what salem said

    I've already explained how to do long multiplication.
    I've already explained how to store a long string of digits.
    You figure something out for yourself.
    You have the tools to get started now. Until you've done some work and tried coding it you're not going to get very far. BTW, you were off to a bad start with the thread title

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How hard can it be to multiply two single digit numbers?

    > the algoritms i need are ok for mathematics but not for computers
    Algorithms exist independently, you can work through the same sequence
    - in your head
    - on paper
    - in a computer
    And get the same answer.

    The only difference with a computer is that it's usually quicker, that's all.

    Now try and write some code to do 12 * 34 like I showed you.
    Once you've managed that much, the big number answer isn't far away at all.

Popular pages Recent additions subscribe to a feed