我正在尝试完成一个研讨会,但在 if 语句中调用 inputVector 函数后出现分段错误。我不知道为什么它给我一个错误。编译时没有警告,对通过的数组的测试实际上显示了记录的内容。我正在运行 OSX Maverick,但我也在 linux 系统上进行了测试,但无济于事。如果有人能对此有所启发,我将不胜感激。
#include <stdio.h>
#include "matrix.h"
int main() {
int row = 0 , col = 0, option = 0;
int vector[9], result[9];
int matrix[9][9];
do{
printf("Number of rows (0 to exit) ? ");
scanf("%d", &row);
if(row != 0) {
printf("Number of columns ? ");
scanf("%d", &col);
inputMatrix(row, col, matrix);
printf("Pre (0) or Post (1) Multiply ? ");
scanf("%d", &option);
if(option == 0){
inputVector(row, vector);
printf("TESTING 1");
preMulti(row, col, matrix, vector, result);
printf("TESTING 2");
display(col, result);
}
else if(option == 1){
inputVector(col, vector);
postMulti(row, col, matrix, vector, result);
display(row, result);
}
return 0;
}// END IF
} while( row != 0 );// End While
} // End Main
矩阵.cpp
#include "matrix.h"
#include <stdio.h>
void inputMatrix(int row , int col, int matrix[][MAXCOL]) {
for ( int i = 0; i < row; i++) {
printf("Row %d ? ", i + 1);
for ( int k = 0; k < col; k++)
scanf("%d", &matrix[i][k]);
}
}
void inputVector(int size, int vector[]) {
printf("Vector? ");
for(int i = 0; i < size; i++){
scanf("%d" , &vector[i]);
printf(" Recorded %d \n", vector[i]);
}
printf("TESTING 3");
}
void preMulti(int row, int col, int matrix[][MAXCOL], int vector[], int result[]) {
for(int i = 0; i < col; i++ ) {
result[i] = 0;
for( int k = 0; k < row; i++)
result[i] += vector[k] * matrix[k][i];
}
}
void postMulti(int row, int col, int matrix[][MAXCOL], int vector[], int result[]) {
for( int i = 0; i < row; i++ ) {
result[i] = 0;
for( int k = 0; k < col; i++)
result[i] += vector[k] * matrix[i][k];
}
}
void display(int size, int vector[]) {
printf("Result: ");
for ( int i = 0; i < size; i++)
printf("%d ", vector[i]);
}