-1

The assignment asks us to finish a code that adds matrices up and I'm not too sure what I'm supposed to be returning. It tells me that there's a runtime error but I don't know how to fix it. It will be due pretty soon so someone please help me!

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int** getMatrix(int n, int m);
int** allocateMatrix(int n, int m);
int** addMatrices(int** A, int** B, int n, int m);
void printMatrix(int** A, int n, int m);
void deallocateMatrix(int** A, int n);


// This program reads in two n by m matrices A and B and
// prints their sum C = A + B
//
// This function is complete, you do not need to modify it
// for your homework
int main() {
    int n = 0, m = 0;
    printf("Enter the number of rows and columns: ");
    scanf("%d %d", &n, &m);
    assert(n > 0 && m > 0);
    printf("Enter matrix A:\n");
    int** A = getMatrix(n, m);
    printf("Enter matrix B:\n");
    int** B = getMatrix(n, m);
    int** C = addMatrices(A, B, n, m);
    printf("A + B = \n");
    printMatrix(C, n, m);
    deallocateMatrix(A, n);
    deallocateMatrix(B, n);
    deallocateMatrix(C, n);
}

// Creates a new n by m matrix whose elements are read from stdin
//
// This function is complete, you do not need to modify it
// for your homework
int** getMatrix(int n, int m) {
int** M = allocateMatrix(n, m);
int i, j;

for ( i = 0; i < n; i++) {
    printf("Input row %d elements, separated by spaces: ", i);
    for ( j = 0; j < m; j++) {
        scanf("%d", &M[i][j]);
    }
}
return M;
}

// Allocates space for an n by m matrix of ints
// and returns the result
int** allocateMatrix(int n, int m) {
// Homework TODO: Implement this function
int i, j;
int** L;

L = (int**)malloc(sizeof(int) * n);
for(i = 0; i < n; i++) {
    for(j = 0; j < m; j++){
        L[i] = (int*) malloc(sizeof(int) * m);
    }
}
}

// Adds two matrices together and returns the result
int** addMatrices(int** A, int** B, int n, int m) {
// Homework TODO: Implement this function
int j, i;
int** C;

for(i = 0; i < n; i++) {
    for(j = 0; j < m; j++) {
        C[i][j] = A[i][j] + B[i][j];
    }
}

}

// Prints out the entries of the matrix
void printMatrix(int** A, int n, int m) {
// Homework TODO: Implement this function
int i, j;
int** C;

for (i = 0; i < n; i++) {
  for (j = 0 ; j < m; j++) {
     printf("%d\t", C[i][j]);
  }
  printf("\n");
  }
}

// Deallocates space used by the matrix
void deallocateMatrix(int** A, int n) {
int i;

for ( i = 0; i < n; i++) {
    free(A[i]);
}
free(A);
}
Sara
  • 1
  • 1

2 Answers2

1

A few things:

  • In allocateMatrix you allocate space for n integers, not pointers to integers.

  • In many function you use an uninitialized pointer C.

  • In many function declared to return something, you don't return anything at all.

These things, and probably others, leads to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I just recently learned how to allocate spaces, I don't really understand what I'm supposed to do/use malloc. Also aren't I initializing them with "int** C;"? – Sara Feb 22 '17 at 04:43
  • Should I return like: "return C**;"? and "return L**;"? – Sara Feb 22 '17 at 04:43
  • @Sara A pointer must point somewhere for it to be valid. You must initialize it, or assign to it. And yes you should do e.g. `return L;` in your `allocateMatrix` function. – Some programmer dude Feb 22 '17 at 04:55
  • Where would I point it to and how do I do that? int **C;? I'm sorry for so many questions. I'm not that good at this :/ – Sara Feb 22 '17 at 05:03
  • I know that pointers point to things but I don't know when to use them and why we would need anything to point to anything. – Sara Feb 22 '17 at 05:05
  • @Sara Allocate memory for `C` as well, just like you did for `A` and `B` – Spikatrix Feb 22 '17 at 05:24
  • Now my code is running but it's giving me only 0's. I added in int** C = allocateMatrix(n, m); to all the int** C I had, is the problem in my addMatrices? @CoolGuy – Sara Feb 22 '17 at 05:29
0

Expanding a bit on @Someprogrammerdude's answer

  • You should be allocating room for n integer pointers (int*s) and then in each row, allocate memory for m integers (ints). So, use

    // Allocates space for an n by m matrix of ints
    // and returns the result
    int** allocateMatrix(int n, int m) {
        int i, j;
        int** L;
    
        L = malloc(sizeof(int*) * n);       /* Make `n` rows */
        for(i = 0; i < n; i++) {            /* For each row, */
            L[i] = malloc(sizeof(int) * m); /* Make room for `m` ints */
        }
    
        return L; /* Return the allocated variable. See 3rd point ↓ */
    }
    

    I've removed the casts because they aren't recommended. I leave error checking malloc upto you.

  • You use C uninitialized: int** C;. Allocate memory for it just as you did for A and B:

    int** C = allocateMatrix(n, m);
    
  • You forgot to return variables from functions. Add return L; and return C; at the end of the functions allocateMatrix and addMatrices respectively.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83