gpt4 book ai didi

创建一个幸运数字猜谜游戏(将数组传递给函数/调用函数)

转载 作者:行者123 更新时间:2023-11-30 14:23:15 25 4
gpt4 key购买 nike

我在玩猜谜游戏时遇到了一些麻烦,该游戏用随机整数填充数组,然后让用户尝试猜测数组中的数字。我相信我的大部分内容都是正确的,但是程序不会超越“初始化幸运数字...”打印命令来检查我的功能是否正常工作。我相信问题要么在于我如何将数组传递给函数,要么在于我如何调用函数(也许在它们的原型(prototype)中?)任何帮助将不胜感激。这是我的第一篇文章,所以如果我无法正确包含代码,我深表歉意。谢谢!

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

/*
This program is a lucky number guessing game. A series of unique, random numbers
will be generated and stored in an array. Users will have up to three chances to
guess a number stored in the array.*/

//Function Prototypes
int fillArray (int,int []);
int searchArray (int, int []);
int printArray (int, int []);

int main(){
const int MAX = 25;
int iArray[MAX]={0};
int count;

printf("Initializing lucky numbers...");
//printf("%d", iArray[0]);
//for(count=0;count<MAX;count++){
//printf("%d ",iArray[count]);
//}//end for loop
fillArray(MAX,iArray);
//ONLY TO CHECK ARRAY CONTENTS
//printf("%d",iArray[0]);
//for(count=0;count<MAX;count++){
// printf("%d ",iArray[count]);
//}//end for loop
//CARRY ON WITH PROGRAM
searchArray(MAX,iArray);

getch();
return 0;
}

//Function Definitions

//Fill Array Function
int fillArray(int max,int Array[]){
int i;
int j;
int uniqueFlag;
int random;
srand(time(NULL));

for(i = 0; i < max; i++) {
/* Assume things are unique... we'll reset this flag if not. */
uniqueFlag = 1;
do {
random = (rand() % 101);
/* This loop checks for uniqueness */
for (j = 0; j < i && uniqueFlag == 1; j++) {
if (Array[j] == random) {
uniqueFlag = 0;
}//end if statement
}//end for loop
} while (uniqueFlag != 1);
Array[i] = random;
}//end while
//ONLY TO CHECK ARRAY CONTENTS
printf("%d",Array[0]);
printf("\n%d\n",Array[1]);
for(i=0;i<max;i++){
printf("%d ",Array[i]);
}//end for
}//end function

//Search Array Function
int searchArray(int max, int Array[]){
int i;
int guessCounter=0;
int iGuess;
do{
printf("Please guess a number between 0 and 100: ");
scanf("%d",&iGuess);
for(i=0;i<max&&guessCounter<3;i++){
if(Array[i] == iGuess){
printf("Correct");
guessCounter = 3;
}//end if
else{
printf("%d is not one of the lucky numbers.\n",iGuess);
guessCounter++;
}//end else
}//end for
}while(guessCounter < 3); //end do-while loop
if(guessCounter==3){
printf("Sorry! You lose!\n");
printArray(max, Array);
}//end if
}//end function

//Print Array Function
int printArray(int max, int Array[]){
int i;
printf("Array contents: ");
for(i=0;i<max;i++){
printf("%d ",Array[i]);
}//end for
}//end function

最佳答案

每当您选择随机数时,uniqueFlag 都需要重置,而不仅仅是在 fillArray 中的外循环开始时重置。

关于创建一个幸运数字猜谜游戏(将数组传递给函数/调用函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com