为什么这个矩阵点积程序在某些情况下会在最后一列打印地址?
Why does this matrix dot product program print addresses in the last column in some cases?
我编写了这个程序来计算两个矩阵的点积。它在几种情况下工作正常,但在我测试时我注意到它在最后一列中打印了似乎是地址的内容,我无法弄清楚为什么。具体针对以下输入:3 2 3 1 2 1 1 1 2 1 2 3 3 2 1
我正在使用 CodeBlocks 编译和 运行 代码。另外,我对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;
}
你没有在这里分配任何内存:
matrix[i] = calloc(0,columns*sizeof(int));
calloc 的第一个参数设置要分配的元素数。在这种情况下,它应该是 columns
matrix[i] = calloc(columns,sizeof(int));
同时确保您验证了您的 scanf 输入。
我编写了这个程序来计算两个矩阵的点积。它在几种情况下工作正常,但在我测试时我注意到它在最后一列中打印了似乎是地址的内容,我无法弄清楚为什么。具体针对以下输入:3 2 3 1 2 1 1 1 2 1 2 3 3 2 1
我正在使用 CodeBlocks 编译和 运行 代码。另外,我对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;
}
你没有在这里分配任何内存:
matrix[i] = calloc(0,columns*sizeof(int));
calloc 的第一个参数设置要分配的元素数。在这种情况下,它应该是 columns
matrix[i] = calloc(columns,sizeof(int));
同时确保您验证了您的 scanf 输入。