gpt4 book ai didi

c - 具有将矩阵乘以标量的函数的程序

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:07 26 4
gpt4 key购买 nike

这是我从《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.)

谢谢!

最佳答案

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

你在 matrix[num_rows][num_columns] 之前忘记了 int

关于c - 具有将矩阵乘以标量的函数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com