在C中打印数组的随机元素

时间:2017-01-31 23:45:07

标签: c arrays function output

我编写了一个包含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);
    }
}

3 个答案:

答案 0 :(得分:0)

编译器抱怨函数USE [DATABASENAME] IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DBO].[TABLENAME] ') AND type in (N'U') ) DROP TABLE [DBO].[TABLENAME] SELECT * into [DBO].[TABLENAME] FROM OPENQUERY(QUICKBOOKS, 'SELECT * FROM Invoice') 的隐式声明,因为您在之前声明/定义它之前调用它。在PopulateRandom中调用之前,只需定义PopulateRandom即可。 对此,您可能会发现this很有趣。 第二个问题可能是由这行代码引起的:

PrintArray2D

如果要连接更多条件,则应使用逻辑和(&amp;&amp;)或/和逻辑或(||)。

你使用while循环的方式使得它不正确。 while循环不是for循环,语法不同。

for-loop语法:

while(i = 0; i < M*N; i++)

while循环语法:

for (init; condition; increment)

我能看到的其他问题是:

  • 开关盒: 您应该放入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 []

答案 1 :(得分:0)

在此功能中

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

变量rowcolumni未初始化

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语句中,您应附加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 

答案 2 :(得分: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);
    }
}