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();
}
Community
  • 1
  • 1
  • could you add the definition of theAMatrix – Itay Sela Oct 08 '15 at 09: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. – M Oehm Oct 08 '15 at 09:43
  • I assume that the code you show is in a function called `Menu`? If so, don't call it recursively, that causes the new function to have all new local variables. Instead use a loop. – Some programmer dude Oct 08 '15 at 09:43
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Oct 08 '15 at 09:44
  • Is it possible that the allocation of the matrix is buggy? The implementation uses `rowsizeA*colsizeA` calls of `malloc`, while to my understanting it should be either `rowsizeA`or `colsizeA`, depending on whether the matrix is to be stored row-wise or column-size. – Codor Oct 08 '15 at 09:48
  • 1
    Why don't you use the "AMatrix[i][j]" for printing – Rahul Jha Oct 08 '15 at 09:52
  • so just theAMatrix = malloc(sizeof(int *)*rowsizeA)? –  Oct 08 '15 at 09: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? – Weather Vane Oct 08 '15 at 09:57
  • I've just compiled the code in VS2006 and it worked. – UpmostScarab Oct 08 '15 at 09:59
  • change `printf("%d", *(*theAMatrix + i) + j);` to `printf("%d", *(*(theAMatrix + i) + j));` or `printf("%d", theAMatrix[i][j]);` – BLUEPIXY Oct 08 '15 at 10:02

1 Answers1

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);

WedaPashi
  • 3,561
  • 26
  • 42