C++ 我想,功能不互相沟通?
现在,在创建guess 1数组之后,guess数组将被置为零?知道这是怎么发生的吗?这是在matches函数中吗?在我将值设置为guess 1数组之前,guess数组等于我输入的数字。但是,在我使用if和else语句生成guess 1数组之后,guess数组被设置为零C++ 我想,功能不互相沟通?,c++,C++,现在,在创建guess 1数组之后,guess数组将被置为零?知道这是怎么发生的吗?这是在matches函数中吗?在我将值设置为guess 1数组之前,guess数组等于我输入的数字。但是,在我使用if和else语句生成guess 1数组之后,guess数组被设置为零 #include <iostream> #include <cstdlib> #include <algorithm> using namespace std;
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int size1=80;
const int size2=10;
const int size3=20;
void initboard(int keno[], int size1);//initialize the keno board
void twentnum(int keno[], int size3,int size1);//twenty randomly selected numbers
void matches(int keno[],int guess[],int guess1[],int& numofmatches,int size1,int size2);
void dispkeno(int keno[],int guess1[],int size1);//displays keno board
void dispmess(int guess[], int numofmatches,int size2);
int main()
{
int keno[size1];//keno board array
int guess[size2];//guess array
int guess1[size3];//first guess array
int numofmatches;//number of matches
//This code represents a game keno that chooses twenty random numbers
//from 1-80 and asks the user for 10 numbers from 1-80. The code then
//finds the matches between the users array and the randomly selected
//number array.
initboard(keno,size1);
twentnum(keno,size3,size1);
matches(keno,guess,guess1,numofmatches,size1,size2);
dispkeno(keno,guess1,size1);
dispmess(guess,numofmatches,size2);
return 0;
}
void intiboard(int keno[], int size1)
{
int i;
for(i=1;i<size1+1;i++)
{
keno[i]=0;
}
}
void twentnum(int keno[], int size3, int size1)
{
int seed;
int j;
int k;
cout<<"Enter an integer to seed the random number generator."<<endl;
cin>>seed;
srand(seed);
j=1;
k=0;
while(j<size3+1)
{
k=rand()%size1+1;
if(keno[k]!=k)
{
keno[k]=k;
j++;
}
}
}
void matches(int keno[],int guess[],int guess1[],int& numofmatches,int
size1,int size2)
{
int number;
for(int v=1;v<size1+1;v++)
{
guess1[v]=0;
}
cout<<"Please enter 10 numbers from 1-80, inclusive, as your
guesses."<<endl;
for(int a=1; a<size2+1;a++)
{
cin>>number;
guess[a]=number;
}
sort(guess, guess+11);
int h=1;
int y=1;
while(h<size1+1 && y<size2+1)
{
if(guess[y]==h)
{
guess1[h]=h;
h++;
y++;
}
else
{
guess1[h]=0;
h++;
y;
}
}
for(int w=1;w<size1+1;w++)
{
if(keno[w]==guess1[w] && keno[w]!=0)
{
numofmatches=1+numofmatches;
}
}
}
void dispkeno(int keno[],int guess1[],int size1)
{
for(int u=1;u<size1+1;u++)
{
if(keno[u]==0)
{
cout<<keno[u]<<endl;
}
if(keno[u]==guess1[u] && keno[u]!=0)
{
cout<<keno[u]<<"*"<<endl;
}
if(keno[u]!=guess1[u] && keno[u]!=0)
{
cout<<keno[u]<<endl;
}
}
}
void dispmess(int guess[], int numofmatches,int size2)
{
cout<<"Your numbers were ";
for(int q=1;q<size2+1;q++)
{
cout<<guess[q]<<", ";
}
cout<<"\nYou had "<<numofmatches<<" matches"<<endl;
if(numofmatches==10)
{
cout<<"You win $50000.00!"<<endl;
}
if(numofmatches==9)
{
cout<<"You win $1000.00!"<<endl;
}
if(numofmatches==8)
{
cout<<"You win $250.00!"<<endl;
}
if(numofmatches==7 || numofmatches==6)
{
cout<<"You win a free game!"<<endl;
}
else
{
cout<<"You did not win anything this time. Maybe next time"
<<endl;
}
}
#包括
#包括
#包括
使用名称空间std;
常数int size1=80;
常数int size2=10;
常数int size3=20;
无效初始板(内部基诺[],内部尺寸1)//初始化基诺板
void twentnum(整数基诺[],整数大小3,整数大小1)//二十个随机选择的数字
无效匹配(int基诺[]、int猜测[]、int猜测1[]、int&numomfmatches、int大小1、int大小2);
void dispkeno(整数基诺[],整数猜测1[],整数大小1)//显示基诺板
void dispmess(int guess[],int numofmatches,int size2);
int main()
{
int基诺[size1];//基诺板阵列
int guess[size2];//guess数组
int guess1[size3];//第一个猜测数组
int numofmatches;//匹配数
//此代码表示一个选择20个随机数的游戏基诺
//从1-80,并要求用户从1-80中输入10个数字。然后输入代码
//查找用户数组和随机选择的对象之间的匹配项
//数字数组。
初始板(基诺,尺寸1);
twentnum(基诺、3号、1号);
匹配项(基诺、猜测、猜测1、numofmatches、大小1、大小2);
dispkeno(基诺,猜测1,大小1);
dispmess(猜测、numofmatches、size2);
返回0;
}
无效板(内部基诺[],内部尺寸1)
{
int i;
对于(i=1;i您没有初始化'numfmatche'变量,只需在主函数中添加
int numofmatches = 0;//number of matches
计数将从零开始。如果不初始化变量,则内存中剩余的任何垃圾(分配了“numofmatches”)都将是它的起始值。这将导致意外的-856666。您确定所有这些代码都是重现问题所必需的吗?为什么您认为它不应该打印-856666?您的所有代码都用于循环从n=1开始;n<大小+1,其中C++中的n=0;n<n。size@DaveSfor循环可以按程序员的意愿进行。另一方面,数组索引…我希望代码在第一个void函数中执行:将电路板初始化为全零。然后第二个函数生成随机数并将其输入到基诺数组。第三个是向用户索要10个数字,然后在用户给出的数字和随机生成的数字之间找到匹配项。然后在显示数字后,将结果与所有匹配项一起输出,并在结果中加上星号。