int r;
int c;
int n;
for (c = 0; c < 2; c++)
{
for (n = 0; n < 6; n++)
{
for (r = 0; r < n; r++)
{
if (tickets[n][c] > tickets[n+1][c])
{
int temp = tickets[n][c];
tickets[n][c] = tickets[n+1][c];
tickets[n+1][c] = temp;
}
}
}
}
Could anyone point me in the right direction on what I am doing wrong?
I was working on one of the projects I saw posted in the forums here. Here is what I have so far.
I have already switched one of the arrays to a vector because it was giving me a lot of problems with deleting the elements. But I would like to try and keep the rest of the arrays.
I know MD arrays are not optimal, but I would like to try and finish it like this. This is all for learning anyways.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <time.h>
#include <new>
#include <vector>
usingnamespace std;
// array that holds the lotto tickets
int tickets[6][10];
// array to hold wining numbers
int lottowinnum[6];
// var for the powerball
int powerball;
void lottosort (int *array,int length)
{
int i,j;
for (i = 0; i < 6; i++)
{
for (j = 0; j < i; j++)
{
if (array[i] < array[j])
{
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
usingnamespace std;
int main()
{
cout << "Welcom to Sam's LOTTO!" << endl << endl;
cout << "How many tickets would you like, you may choose up to 10." << endl << endl;
int numplrtick;
cin >> numplrtick;
cout << "Please Choose 6 numbers from 1-50." << endl << endl;
int t = 0;
for (t=0; t <= (numplrtick - 1); t++)
{
int n = 0;
vector<unsignedint> tempnumchk (6); // vector to hold the numbers to check.
for (n=0 ; n<6; n++)
{
int plrnumcho;
bool lottonum2t = false;
do
{
// Set the bool back to false so that loop can continue.
lottonum2t = false;
cout << "please choose your " << n + 1 <<" number." << endl << endl;
cin >> plrnumcho;
// check if same number picked twice.
int lnc; // Counter for loop
for (lnc = 0; lnc <= 5; lnc++)
{
if (tempnumchk[lnc] == plrnumcho)
{
lottonum2t = true; // Bool to see if the number has been choosen before.
cout << "Each number must be diffrent, please choose again" << endl << endl;
break;
}
}
// add the player number to the temp vector
tempnumchk [n] = plrnumcho;
} while ((plrnumcho > 50 || plrnumcho < 1) || lottonum2t);
tickets[t][n] = plrnumcho;
//int results = tickets[t][n];
//cout << results << endl << endl;
}
// delete/unassign the temp array.
tempnumchk.erase (tempnumchk.begin(),tempnumchk.begin()+6);
if (numplrtick > 1)
{
cout << "Now choose the 6 numbers for your " << t + 1 << " ticket" << endl << endl;
}
}
cout << "Great, now its time for the lotto drawing!" << endl << endl;
srand(time(0));
int ln;
for (ln = 0; ln <= 6 ; ln++)
{
int temprand = rand() % 50 + 1;
lottowinnum[ln] = temprand;
}
// sort array of winning lotto numbers
lottosort(lottowinnum,6);
// sort player tickets
int r;
int c;
int n;
for (c = 0; c < numplrtick; c++)
{
for (n = 0; n < 6; n++)
{
for (r = 0; r < n; r++)
{
if (tickets[r][c] > tickets[r+1][c])
{
int temp = tickets[r][c];
tickets[r][c] = tickets[r+1][c];
tickets[r+1][c] = temp;
}
}
}
}
// print the winning lotto numbers
int wn;
for (wn = 0; wn <= 5; wn++)
{
int winnum = lottowinnum[wn];
cout << winnum << endl << endl;
}
// print player tickets
int pt;
for (pt = 0; pt < numplrtick; pt++)
{
int n;
for (n = 0; n < 6; n++)
{
cout << tickets [n][pt] << endl << endl;
}
}
int pause;
cout << "Press any key to continue...";
cin >> pause;
return 0;
}