dimanche 16 octobre 2016

C++ Lotto 649 Application

Alright, so first I get the user to enter 6 numbers between 1-49. Then I insert those into an array, send that array to a BubbleSort function to get sorted and then display the sorted array. I am almost positive my BubbleSort function is correct, so maybe it's something in my main function? Here is my code... (Please disregard the WinningNumbers array)

#include <iostream>
#include <time.h>

using namespace std;

int GetInteger();  
void RandomizeSeed();  
int BubbleSort(int a[], const int size);  
int LuckyNumbers[6] = {};  
int WinningNumbers[6] = {};  

int main()  {  
    RandomizeSeed();  
    //Ask the user to enter their numbers  
    cout << "Enter 6 numbers between 1 and 49:\n";   

    //Get the user's numbers  
    for (int i = 0; i < 6; i++)  
    {  
        LuckyNumbers[i] = GetInteger();  
    }  
    cout << endl;  
    BubbleSort(LuckyNumbers, 6);  
    system("pause");  
    return 0;  
}  
int GetInteger()  
{  
    int value = 0;  
    while (!(cin >> value))  
    {  
        cin.clear();  
        cin.ignore(numeric_limits<streamsize>::max(), '\n');  
    }  
    return value;  
}  
void RandomizeSeed() {  
    srand(time(NULL));  
}  
int RandomRange(int min, int max) {  
    int randomValue = rand() % (max + 1 - min) + min;  
    return randomValue;  
}  
int BubbleSort(int a[], const int size) {  
    //Cycle through, and sort the array of numbers from lowest to highest  
    for (int i = 0; i < size - 1; i++)  
    {  
        //didSwap flag  
        bool didSwap = false;  
        //Cycle through the values of the array and compare elements  
        for (int j = 1; j < size; j++)  
        {  
            //Compare the two elements  
            if (a[j - 1] > a[j])  
            {  
                //Swap values  
                int temp = a[j];  
                a[j] = a[j - 1];   
                a[j - 1] = temp;  
                cout << a[j] << endl;  
                //Indicates that a swap occurred  
                didSwap = true;  
            }  
        }  
        //If a swap did not occur, that means we are done sorting and   
        //we can break out of the outer for loop  
        if (didSwap == false)  
        {  
            break;  
        }  
    }  
    return 0;  
}  




Aucun commentaire:

Enregistrer un commentaire