使用函数将程序与标量相乘的程序

时间:2015-09-25 09:31:08

标签: c function

这是我输入的一个程序(逐字逐句,写字母,特殊字符的特殊字符)来自C编程。但是我的编译器没有编译它(Xcode 4.6.3,Terminal 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.)

谢谢!

2 个答案:

答案 0 :(得分:3)

  

List<String> favoredNodes

必须是

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]);

之前忘记了int

答案 1 :(得分:1)

声明原型和定义 display_matrix函数时,您错过了在int之前键入matrix[num_rows][num_columns]

这会产生错误: 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
    }