【问题标题】:Placing an integer unto a two dimension array (matrix)将整数放入二维数组(矩阵)
【发布时间】:2016-07-12 13:43:10
【问题描述】:

好的,伙计们,我正在尝试制作一个程序,您可以在其中猜测您输入的数字随机放置到矩阵或二维数组中的位置。 例如,用户输入了“3”,因此整数将被放置在矩阵的随机行和列上。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define R 4
#define C 4
void display(char a[][C]);
void placing(char b[][C]);
void guess(int m, int n);

void main(){
int x=0,y=0,m,n,number;
int matrix[R][C];
char a[R][C]={{'x','x','x','x'},{'x','x','x','x'},{'x','x','x','x'},{'x','x','x','x'}};
printf("\t\t\t      Welcome to my game");
printf("\nEnter your number: ");
scanf("%d",&number);
display(a);
placing(a);
printf("\n\nYour number is now being placed at a random location");
printf("\n\nGuess where your number is located (row) (column)");
printf("\n\nEnter coordinates: ");
scanf("%d%d",&m,&n);

getch();
}

void display(char a[][C]){
for(int row=0; row<R; row++)
{
    printf ("\n");
    for(int column=0; column<C; column++)
    {
    printf ("\t%c\t", a[row][column]);
    }
}
}

void placing(int b[][C]){
int randomrow=0,randomcolumn=0;
int i=0;

randomrow = rand()%4;
randomcolumn = rand()%4;

for(int row=0; row < randomrow; row++){
    for(int column=0;column < randomcolumn; column++, i++){
        b[row][column];
    }
}
printf("%d",b[R][C]);
}

这是整个程序。有什么建议吗?

【问题讨论】:

  • 而且它也不完整,所以 b[row][column] 行仍然缺少代码。
  • 这个程序与“猜一个四分之一的数字”有何不同?它与矩阵有什么关系?
  • C++ C,选一个该死的。
  • @Steven 输入的号码在哪里?
  • 我不确定你想要什么...数组中的其他元素会被填充什么?你想让人们猜两个数字,看看它们是否都是正确的而不是一个?输入的数字 3 与它有什么关系?二维数组真的有必要吗?

标签: c arrays matrix


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int matrix[4][4] = 
    {
        {-1,-1,-1,-1},
        {-1,-1,-1,-1},
        {-1,-1,-1,-1},
        {-1,-1,-1,-1},
    };

    int user_input;
    int random_row;
    int random_column;
    int row_guess;
    int col_guess;

    printf("Enter a number to add: \n");
    scanf("%d", &user_input);
    printf("User entered: %d\n", user_input);

    srand(time(NULL)); 

    random_row = rand() % 4;
    random_column = rand() % 4;

    printf("random_row: %d\n", random_row);
    printf("random_column: %d\n", random_column);

    matrix[random_row][random_column] = user_input;

    for(int row = 0; row < 4; row++)
    {
        for(int column = 0; column < 4; column++)
        {
            printf("%d ", matrix[row][column]);
        }
        printf("\n");
    }

    printf("Guess Random Row: \n");
    scanf("%d", &row_guess);
    printf("User Row Guess: %d\n", row_guess);

    printf("Guess Random Column: \n");

    scanf("%d", &col_guess);
    printf("User Column Guess: %d\n", col_guess);

    if (matrix[row_guess][col_guess] == user_input)
    {
        printf("You guessed correctly!\n");
    }
    else
    {
        printf("You guessed incorrectly\n");
    }

    return 0;
}

【讨论】:

  • +1 我的矩阵是一个包含 16 个 x 的 char[4][4]。给定您的代码,是否可以做同样的事情?还是我只是要把它作为一个整数?我的代码在顶部,我已对其进行了编辑。
  • @Steven 元素的类型并不重要。
  • @Steven 你需要调用 srand(time(NULL)) 为你的 rand() 提供不同的值man7.org/linux/man-pages/man3/rand.3.html