Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I need help converting from C to C++

  1. Feb 23, 2006 #1
    I tried to convert from C++ to visual C++ .NET but got many errors. (I haven't used C in years) If anyone can do this for me, I'd really appreciate it.

    Please convert the following to visual C++ .NET code (to run on microsoft visual studio 2003 .NET):

    http://www.cplusplus.happycodings.com/Algorithms/code17.html

    thanks in advance

    [edit]Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. (I also want to learn how to do this myself) Help would be greatly appreciated.
     
    Last edited: Feb 24, 2006
  2. jcsd
  3. Feb 23, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    1) If this is some kind of homework assignment, we won't do it for you.

    2) It appears to be pretty atrocious code to begin with.

    3) C++ is a superset of C, so anything written in ANSI C should compile just fine with a C++ compiler.

    - Warren
     
  4. Feb 23, 2006 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    And, incidentally, visual studio has a vanilla C compiler. (At least the version with which I'm familiar)
     
  5. Feb 23, 2006 #4
    I don't think this is an assignment since only a few minor changes were needed to compile under gcc. The errors are mainly caused by the use of Borland extensions that are not part of the standard language (notably clrscr and getch). I just replaced getch and commented out clrscr to get it to compile. I can't say if this code actually works but the new version is below.

    Note: do not use Visual C++ 6.0, it pre-dates the standard and is therefore not compatible with a number of standard C++ expressions. Plus it has some nasty bugs. If this is what you are stuck with then at least get the service packs. Better yet, move on to Code::Block or Dev-C++ which come packaged with gcc, a much better compiler. Both are free.

    Technical note: C++ is not a strict superset of C, it has a few constructs that differ from C so an occasional valid C program may not compile under C++. But it's uncommon and required changes are often minor.

    Code (Text):

    // Matrix Multiplication
    #include <cstdio>
    #include <cstdlib>

    /* to initialize matrix */
    int** init(int** arr,int row,int col) {
        int i=0,
            j=0;

        arr=(int**)malloc(sizeof(int)*row*col);

        for(i=0;i<row;i++) {
            for(j=0;j<col;j++) {
                *((arr+i)+j)=(int*)malloc(sizeof(int));
                *(*(arr+i)+j)=0;
            }
        }
        return arr;
    }

    /* to set value in matrix */
    int** set(int** arr,int row,int col) {
        int i=0,
            j=0,
            val=0;

        for(i=0;i<row;i++) {
            for(j=0;j<col;j++) {
                printf("Enter value for row %d col %d  :",(i+1),(j+1));
                scanf("%d",&val);
                *(*(arr+i)+j)=val;
            }
        }
        return arr;
    }


    /* print values of the passed matrix */
    void get(int** arr,int row,int col) {
        int i=0,
            j=0;

        for(i=0;i<row;i++) {
            for(j=0;j<col;j++) {
                printf("%d\t",*(*(arr+i)+j));
            }
            printf("\n");
        }
    }

    /* mutiply two matrices and return the resultant matrix */
    int** mul(int** arr1,int** arr2,int row,int col,int col1) {
        int **result,
            i=0,
            j=0,
            k=0;

        result=init(result,row,col);

        for(i=0;i<row;i++) {
            for(j=0;j<col;j++) {
                for(k=0;k<col1;k++) {
                    printf("%dX%d(%d)",*(*(arr1+i)+k),*(*(arr2+k)+j),
                           (*(*(arr1+i)+k))*(*(*(arr2+k)+j)));
                    *(*(result+i)+j)+=(*(*(arr1+i)+k))*(*(*(arr2+k)+j));

                    if (k!=(col1-1))
                        printf("+");
                }
                printf("\t");
            }
            printf("\n");
        }
        return result;
    }

    int main() {
        int row1=0,
            col1=1,
            row2=0,
            col2=0,
            **matrix1,
            **matrix2,
            **result;

    //    clrscr();
        printf(" Enter number of row for first matrix ");
        scanf("%d",&row1);

        while (col1!=row2) {
            printf(" Enter number of column for first matrix ");
            scanf("%d",&col1);

            printf(" Enter number of row for second matrix ");
            scanf("%d",&row2);

            if (col1!=row2) {
    //            clrscr();
                printf("Column number of first matrix must be "
                       "same as the row number of second matrix");
            }
        }

        printf(" Enter number of column for second matrix ");
        scanf("%d",&col2);

        matrix1=init(matrix1,row1,col1);
        matrix2=init(matrix2,row2,col2);
        /* setting values in matrix */
        printf("First matrix \n");
        set(matrix1,row1,col1)
        ;
        printf("Second matrix \n");
        set(matrix2,row2,col2)
        ;
        /* printint matrix */
    //    clrscr();
        printf("       [ First matrix ]\n");
        get(matrix1,row1,col1)
        ;
        printf("       [ Second matrix ]\n");
        get(matrix2,row2,col2)
        ;

        printf("       [ Multiplication Result ]\n");
        result=mul(matrix1,matrix2,row1,col2,col1);
        get(result,row1,col2)
        ;
        printf("\n\t\t Thanks  from debmalya jash");

    //    getch();
    system("pause");

        free(matrix1);
        free(matrix2);
        free(result);

    } /* end main */
     
     
  6. Feb 24, 2006 #5
    Sorry, I meant to mention that I need to convert this to c++ visual .NET. I only learned c++ visual .NET and I'm not very familiar with some of the commands in c++ (even though they are very similar). The way arrays are arranged in that code is very different from how i learned it. Oh, and this is not for an assignment. I'm doing this to impress my girlfriend since she's learning matrices in her math class. (I also want to learn how it works myself) Help would be greatly appreciated.
     
  7. Feb 24, 2006 #6
    if the problem is just getch and clrscr just dump the conio include in there.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: I need help converting from C to C++
  1. Need help about c++ (Replies: 3)

Loading...