CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2009
    Posts
    1

    large number multi plication

    hi all i am trying to multiply to big numbers..the code is here as u scroll down to the last part i am unable to get the result for multiplication ..can some one help me out please the addition is successful





    #include <stdio.h>
    #include <stdlib.h>
    #include<iostream>
    #include<ncurses.h>
    #include<string.h>

    using namespace std;

    int main ()
    {
    int i=0,k=0;
    int tmp1=0, tmp2=0, tmp3=0;
    char * a;
    a= (char *) malloc(30 * sizeof(char));

    char * b;
    b= (char *) malloc(30 * sizeof(char));

    int * x;
    x= (int *) malloc(30 * sizeof(int));

    int * y;
    y= (int *) malloc(30 * sizeof(int));

    int * z;
    z= (int *) malloc(61 * sizeof(int));

    int * w;
    w= (int *) malloc(31 * sizeof(int));

    int * v;
    v= (int *) malloc(61 * sizeof(int));

    int * solution;
    solution= (int *) malloc(61 * sizeof(int));


    int carry = 0;
    int c[61];
    int d[61];

    int f[61];
    int g[61];

    cout<<"\nEnter a number (up to 30 digits in length)> "; cin >> a;

    i=0;

    while(i < strlen(a)){
    c[i] = a[i] - '0';
    i++;}


    cout << "\nThe first number is: ";

    for(i=0; i < strlen(a); i++){
    cout << c[i];}

    cout <<"\n";


    //string reversal

    for(i=0, k = 29; i < strlen(a); i++){
    x[k] = c[(strlen(a)-1)-i];
    k--;}

    cout << "\nEnter a second number (up to 30 digits in length)> "; cin >> b;

    i=0;
    while(i < strlen(b)){
    d[i] = b[i] - '0';
    i++;}

    cout << "\nThe second number is: ";

    for(i=0; i < strlen(b); i++){
    cout << d[i];}
    cout << "\n";

    //string reversal

    for(i=0, k = 29; i < strlen(b); i++){
    y[k] = d[(strlen(b)-1)-i];
    cout <<"\nThe sum of the two numbers is: ";

    for(k=30; k>=0; k--)
    {
    w[k] = 0; //w[k] initialised
    }

    //addition

    for (k = 29; k >= 0; k--){
    w[k] = x[k] + y[k] + w[k]; //+w[k] is accounting for the carry

    //carry taken care of

    if (w[k] >= 10){
    w[k] = w[k]%10;
    w[k-1] = 1;}
    }

    for(i= -1; i < 30; i++){
    cout << w[i];}

    cout <<"\n";

    for(k = 60; k>=0; k--){
    z[k] = 0;
    f[k] = 0;
    g[k] = 0;
    solution[k] = 0;
    }
    for(k = 0; k < 30; k++){
    f[k+31] = x[k];
    g[k+31] = y[k];
    }

    cout <<"\nThe product of the two numbers is: ";

    /*The multiplication algorithm begins here*/

    for(i= 60; i >= 0; i--) {
    for(k=60; k>=0; k--)
    { tmp1 = f[i] * g[k];
    z[k] = tmp3 + z[k-1];
    tmp3 = tmp1 % 10;
    z[k-1]= tmp1 / 10;
    }
    }

    /*The multiplication algorithm ends here*/

    for(i = 0; i < 61; i++){
    cout << z[i];}
    cout <<"\n\n";

    return 0;
    }
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: large number multi plication

    1) Please use code tags when posting code. The code you posted is unreadable due it being unformatted.

    2) Why are you using malloc()? Why not just declare arrays?

    3) Your program has memory leaks due to never calling free().

    4) Use more descriptive names for your variables instead of "x", "y", "z", etc.

    5) You should break down that entire main() function into more functions. There should be a function just to add to big numbers, a function to reverse numbers, etc. Making one huge main() function gets confusing and unwieldly.

    6) Did you use a debugger? If not, please do so to know exactly what your program is doing at each step.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 25th, 2009 at 10:18 AM.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: large number multi plication

    Besides what Paul already pointed out, it's not clear what the issue is. You may want to try using 64-bit types instead of 32-bit ones, and by that I mean __int64 instead of int, that may help if your value wraps around 2+ billion for int.

    Quote Originally Posted by Paul McKenzie
    1) Please use code tags when posting code.
    Paul, I'm not sure if you have access to the webmaster of this site, but it would greatly help if they installed a "code" button, like many forums now have, that will insert the code block automatically.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: large number multi plication

    They do. It's the small button with the word 'Code' written on it.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: large number multi plication

    Quote Originally Posted by ankscorek View Post
    hi all i am trying to multiply to big numbers..
    Are you attempting to write this in C or C++?
    Nearly all of your code is in C, except for the <iostream> functions.

    It should be worth pointing out, assuming you want to learn a lot more about C++, that experienced C++ coders, if asked to do this task from scratch, would most probably create a Big_Number class with overloaded arithmetic and streaming operators.

    i.e.

    Code:
    Big_Number a;
    Big_Number b;
    
    cout <<  "Enter the first number";
    cin >> a;
    
    cout <<  "Enter the second number";
    cin >> b;
    
    Big_Number c = a + b;
    
    cout << "The sum is " << c;
    This is probably way beyond your understanding of C++ at the moment, but should show you what can be achieved.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: large number multi plication

    Quote Originally Posted by JohnW@Wessex View Post
    They do. It's the small button with the word 'Code' written on it.
    I'm not sure what browser you use, my Firefox doesn't have such button. Take a look....
    Attached Images Attached Images

  7. #7
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: large number multi plication

    Hi,

    To see the "code" and all the other nice buttons go to the User CP -> Edit Options -> Miscelaneous Options -> Editor Interface.

    With regards
    Programartist
    Last edited by ProgramArtist; August 27th, 2009 at 03:41 AM.

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: large number multi plication

    Quote Originally Posted by ProgramArtist View Post
    To see the "code" and all the other nice buttons go to the User CP -> Edit Options -> Miscelaneous Options -> Editor Interface.
    You see, this all should be available by default... I've been using this forum for all this time and had no clue that it was there What would you expect from a novice user then?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: large number multi plication

    Quote Originally Posted by dc_2000 View Post
    You see, this all should be available by default...
    Strange, since I've never had to set anything to see the code tags button.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: large number multi plication

    Quote Originally Posted by Paul McKenzie
    Strange, since I've never had to set anything to see the code tags button.
    There likely was a change in editor interface (e.g., when updating vbulletin) that your forum participation pre-dated.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured