Arrays

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//scott jorgensen

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

void displayArray(int lotto[], int numElements);



int main()
{
    //array
    int lottoNum[6] = {0};
    int num = 0;
   
    int sub = 0;
    while (sub < 6)
        {
        
        srand ( time(NULL) );
        num = 1 + rand() % (54 - 1 + 1);
        cout << num; 
        cout << sub + 1; 
        cin >> lottoNum[sub];
        sub += 1;
        }// end while
    
    //displayArray
    displayArray(lottoNum), 7);
    
    system("pause");
    return 0;
}//end main

//defs
void displayLotto(int lotto[], int numElements)
{
    
    cout << fixed << setprecision (0) << endl << endl;
    int sub = 0;
    while (sub < numElements) 
    {
        cout << "Lottery Numbers: " << sub + 1 << " ";
        cout << lotto[sub] << endl;
        sub += 1;
    }
}


i appreciate any help
this is code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include<cstdlib>
#include<time.h>
using namespace 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;}

Last edited on
cin >> lottoNum[sub];

This is asking for lottoNum[sub] from the user. But, you already computed the number in num, didn't you?

displayArray(lottoNum), 7);

7? I think you want 6.
ok so was I making this too difficult on my self? I feel like im making this harder than it is. Is my way completely wrong?
Oh ok, I think what you need to do is put the randomly generated numbers into the array. I did this using a for loop:

1
2
for (int i = 0; i < 6; i++) {  //sets each array index equal to a random number
		array[i] = rand();}


What you did instead was do cin >>, but but cin accepts input from the console I believe. I may be wrong though.
closed account (DSLq5Di1)
I've commented some of the problem areas for you and pasted the working source in codepad below,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//scott jorgensen

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

void displayArray(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;
}

void displayLotto(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];
}
*/
http://codepad.org/OF5qaJvX
@creekist thanks for the code helped me too.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include<cstdlib>
#include<time.h>
using namespace 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;
}
Last edited on
Topic archived. No new replies allowed.