I've been trying to make a matrix calculator for class where the user inputs the details for a matrix (number of rows, columns, etc.) then prints the input. End result's supposed to look like this:
Ex: 2x2 matrix
1 2
3 4
I looked at some of the similar questions like this one: Print a square matrix using pointers but I don't think it's what I'm looking for. This is the code:
int **theAMatrix; switch(choice) { case 1: theAMatrix = (int **)malloc(sizeof(int*)*rowsizeA); for(i = 0; i < rowsizeA; i++) { for(j = 0; j < colsizeA; j++) { theAMatrix[i] = (int*)malloc(sizeof(int)*colsizeA); } } for(i = 0; i < rowsizeA; i++){ for(j = 0; j < colsizeA; j++){ printf("Enter Row: %d Col: %d ", i, j); scanf("%d", &theAMatrix[i][j]); } } printf("Matrix A Complete\n\n"); Menu(); case 2: printf("Matrix A\n"); for(i = 0; i < rowsizeA; i++) { for(j = 0; j < colsizeA; j++){ printf("%d", *(*theAMatrix + i) + j); } printf("\n"); } Menu(); Thing is, the program won't print the matrix when you tell it to. It doesn't crash either, just ends with no fuss. Did I mess up the syntax in some way?
Note: there's a header file with the Menu(), makeChoice(), and getMatrixAValues() functions in it. I already checked it, everything looks good on that front but if anybody thinks something's wrong there I'll post it here too.
[edit] @Joachim this is the code for Menu(), just displays stuff for the user:
int Menu() { printf("**MATRIX MULTIPLICATION CALCULATOR**\n"); printf("[1] Enter Matrix A\n"); printf("[2] Enter Matrix B\n"); printf("[3] Print Matrix A\n"); printf("[4] Print Matrix B\n"); printf("[5] Print Transpose of Matrix A\n"); printf("[6] Print Transpose of Matrix B\n"); printf("[7] (bonus) Change size of Matrix A\n"); printf("[8] (bonus) Change size of Matrix B\n"); printf("[9] Print A x B\n"); printf("[0] Exit\n"); makeChoice(); }
Menu()recursively, it would be better to wrap a loop around the function andbreakout of theswitchafter each case.malloc()in C.case 1:to fall through intocase 2:? There is nobreak;statement. And I don't understand why eachcase:contains a call toMenu(), why isn't that done first?