2015-09-25 73 views
1

这是我从一本书中用C语言编写的程序(一字一字,一字母一字母,一个特殊字符)但是我的编译器不能编译它(Xcode 4.6.3,终端2.2.3)。程序的功能是将一个矩阵乘以一个标量

这是程序和我得到的以下错误消息。我不明白为什么我会收到这些错误消息。

// program to multiply two-dimensional matrix by a scalar 

#include <stdio.h> 

void scalar_multiply (int num_rows, int num_columns, int matrix[num_rows][num_columns], int scalar); 
void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns]); 

int main(void) 
{ 
    int sample_matrix[3][5] = 
    { 
     { 7, 16, 55, 13, 12}, 
     {12, 10, 52, 0, 7}, 
     {-2, 1, 2, 4, 9} 
    }; 

    printf("Original matrix:\n"); 
    display_matrix(3, 5, sample_matrix); 

    scalar_multiply(3, 5, sample_matrix, 2); 

    printf("\nMultiplied by 2:\n"); 
    display_matrix(3, 5, sample_matrix); 

    scalar_multiply(3, 5, sample_matrix, -1); 

    printf("\nMultiplied by -1:\n"); 
    display_matrix(3, 5, sample_matrix); 

    return 0; 
} 

// function to multiply a matrix by a scalar 
void scalar_multiply (int num_rows, int num_columns, int matrix[num_rows][num_columns], int scalar) 
{ 
    int row, column; 

    for (row = 0; row < num_rows; row++) 
     for (column = 0; column < num_columns; column++) 
      matrix[row][column] *= scalar; 
} 

void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns]) 
{ 
    int row, column; 

    for (row = 0; row < num_rows; row++) 
    { 
     for (column = 0; column < num_columns; column++) 
      printf("%5i", matrix[row][column]); 

     printf("\n"); 
    } 
} 

错误消息:

prog7-14.c:6: error: expected declaration specifiers or ‘...’ before ‘matrix’ 
prog7-14.c: In function ‘main’: 
prog7-14.c:18: error: too many arguments to function ‘display_matrix’ 
prog7-14.c:23: error: too many arguments to function ‘display_matrix’ 
prog7-14.c:28: error: too many arguments to function ‘display_matrix’ 
prog7-14.c: At top level: 
prog7-14.c:42: error: expected declaration specifiers or ‘...’ before ‘matrix’ 
prog7-14.c: In function ‘display_matrix’: 
prog7-14.c:49: error: ‘matrix’ undeclared (first use in this function) 
prog7-14.c:49: error: (Each undeclared identifier is reported only once 
prog7-14.c:49: error: for each function it appears in.) 
Allas-MacBook-Pro:ch7 Alla$ touch prog7-14.c 
Allas-MacBook-Pro:ch7 Alla$ gcc prog7-14.c -o prog7-14 
prog7-14.c:6: error: expected declaration specifiers or ‘...’ before ‘matrix’ 
prog7-14.c: In function ‘main’: 
prog7-14.c:18: error: too many arguments to function ‘display_matrix’ 
prog7-14.c:23: error: too many arguments to function ‘display_matrix’ 
prog7-14.c:28: error: too many arguments to function ‘display_matrix’ 
prog7-14.c: At top level: 
prog7-14.c:42: error: expected declaration specifiers or ‘...’ before ‘matrix’ 
prog7-14.c: In function ‘display_matrix’: 
prog7-14.c:49: error: ‘matrix’ undeclared (first use in this function) 
prog7-14.c:49: error: (Each undeclared identifier is reported only once 
prog7-14.c:49: error: for each function it appears in.) 

谢谢!

回答

3

void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns]);

必须

void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns]); 

你忘intmatrix[num_rows][num_columns]

1

虽然宣布原型和定义你错过了之前matrix[num_rows][num_columns]键入intdisplay_matrix功能。

这是生产错误:太多的论据display_matrix

  1. display_matrix原型进行更改。

    void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns]); 
    
  2. 更新类似的更改为display_matrix的定义。

    void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns]) 
    { 
        int row, column; 
        .... rest of the code 
    } 
    
相关问题