【发布时间】:2017-01-31 23:45:06
【问题描述】:
我编写了一个包含 M*N 个元素的程序,其中变量 M 和 N 由用户输入。该程序打印 M x N 表中的元素,并为每个元素分配随机值。当我尝试编译程序时,不断弹出两个错误。第一条消息说在第 12 行有一个函数“PopulateRandom”的隐式声明。第二条消息说在第 25 行的“int”之前有一个缺少的表达式。
#include <stdio.h>
#include <stdlib.h>
// Prints elements of array in a M x N table
int PrintArray2D(int M, int N){
int array[M*N], row, column, i;
while(row <= M && column <= N){
for(row = 0; row <= M; row++){
for(column = 0; column <= N; column++){
array[i] = PopulateRandom(array[M * N]);
printf("%d", array[i]);
if(column == 4){
break;
}
}
column = 0;
printf("\n");
}
}
return array[i];
}
//Assigns elements of the array "array" random values
int PopulateRandom(int array[int M * int N]){
int i;
while(i = 0; i < M*N; i++){
array[i] = rand() % (M*N);
}
return array[i];
}
int main(void){
int option, M, N;
printf("If you would like to search ann array, enter 1 \n: ");
printf("If you would like to exit, enter 0 \n: ");
scanf("%d", &option);
while(option != 0){
switch(option){
case 1: if(option == 1){
printf("Enter two numbers M and N: ");
scanf("%d %d", &M, &N);
PrintArray2D(M, N);
}
case 0: if(option == 0){
break;
}
}
printf("If you would like to search ann array, enter 1 \n: ");
printf("If you would like to exit, enter 0 \n: ");
scanf("%d", &option);
}
}
【问题讨论】:
-
PopulateRandom没有函数原型(前向引用),因此编译器只能对其参数和返回类型进行假设。 -
... 和
array[int M...不正确。你的意思是int PopulateRandom(int array[M * N])? -
对不起,你指的是哪一行?
-
@Shoaib Ahmed 二维数组在哪里?:)