简体   繁体   English

在C中打印和填充2D数组

[英]Printing and filling a 2D array in C

ORIGINAL POST: 原始帖子:
Okay, so this is an exercise I worked through and understand how it works quite well, however I have an assignment where I have to fill an array with random numbers and then have it print out, as well as be able to search the array for a particular number (query) and print the number in inputted location. 好的,这是我练习过的一个练习,并了解了它的工作原理,但是我要分配一个任务,我必须用随机数填充数组,然后打印出来,并能够在数组中搜索特定号码(查询)并在输入的位置打印该号码。
The array needs to be 5 rows, 10 columns, so I know that in my definitions I need to change the 4 and 5 accordingly. 该数组需要为5行10列,因此我知道在定义中需要相应地更改4和5。 However, I am not sure how to generate a random number per place in the array. 但是,我不确定如何在数组中的每个位置生成随机数。 I know the following has to be used somewhere, but I'm not quite sure how or where in the code it should be. 我知道以下内容必须在某个地方使用,但是我不太确定代码应如何或在何处使用。 I also have to use functions...and generate a menu for the user to choose actions from, one for fill, print, query, and terminate. 我还必须使用函数...并生成一个菜单,供用户从中选择操作,其中一个用于填充,打印,查询和终止。 I obviously need to replace the locations within the array with a variable in order to make the locations dependent on the random number generator while still making sure they're random and not all the same. 我显然需要用变量替换数组中的位置,以使位置取决于随机数生成器,同时仍要确保它们是随机的并且不完全相同。 (0-999) (0-999)

EDITED POST: 编辑后:

Okay, so I have now been able to get the array to fill and store random numbers, I need to adjust the values accordingly to have them match the dimensions of the array and the range, but it's a start. 好的,现在我已经能够使数组填充和存储随机数,我需要相应地调整值以使其与数组的尺寸和范围相匹配,但这是一个开始。 However, I need to now create a menu that I can prompt the user to enter a character and perform a task such as: 但是,我现在需要创建一个菜单,以提示用户输入字符并执行诸如以下的任务:

MENU 菜单

Select one of the following options: 选择以下选项之一:

F. Fill the array with a random number series. F.用随机数序列填充数组。

P. Print the array. P.打印阵列。

Q. Query the array. 问:查询数组。

Z. Terminate the program. Z.终止程序。

and ultimately an 'M' to recall the menu 最后是一个“ M”来调用菜单

SOME PARAMETERS: 一些参数:

-- The fill option will fill an array of 50 integers with random numbers in the range 1...999. -fill选项将填充一个由50个整数组成的数组,其中的随机数范围为1 ... 999。

**Each time the fill option is executed, it will fill the array with a new random number series. **每次执行填充选项时,都会使用新的随机数序列填充数组。

-- Print the array will print the numbers to the screen. -打印阵列会将数字打印到屏幕上。

-- Query will ask the user to enter a location (index), and will then print the number in that location. -查询将要求用户输入一个位置(索引),然后在该位置打印数字。 If the user enters an invalid location, it will print a message stating so. 如果用户输入无效的位置,它将打印一条消息,说明是这样。

EDITED CODE: 编辑代码:

/* Generate a random number permutation.*/

 #include < stdio.h >

 #include < stdlib.h >

 #define ARY_SIZE 20

//Function Declarations

void bldPerm (int randomNos[]);

void printData (int data[], int size, int lineSize);

int main(void)

{

//Local Declarations

    int randNos[ARY_SIZE];

//Statements

    printf("Begin Random Numbers Permutation Generation\n");

    bldPerm (randNos);

    printData (randNos, ARY_SIZE, 10);

    return 0;
}//main

/*=====================bldPerm======================*/


void bldPerm(int randNos[])

{

//LD's

    int oneRandNo;

    int haveRand[ARY_SIZE] = { 0 };


//Statements

    for (int i = 0; i < ARY_SIZE; i++)

        {

        do

            {
            oneRandNo = rand() % ARY_SIZE;

        } while (haveRand[oneRandNo] == 1);

        haveRand[oneRandNo] = 1;

        randNos[i] = oneRandNo;

    } //for

    return;

}//bldPerm

/*=====================printData======================*/

void  printData(int data[], int size, int lineSize)

{

//LD's

    int numPrinted = 0;

//Statements

    printf(" \n ");

    for (int i = 0; i < size; i++)

        {

        numPrinted++;

        printf(" %2d ", data[i]);

        if (numPrinted >= lineSize)

            {

            printf(" \n ");

            numPrinted = 0;

            }//if

        }//for

    printf(" \n ");

    return;

}//printData

I think this is the wrong forum for this kind of questions but anyway. 我认为这是针对此类问题的错误论坛,但无论如何。

First try: 第一次尝试:

1) Remove the initialization of the quizScores 2) Initialize the seed using srand(time(NULL)); 1)删除quizScores的初始化2)使用srand(time(NULL))初始化种子; Remember to include time.h 2) Make a double for loop for initializing the elements using rand()%100. 记住要包括time.h 2)创建一个double for循环,以使用rand()%100初始化元素。 This will give random numbers between 0 and 100. 这将给出0到100之间的随机数。

With the menu, I would suggest using a while look and query for actions using getchar() 通过菜单,我建议您使用一会儿外观并使用getchar()查询操作

Here's a simple start to all of your array issues. 这是所有阵列问题的简单开始。 The menu you have to implement should deal with a switch statement and then its corresponding function call. 您必须实现的菜单应该处理switch语句,然后处理其相应的函数。

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

bool searchArray(int *A, int m, int n, int key){
    int i, j;
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            if ( (*((A+i*n)+j)) == key )
                return true;
    return false;
}

void printArray(int *A, int m, int n){
    int i, j;
    for (i = 0; i < m; i++){
        for (j = 0; j < n; j++){
            printf("(%d) ", *((A+i*n)+j));
        }
        printf("\n");
    }
}

void fillArray(int *A, int m, int n){
    int i, j;
    time_t t;
    srand(time(&t));

    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            *((A+i*n)+j) = rand() % 100;
}

int main(){
    int m = 5, n = 10, key = 10, A[m][n];
    bool found = false;

    fillArray((int *)A, m, n);
    printArray((int *)A, m, n);
    found = searchArray((int *)A, m, n, key);

    if ( found == true ){
        printf("%d was in the array!\n", key);
    } else {
        printf("%d was not in the array!\n", key);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM