Ok, im learning one dimensional arrays, I think im confusing my self. Im supposed to make a program that makes 6 random numbers, put them in a array then displays them. This is what I have.
#include <iostream>
#include<cstdlib>
#include<time.h>
usingnamespace std;
int main() {
srand(time(0)); //sets seed for rand
int array[6] = {0}; //initializes first element and rest of elements to 0
for (int i = 0; i < 6; i++) { //sets each array index equal to a random number
array[i] = rand();}
for (int i = 0; i < 6; i++) { //prints the array
cout << array[i] << " ";}
return 0;}
//scott jorgensen
#include <iostream>
#include <iomanip>
#include <ctime>
usingnamespace std;
voiddisplayArray(int lotto[], int numElements);
int main()
{
srand ( time(NULL) ); // Moved, see below.
int lottoNum[6] = {0};
int num = 0;
int sub = 0;
while (sub < 6)
{
srand ( time(NULL) );
// You should only seed a random number once in an application before calling rand()
num = 1 + rand() % (54 - 1 + 1); // ???
// cout << num;
// cout << sub + 1;
cin >> lottoNum[sub]; // Should be.. lottoNum[sub] = num; ?
sub += 1;
}
displayArray(lottoNum), 7);
// Remove the extra parentheses after lottoNum
// lottoNum has 6 elements not 7.
system("pause");
return 0;
}
voiddisplayLotto(int lotto[], int numElements)
// Rename to displayArray or change your declaration and function call above to displayLotto.
{
cout << fixed << setprecision (0) << endl << endl;
// You are not using any floating point numbers so I'm not sure what the point of this line is.
int sub = 0;
while (sub < numElements)
{
cout << "Lottery Numbers: " << sub + 1 << " ";
cout << lotto[sub] << endl;
sub += 1;
}
}
/* Both of your while loops would serve you better as for loops. ie:-
for (int sub = 0; sub < numElements; sub++)
{
cout << lotto[sub];
}
*/
I also added a little labeling of the indexs. I know I'm a noob lol, I just never have had the time to use more then one initialization/increasement for a for loop.
#include <iostream>
#include<cstdlib>
#include<time.h>
usingnamespace std;
int main() {
cout << "Array Elements: ";
srand(time(0)); //sets seed for rand
int array[6] = {0}; //initializes first element and rest of elements to 0
for (int i = 0; i < 6; i++) { //sets each array index equal to a random number
array[i] = rand();}
for (int i = 0, a = 0; i < 6; i++, a++) { //prints the array
cout << "(" << i << ")" << array[i] << " ";}
cin.get();
cin.get();
return 0;
}