我在使用随机整数填充数组的猜谜游戏时遇到了一些麻烦,然后让用户尝试猜测数组中的数字。我相信我的大部分都是正确的,但程序不会超出“初始化幸运数字...”打印命令,以检查我的功能是否正常。我相信问题或者在于我如何将数组传递给函数,或者我如何调用函数(可能在他们的原型中?)任何帮助都将非常感激。 这是我的第一篇文章,所以如果我无法正确包含代码,我会道歉。谢谢!
#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
答案 0 :(得分:1)
uniqueFlag
都需要重置,而不仅仅是fillArray
中外环的开头。
答案 1 :(得分:1)
谢谢大家!对于任何检查或有类似问题的人,我都包括了我的更正。我能够在一个名为checkArray的单独函数中检查唯一数字,并通过返回fillArray函数中的指针以及一些其他(包含的)调整来解决我的fillArray问题。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
//Function Prototypes
int fillArray (int, int []);
int checkArray (int, 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...\n");
fillArray(MAX,iArray);
searchArray(MAX,iArray);
getch();
return 0;
}
//Function Definitions
//Fill Array Function
int fillArray(int max, int Array[]){
int number;
int i=0;
int uniqueFlag;
srand(time(NULL));
while(i<max){
number = (rand() % 101);
if(checkArray(max, number, Array)==0){ //passes to another function to check for uniqueness
Array[i]=number;
++i;
}//end if
}//end while
return *Array;
}//end function
//Check Array Function
int checkArray(int max, int number, int Array[]){
int i;
int uniqueFlag;
for(i=0;i<=max;i++){
if(Array[i]==number){ //determines if random number has already been used in array
uniqueFlag = 1;
return uniqueFlag;
}//end if
else{
uniqueFlag = 0;
}//end else
}//end for
return uniqueFlag;
}//end function
//Search Array Function
int searchArray(int max, int Array[]){
int i;
int x=0;
int guessCounter=0;
int iGuess;
while(x !=1 && guessCounter<3){
printf("Please guess a number between 0 and 100: ");
scanf("%d",&iGuess);
for(i=0;i<max;i++){
if(iGuess==Array[i]){
x = 1;
}//end if
}//end for
switch(x){
case 1:
printf("%d is one of the lucky numbers. Congratulations! You win!", iGuess);
printArray(max, Array);
getch();
return 0;
default:
printf("%d is not one of the lucky numbers.\n",iGuess);
++guessCounter;
}//end switch statement
}//end while condition
if(guessCounter==3){ //condition limits to 3 guesses per run
printf("Sorry! You lose!");
printArray(max, Array);
}//end if
}//end function
//Print Array Function
int printArray(int max, int Array[]){
int i;
printf("\nArray contents: ");
for(i=0;i<max;i++){
printf("%d ",Array[i]);
}//end for
//getch();
//return 0;
}//end function
答案 2 :(得分:0)
我无法看到searchArray
乍一看是否有问题,所以你应该简化它。我建议将整个uniqueFlag
部分移到自己的函数{@ 1}}?