[英]Printing random elements of an array in C
我編寫了一個程序,該程序具有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
的隱式聲明,因為您在聲明/定義它之前先調用它。 在PrintArray2D
調用PopulateRandom
之前,只需對其進行定義。 關於這一點,您可能會發現這很有趣。 第二個問題可能是由以下代碼行引起的:
while(i = 0; i < M*N; i++)
如果要連接更多條件,則應使用邏輯和(&&) 或/和邏輯或(||)。
您使用此while循環的方式使該方法不合適。 while循環不是for循環,語法不同。
for循環語法:
for (init; condition; increment)
While循環語法:
while (cond1 && cond2 || cond3)
我可以看到的其他問題是:
default
,以防萬一。 另外,您總是必須打破案件。 您沒有打破case 1
。 switch statement
一般語法:
switch (x)
{
case 1 :
break;
case n :
break;
default:
break;
}
在PopulateRandom
:
int array[int M * int N]
應該
int array []
要么
int *array
在此功能
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];
}
變量row
, column
和i
未初始化
int array[M*N], row, column, i;
^^^^^^^^^^^^^^
但是同樣地,它們在循環中被相應地使用
while(row <= M && column <= N){
在這個表達陳述中
array[i] = PopulateRandom(array[M * N]);
此外,在此語句中,尚未聲明的函數調用后綴表達式中使用了名稱PopulateRandom
。 此外,在此表達式array[M * N]
中,嘗試訪問數組之外的內存。
目前尚不清楚此魔術數字4在此聲明中的含義
if(column == 4){
^^^
也完全不清楚帶有未初始化變量i
return語句的含義是什么
return array[i];
此函數聲明
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 PopulateRandom(int M, int N, int array[M * N]){
//...
同樣,不清楚return語句的目的
return array[i];
嘗試返回數組中不存在的元素。
在標簽case 1:
的switch語句中case 1:
您應該附加break
語句。
可以通過以下方式編寫循環
do
{
printf("If you would like to search an array, enter 1 \n: ");
// ^^^
printf("If you would like to exit, enter 0 \n: ");
option = 0;
scanf("%d", &option);
switch( option )
{
case 1:
{
printf("Enter two numbers M and N: ");
scanf("%d %d", &M, &N);
int a[M][N];
PrintArray2D( M, N, a );
break;
}
}
} while ( option != 0 );
函數本身可以按照下面的演示程序中所示編寫
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void PopulateRandom( int m, int n, int a[static const m * n] )
{
const int N = m * n;
srand( ( unsigned int )time( NULL ) );
for( int i = 0; i < N; i++ ) a[i] = rand() % N;
}
void PrintArray2D( int m, int n, int a[static const m * n] )
{
PopulateRandom( m, n, a );
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ )
{
printf( "%3d ", a[i * n + j] );
}
printf( "\n" );
}
}
int main(void)
{
int m = 2;
int n = 3;
int a[m * n];
PrintArray2D( m, n, a );
return 0;
}
程序輸出可能看起來像
1 2 2
3 5 0
這是我在這個論壇上的第一個答案。 對我的解釋或更正要保持溫柔。 祝你今天愉快 ! (這只是簡單的評論編)
#include <stdio.h>
#include <stdlib.h>
/*
-You need to use prototype or just set function "int PopulateRandom" before "int PrintArray2D"
-Try to change your way to process at this application, it's really so complicated.
-Try to use debug for understand what is going on your program.
*/
int PrintArray2D(int M, int N){
int array[M*N], row, column, i; // Use "static int" for row, column and i for auto init at 0, or do it manually.
while(row <= M && column <= N){ // Like i have said in top, you use variable but they're not initialized
for(row = 0; row <= M; row++){
for(column = 0; column <= N; column++){
array[i] = PopulateRandom(array[M * N]); // use directly "array[i] = (int) rand();" for change with random value
printf("%d", array[i]); // you can directly print a random value, you save value, but when you leave this function, your array is destroy.
if(column == 4){
break;
}
}
column = 0;
printf("\n");
}
}
return array[i]; // return is useless for your usage, becaus you don't catch return value in your main. (void PrintArray2D if no return)
}
/*
-Your function need other params : int array[], int M, int N (don't forget to send M and N when you call this function)
-You need to change your algo, because this function take M and N for change just ONE element of this array.
-This function is useless in this context, check comment in "PrintArray2D" for more informations
*/
int PopulateRandom(int array[int M * int N]){
int i;
while(i = 0; i < M*N; i++){ // It's a FOR and not WHILE
array[i] = rand() % (M*N);
}
return array[i];
}
/*
-Duplicate code isn't good, you can use printf and scanf just one time, just after your while, and set option = 1 before.
-Donc use case, but just an if, because your condition in your while check the value.
*/
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.