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

  • Context: C/C++ 
  • Thread starter Thread starter UNIQNESS
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around converting C code to C++ for use in Visual Studio 2003 .NET. Participants explore the challenges of adapting the code, which includes matrix operations, and share their experiences with the differences between C and C++ syntax and functionality.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant requests help converting a specific C code snippet to C++ for Visual Studio 2003 .NET, expressing a desire to learn and impress someone.
  • Another participant suggests that the original code is poorly written and highlights that C++ is a superset of C, implying that ANSI C code should compile with a C++ compiler.
  • A different participant notes that Visual Studio includes a C compiler and mentions that minor changes were needed to compile the code under gcc, specifically pointing out the use of Borland extensions that are not standard.
  • One participant provides a modified version of the code, indicating that it compiles with adjustments, and warns against using Visual C++ 6.0 due to compatibility issues with standard C++.
  • Another participant suggests simply removing the conio include to resolve issues related to specific functions like getch and clrscr.

Areas of Agreement / Disagreement

Participants express differing views on the quality of the original code and the necessary steps for conversion. There is no consensus on the best approach to take, and multiple competing views remain regarding how to handle specific functions and compatibility issues.

Contextual Notes

Some participants mention that certain functions used in the original code are not part of the standard language, which may complicate the conversion process. Additionally, there are references to potential issues with older versions of Visual C++ and the need for service packs.

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.
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
47
Views
5K
  • · Replies 16 ·
Replies
16
Views
5K