我正在尝试制作一个程序来在矩阵中找到鞍点。当我尝试运行时,编译器显示错误:[链接器错误]未定义引用min_cols(float (*) [6], float, int, int, int)' , [Linker error] undefined reference to
max_rows(float(*)[6],float,int,int,int)'
那是我的代码
#include <stdio.h>
#define M 8
#define N 6
void main (){
void enter_matrix (float p[M][N], int r, int c);
void print_matrix (float p[M][N], int r, int c);
void min_cols (float p[M][N],float ,int , int r, int c);
void max_rows (float p[M][N],float mr,int mri, int r, int c);
int saddle_points (float max_row[N],int mrow_i[N],float min_col[N],int mcol_i[N],int r,int c);
int m, n,mrow_i[M],mcol_i[N],flag;
int *mri,*mci;
float matrix[M][N],max_row[M],min_col[N];
float (*p) [M][N],*mr,*mc;
p=&matrix;mr=max_row;mc=min_col;
mri=mrow_i;mci=mcol_i;
do{
printf("Please enter matrix size(max 8 by 6):'\n ");
printf("Enter number of rows M=");
scanf("%d",&m);
printf("Enter number of columns N=");
scanf("%d",&n);
}
while((m>M||m<=0)||(n>N||n<=0));
enter_matrix(*p,m,n);
print_matrix(*p,m,n);
min_cols(*p,min_col[N],mcol_i[N],m,n);
max_rows(*p,max_row[M],mrow_i[M],m,n);
printf("Saddle points:\n");
flag=saddle_points (max_row,mrow_i,min_col,mcol_i,m,n);
if(flag==0){
printf("There are no saddle points");
}
}
void enter_matrix (float p[M][N], int r, int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("Enter %d %d element of matrix: ",i+1,j+1);
scanf("%f",&p[i][j]);
}
}
}
void print_matrix (float p[M][N], int r, int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%f \t",p[i][j]);
}
printf("\n");
}
}
void min_cols (float p[M][N],float min_col[N],int mcol_i[N],int r, int c){
int i,j,*mci;float min,*mc;
mc=min_col;mci=mcol_i;
for(i=0;i<r;i++){
min=p[i][0];
for(j=0;j<c;j++){
if(p[i][j]<min){
*(mc+i)=p[i][j];
*(mci+i)=j;
}
}
}
}
void max_rows (float p[M][N],float max_row[M],int mrow_i[M], int r, int c){
int i,j,*mri;float max,*mr;
mr=max_row;mri=mrow_i;
for(j=0;j<c;j++){
max=0;
for(i=0;i<r;i++){
if(p[i][j]>max){
*(mr+j)=p[i][j];
*(mri+j)=j;
}
}
}
}
int saddle_points (float max_row[N],int mrow_i[N],float min_col[N],int mcol_i[N],int r,int c){
int i,j,flag=0,*mri,*mci; float *mr,*mc;
mr=max_row;mri=mrow_i;mc=min_col;mci=mcol_i;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(*(mr+i)==*(mc+j)){
printf("Saddle point at row %d, col %d value:%f'n",*(mri+i),*(mci+i),*(mr+i));
flag=1;
}
}
return flag;
}
}
答案 0 :(得分:0)
找到解决方案我应该在main之前放置函数的原型,这是工作代码:
#include <stdio.h>
#define M 8
#define N 6
void enter_matrix (float p[M][N], int r, int c);
void print_matrix (float p[M][N], int r, int c);
void min_cols (float p[M][N],float min_col[N],int mcol_i[N],int r, int c){
int i,j,*mci;float min,*mc;
mc=min_col;mci=mcol_i;
for(i=0;i<r;i++){
min=p[i][0];
for(j=0;j<c;j++){
if(p[i][j]<min){
*(mc+i)=p[i][j];
*(mci+i)=j;
}
}
}
}
void max_rows (float p[M][N],float max_row[M],int mrow_i[M], int r, int c){
int i,j,*mri;float max,*mr;
mr=max_row;mri=mrow_i;
for(j=0;j<c;j++){
max=0;
for(i=0;i<r;i++){
if(p[i][j]>max){
*(mr+j)=p[i][j];
*(mri+j)=j;
}
}
}
}
int saddle_points (float max_row[N],int mrow_i[N],float min_col[N],int mcol_i[N],int r,int c);
void main (){
int m, n,mrow_i[M],mcol_i[N],flag;
int *mri,*mci;
float matrix[M][N],max_row[M],min_col[N];
float (*p) [M][N],*mr,*mc;
p=&matrix;mr=max_row;mc=min_col;
mri=mrow_i;mci=mcol_i;
do{
printf("Please enter matrix size(max 8 by 6):'\n ");
printf("Enter number of rows M=");
scanf("%d",&m);
printf("Enter number of columns N=");
scanf("%d",&n);
}
while((m>M||m<=0)||(n>N||n<=0));
enter_matrix(*p,m,n);
print_matrix(*p,m,n);
min_cols(*p,min_col,mcol_i,m,n);
max_rows(*p,max_row,mrow_i,m,n);
printf("Saddle points:\n");
flag=saddle_points (max_row,mrow_i,min_col,mcol_i,m,n);
if(flag==0){
printf("There are no saddle points");
}
}
void enter_matrix (float p[M][N], int r, int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("Enter %d %d element of matrix: ",i+1,j+1);
scanf("%f",&p[i][j]);
}
}
}
void print_matrix (float p[M][N], int r, int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%f \t",p[i][j]);
}
printf("\n");
}
}
int saddle_points (float max_row[N],int mrow_i[N],float min_col[N],int mcol_i[N],int r,int c){
int i,j,flag=0,*mri,*mci; float *mr,*mc;
mr=max_row;mri=mrow_i;mc=min_col;mci=mcol_i;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(*(mr+i)==*(mc+j)){
printf("Saddle point at row %d, col %d value:%f'n",*(mri+i),*(mci+i),*(mr+i));
flag=1;
}
}
return flag;
}
}