将比赛数量转换为获奖奖的功能
Function that converts number of matches to winning prize
我正在创建一个彩票程序,该程序在1-36之间生成5个随机数的机票。用户可以选择他想要多少票,这是我的主要循环。我完成了大部分代码,但是我在创建一个函数方面遇到了问题,该功能转换了在阵列中赢得奖金的比赛数量,该数组将在for循环结束后结合奖金。一切都很好,但是我很难理解如何将for循环的奖金存储到另一个数组中,以便我可以结合奖金的总数。这是我的代码。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int ARRAY_SIZE = 5; //size of the array
//function prototypes
void generateWinning (int [], int);
void genTicket(int[], int);
void bubbleSort(int [], int);
void findMatch(int [], int [], int[]);
void printArray(int [], int);
void printWinning(int);
int main()
{
srand( time(NULL) );
int numTickets, j;
int totalWinnings[4]={0,0,0,0};
int lottery[ARRAY_SIZE]; //holds winning lottery
int user[ARRAY_SIZE]; //holds users number of ticks
cout << "Enter number of tickets: ";
cin >> numTickets;
generateWinning (lottery, ARRAY_SIZE); //generate winning tickets, then
//display
printArray(lottery, ARRAY_SIZE);
for( int i=0; i < numTickets; i++)//loop amount of times user chooses tickets
{
genTicket(user, ARRAY_SIZE);//generate random numbers without duplicates
bubbleSort(user, ARRAY_SIZE);//sort list of array sorted.
printArray(user, ARRAY_SIZE); //print results.
findMatch(lottery,user, totalWinnings);//find matches then store
//them into totalWinning Array
}
printArray(totalWinnings, 4);
//j=0;
//int total = totalWinnings[j] + totalWinnings[j+1] + totalWinnings[j+2]
+ totalWinnings[j+3];
//cout << "You Total Prize Amount = "<< "$" << total <<endl;
return 0;
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
{
printf("%d", arr[i]);
printf(" ");
}
cout << endl;
}
void generateWinning(int lottery[], int ARRAY_SIZE)
{
cout << "Winner ";
int index =0;
int lotteryPool[36];
for(int x = 0; x<36; x=x+1)
{
lotteryPool[x]=x+1;
}
random_shuffle(&lotteryPool[0], &lotteryPool[35]);
while (index < ARRAY_SIZE)
{
lottery[index] = lotteryPool[index];
index++;
}
bubbleSort(lottery,ARRAY_SIZE);
}
void genTicket(int arr[], int ARRAY_SIZE)
{
int index=0;
int lotteryPool[36];
for (int x=0; x<36; x=x+1)
{
lotteryPool[x]= x+1;
}
random_shuffle(&lotteryPool[0], &lotteryPool[35]);
while (index < ARRAY_SIZE)
{
arr[index] = lotteryPool[index];
index++;
}
}
void bubbleSort(int user[], int ARRAY_SIZE)
{
int i, j, temp;
for (i=1; i < ARRAY_SIZE; ++i)
{
for(j=0; j<(ARRAY_SIZE-i); ++j)
{
if (user[j] > user[j+1])
{
temp = user[j];
user[j]=user[j+1];
user[j+1] = temp;
}
}
}
}
void findMatch(int user[], int lottery[], int winning[])
{
int index, temp;
int matches = 0;
for(int index = 0; index < ARRAY_SIZE; index++)
{
for(int temp =0; temp < ARRAY_SIZE; temp++)
{
if(lottery[index]==user[temp])
{
matches++;
}
else
{
cout <<"";
}
}
}
cout << matches << endl;
int i;
if (matches == 2)
{
winning[i]=1;
}
else if (matches == 3)
{
winning[i]=10;
}
else if(matches == 4)
{
winning[i]=500;
}
else if (matches = 5)
{
winning[i]=25000;
}
}
This is my output:
Enter number of tickets: 5
Winner 2 9 14 30 33
7 11 14 16 29
1
10 14 33 34 35
2
1 8 12 25 34
1
6 13 25 28 33
1
2 3 11 13 26
1
0 0 0 0 //why is this not storing match that equal prize?
您的问题是您在代码的这一部分中调用了未定义的行为:
// in findMatch
int i; // i is uninitialized
if (matches == 2)
{
winning[i]=1; // using i as an array index results in undefined behavior
}
else if (matches == 3)
{
winning[i]=10;
}
else if(matches == 4)
{
winning[i]=500;
}
//else if (matches = 5) // typo in the operator here
else if (matches == 5)
{
winning[i]=25000;
}
要解决此问题,只需将i
作为参数添加到您的功能中,然后在FindMatch中摆脱int i;
:
void findMatch(int user[], int lottery[], int winning[], int i)
{ ... }
相关文章:
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 字符转换功能 std::isupper() & std::islower() C++17
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 为什么从基转换为派生提供此功能?
- 借助功能将 2D 转换为 1D 数组
- 不存在从"Magick::Color"到"MagickCore::Quantum"的合适转换功能
- 模板功能:根据类型名称进行转换
- 组件X86转换为ARM功能呼叫,其中有不同数量的参数为ARM
- 将variadic模板功能转换为许多具体功能
- 不存在从"const std::string"到"time_t"的合适转换功能
- 功能指针参数参数转换为const
- 最佳可行的转换功能
- 为什么在相同大小的功能参数中隐式转换不会发出警告
- 功能采用variadic模板包将std :: strings转换为const char *
- 为什么转换涉及两个用户定义的转换功能/构造函数
- 将C 回调功能转换为Delphi
- 将过载功能转换为专用功能模板
- 将C 功能转换为MIP
- C 概念:某些特征功能转换
- 如何从类功能转换指针