C/C++ How can I convert C code to C++ in Visual Studio 2003 .NET?

  • Thread starter Thread starter UNIQNESS
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
Converting C code to C++ in Visual Studio 2003 .NET can be challenging due to differences in syntax and library functions. The original C code provided has several issues, including the use of Borland-specific functions like `clrscr` and `getch`, which are not standard. C++ is generally compatible with C, but some constructs may require minor adjustments. The main recommendation is to replace non-standard functions and ensure proper memory management using C++ features like `new` and `delete` instead of `malloc` and `free`. Overall, understanding these differences will aid in successfully converting the code.
UNIQNESS
Messages
18
Reaction score
0
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:
Technology news on Phys.org
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
 
And, incidentally, visual studio has a vanilla C compiler. (At least the version with which I'm familiar)
 
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:
// 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 */
 
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.
 
if the problem is just getch and clrscr just dump the conio include in there.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top