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

  • C/C++
  • Thread starter UNIQNESS
  • Start date
  • Tags
    C++
In summary: (*(arr1+i)+j)*(*(arr2+k)+col1+k)=result; } } } return result; } /* inverse of matrix multiplication */ int** invert(int** arr1,int** arr2) { int** result; //[edit] int i,j; //[edit] if(arr1!=arr2) { //[edit] result=mul(result,arr1,0,0,row,col-1); //[edit] } else { //[edit] result=mul(result
  • #1
UNIQNESS
18
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
  • #2
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
 
  • #3
And, incidentally, visual studio has a vanilla C compiler. (At least the version with which I'm familiar)
 
  • #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:
// 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 */
 
  • #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.
 
  • #6
if the problem is just getch and clrscr just dump the conio include in there.
 

What is the difference between C and C++?

The main difference between C and C++ is that C is a procedural programming language, while C++ is an object-oriented programming language. This means that C++ allows for the use of classes, inheritance, and polymorphism, while C does not.

What are the benefits of converting from C to C++?

Converting from C to C++ can bring several benefits, such as improved code organization and reusability, better memory management through the use of objects and classes, and access to a wider range of libraries and tools.

What are the challenges of converting from C to C++?

One of the main challenges of converting from C to C++ is the learning curve associated with transitioning from a procedural to an object-oriented programming paradigm. Additionally, C and C++ have different syntax and features, so some code may need to be rewritten or refactored.

Can C code be directly compiled in a C++ compiler?

Yes, C code can be directly compiled in a C++ compiler. C++ is designed to be backwards compatible with C, so most C code should compile without any issues in a C++ compiler. However, there may be some minor differences in syntax and features that need to be addressed.

Are there any tools or resources available for converting from C to C++?

Yes, there are various tools and resources available for converting from C to C++. Some IDEs, such as Visual Studio, have built-in features for converting C code to C++. Additionally, there are online tutorials, books, and forums that can provide guidance and support during the conversion process.

Similar threads

  • Programming and Computer Science
Replies
0
Views
127
  • Programming and Computer Science
Replies
2
Views
172
  • Programming and Computer Science
Replies
1
Views
121
  • Programming and Computer Science
Replies
8
Views
834
  • Programming and Computer Science
Replies
25
Views
14K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
14
Views
5K
Back
Top