0

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(); } 
10
  • could you add the definition of theAMatrix Commented Oct 8, 2015 at 9:41
  • 2
    Instead of calling the Menu() recursively, it would be better to wrap a loop around the function and break out of the switch after each case. Commented Oct 8, 2015 at 9:43
  • 1
    Please don't cast the return value of malloc() in C. Commented Oct 8, 2015 at 9:44
  • 1
    Why don't you use the "AMatrix[i][j]" for printing Commented Oct 8, 2015 at 9:52
  • 1
    Did you intend case 1: to fall through into case 2:? There is no break; statement. And I don't understand why each case: contains a call to Menu(), why isn't that done first? Commented Oct 8, 2015 at 9:57

1 Answer 1

1

Don't know if it is the real problem, but logically, you need to add break; statements after the case.

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(); // You need a break; statement here!!! 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(); // And here as well 

If that is deliberately done, then I think the way I see it, you call menu() in main() and the switch-case you have written is part of that makeChoice() function. And if it is so, then may be you can share the snippet where the variable choice is getting updated. Can be a silly scanf issue.

Did you bother to have a default: in the switch case by the way? You can use that to print debug messages just to know if your choice is being set properly or not.

Additionally, your printf("%d", *(*theAMatrix + i) + j); should be printf("%d", *(*(theAMatrix + i) + j);

Sign up to request clarification or add additional context in comments.

Comments