1 Attachment(s)
Need help to avoid using a global array variable
Hi all, this is my first post :-) I am learning C++ and I would really appreciate your help.
I wrote a small program that generates random combinations of lottery numbers, and which can be adapted to different types of lotteries.
The problem I am trying to solve is that, as you can see, I have used an array as a global variable.
If this wasn't an array, I would know how to use either pointers or references to avoid a global variable. However, I am not sure how to do this with an array.
Many thanks in advance -- any help or input will be greatly appreciated!
Code:
/* Generate combinations of lottery numbers */
#include <algorithm> // for sort()
#include <conio.h> // for getch()
#include <iostream>
#include <time.h> // for time()
using namespace std;
int const HOW_MANY = 5; // set how many numbers are returned each time
int const CEILING = 90; // set the highest number that can be returned
int numbers[HOW_MANY] = {0}; // array containing the main numbers
int randomize(int max) { // return a random number between 1 and max
return rand() % max + 1;
}
void doNumbers() {
int i;
for (i=0; i<HOW_MANY; i++)
numbers[i] = randomize(CEILING); // call randomize() HOW_MANY times to generate the combination
sort(numbers, numbers+HOW_MANY); // sort the combination
for (i=0; i<(HOW_MANY-1); i++)
if (numbers[i] == numbers[i+1]) // compare each number with the next one
doNumbers(); // if there are any duplicates, repeat the function
}
void printNumbers() {
int i;
for (i=0; i<HOW_MANY; i++)
cout << numbers[i] << "\t";
}
void giveOptions() {
cout << "\n\nWould you like one more combination?\n\n";
cout << "- Enter Y for YES\n";
cout << "- Enter N to end the program" << "\n\n\n";
int _getch();
switch (_getch()) {
case 'y':
case 'Y':
doNumbers();
printNumbers();
giveOptions();
break;
case 'n':
case 'N':
return;
default:
cout << "\n\nPlease enter either Y or N\n\n\n";
giveOptions();
}
}
int main() {
srand(unsigned(time(NULL)));
doNumbers();
printNumbers();
giveOptions();
return 0;
}
Re: Need help to avoid using a global array variable
The name of an array may be interpreted as a pointer to the first element of the array. Does that help?
Re: Need help to avoid using a global array variable
Thanks Lindley,
I tried to use the array name as if it were a pointer, but I got all sorts of compiler errors. As you can see, the code not only assigns a value to each element of the array, but it also sorts the values, and checks for duplicates.
I am probably missing something obvious, but I am not sure of which exact syntax should be used in the function doNumbers() in case I were to create a local array instead than a global one.
Re: Need help to avoid using a global array variable
I also tried creating an array on the free store:
Code:
int *numbers = new int[5];
...but then I didn't know how to refer to it from within another function: whatever syntax I use, I get an "undeclared identifier" compile-time error.
What am I missing?
Re: Need help to avoid using a global array variable
So you want to know how to pass arrays to functions?
Code:
void f(int* numbers, size_t size)
{
for(size_t i = 0; i < size; ++i)
{
std::cout << numbers[i] << std::endl;
}
}
int main()
{
const int QTY = 5;
int numbers[QTY] = {10, 20, 30, 40, 50};
f(numbers, QTY);
return 0;
}
Quote:
If this wasn't an array
Does it have to be an array? A vector is a lot more convenient.
Re: Need help to avoid using a global array variable
Quote:
Originally Posted by
random++
Thanks Lindley,
I tried to use the array name as if it were a pointer, but I got all sorts of compiler errors.
Such as? Show both the attempted code and the result.
Re: Need help to avoid using a global array variable
Quote:
Originally Posted by
Mybowlcut
So you want to know how to pass arrays to functions?
Thank you SO much! I finally know what I was missing -- I have fixed the code as per your suggestion and it works perfectly!
Code:
/* Generate combinations of lottery numbers */
#include <algorithm> // for sort()
#include <conio.h> // for getch()
#include <iostream>
#include <time.h> // for time()
using namespace std;
int const SIZE = 5; // set how many numbers are returned each time
int const CEILING = 90; // set the highest number that can be returned
void doNumbers(int *numbers, int SIZE);
void printNumbers(int *numbers);
void giveOptions(int *numbers, int SIZE);
int main() {
int numbers[SIZE] = {0};
srand(unsigned(time(NULL)));
doNumbers(numbers, SIZE);
printNumbers(numbers);
giveOptions(numbers, SIZE);
return 0;
}
int randomize(int max) { // return a random number between 1 and max
return rand() % max + 1;
}
void doNumbers(int *numbers, int SIZE) {
for (int i=0; i<SIZE; i++)
numbers[i] = randomize(CEILING); // call randomize() SIZE times to generate the combination
sort(numbers, numbers+SIZE); // sort the combination
for (int i=0; i<(SIZE-1); i++)
if (numbers[i] == numbers[i+1]) // compare each number with the next one
doNumbers(numbers, SIZE); // if there are any duplicates, repeat the function
}
void printNumbers(int *numbers) {
for (int i=0; i<SIZE; i++)
cout << numbers[i] << "\t";
}
void giveOptions(int *numbers, int SIZE) {
cout << "\n\nWould you like one more combination?\n\n";
cout << "- Enter Y for YES\n";
cout << "- Enter N to end the program" << "\n\n\n";
int _getch();
switch (_getch()) {
case 'y':
case 'Y':
doNumbers(numbers, SIZE);
printNumbers(numbers);
giveOptions(numbers, SIZE);
break;
case 'n':
case 'N':
return;
default:
cout << "\n\nPlease enter either Y or N\n\n\n";
giveOptions(numbers, SIZE);
}
}
Quote:
Originally Posted by
Mybowlcut
Does it have to be an array? A vector is a lot more convenient.
I know -- thanks. That's actually my next goal: to re-write the same program using vectors. After that, I'll try to re-write it using classes as well. This will help me learn :)
Thanks again!
Re: Need help to avoid using a global array variable
Quote:
Originally Posted by
Lindley
Such as? Show both the attempted code and the result.
The error was "undeclared identifier".
What I was doing wrong (I now realized) is that, after creating the array within one function, I was trying to use it in another function without passing it in as an argument. Newbie mistake :)
Thanks a lot for your help!