好的,所以大家好我试着制作一个程序,你可以猜到输入的数字随机放在矩阵或二维数组中。 例如,用户输入了' 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]);
}
这是整个计划。任何提示?
答案 0 :(得分:0)
#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;
}