简体   繁体   中英

Why does this matrix dot product program print addresses in the last column in some cases?

I have written this program to find the dot product of two matrices. It works fine for several cases, but while I was testing I noticed that it printed what appear to be addresses in the last column and I cant figure out why. Specifically for the following input: 3 2 3 1 2 1 1 1 2 1 2 3 3 2 1

在此处输入图片说明

I am using CodeBlocks to compile and run the code. Also, I am relatively new to C.

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

//Method to allocate memory for a 2D array
int** allocateMatrix(int rows, int columns)
{
    //Declaration of variables
    int **matrix, i;

    //Allocate memory to store 'rows' many pointers to integers
    matrix = malloc(rows*sizeof(int*));

    //For-loop to allocate memory to store 'columns' many integers, and initialize them to zero
    for (i=0; i<rows; i++)
    {
        matrix[i] = calloc(0,columns*sizeof(int));
    }

    return matrix;
}

int main()
{
    //Declaration of variables
    int n, m, p; //used as matrix dimensions
    int i, j, k; //used in for-loops

    //Read input
    do
    {
        //printf("Enter value of rows for first matrix (greater than 0):\n");
        scanf("%d", &n);
    }while(n<0);

    do
    {
        //printf("Enter value of columns for first and rows for second matrix (greater than 0):\n");
        scanf("%d", &m);
    }while(m<0);

    do
    {
        //printf("Enter value of columns for second matrix (greater than 0):\n");
        scanf("%d", &p);
    }while(p<0);

    //Create three matrices, by calling 'allocateMatrix' function
    int** matrix1 = allocateMatrix(n,m);
    int** matrix2 = allocateMatrix(m,p);
    int** matrix3 = allocateMatrix(n,p);

    //Two for-loops to store values in 'matrix1'
    //For-loop to go through rows
    for (i=0; i<n; i++)
    {
        //For-loop to go through columns
        for (j=0; j<m; j++)
        {
            //Read input
            scanf("%d", &matrix1[i][j]);
        }
    }

    //Two for-loops to store values in 'matrix2'
    //For-loop to go through rows
    for (i=0; i<m; i++)
    {
        //For-loop to go through columns
        for (j=0; j<p; j++)
        {
            //Read input
            scanf("%d", &matrix2[i][j]);
        }
    }

    //THREE for-loops to multiply values in 'matrix1' and 'matrix2' and store results in 'matrix3'
    //For-loop to go through rows of 'matrix3' and 'matrix1'
    for (i=0; i<n; i++)
    {
        //For-loop to go through columns of 'matrix3' and 'matrix2'
        for (j=0; j<p; j++)
        {
            //For-loop to go through columns of 'matrix1' and rows of 'matrix2'
            for(k=0; k<m; k++)
            {
                //Read input
                matrix3[i][j] = matrix3[i][j] + (matrix1[i][k] * matrix2[k][j]);
            }
        }
    }

    //Print the resulting matrix
    //Two for-loops to print values in 'matrix3'
    //For-loop to go through rows
    for (i=0; i<n; i++)
    {
        //For-loop to go through columns
        for (j=0; j<p; j++)
        {
            if (j==p-1) //for correct format
            {
                printf("%d", matrix3[i][j]);
            }
            else
            {
                printf("%d ", matrix3[i][j]);
            }
        }
        //Change line
        printf("\n");
    }

    //Free the memory up!!!!
    free(matrix1);
    free(matrix2);
    free(matrix3);

    return 0;
}

You don't allocate any memory here:

matrix[i] = calloc(0,columns*sizeof(int));

The first parameter to calloc sets the number of elements you want to allocate. In this case it should be columns

matrix[i] = calloc(columns,sizeof(int));

Also made sure you validate your scanf input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM