Need Help putting random # into array

Feb 19, 2008 at 1:05am
I can generate a set of random numbers, but I need to get them into an array. Can someone please help me? If I cout << number after the rand it prints me out a list, but I can't seem to get it into the array. Here 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
51
52
53
54
55
56
57
*/
//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions

//********************************** Function Prototypes

//********************************** Main Function


int main()
{
	time_t t;
	time(&t);
	yl;
	cls;
    
    srand((unsigned int)time(NULL));
    int data[45];
    int number;
    int i, j, k, high, low;
    high=206;
    low =147;
    int count=0;
	
	for(i=0;i<45;i++)
	{
          data[i] = 0;
        } // end for i

	
     for (i=93;i<=139;i++)
	{
        number=rand()% (high-low+1) + low;//
        data[i]=number; 
        cin >> data[i];
        i++;
       
    }//end for i 
Last edited on Feb 19, 2008 at 6:50pm
Feb 19, 2008 at 5:30am
i am somewhat of a newb myself, but i think i found some of the problems

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
*/
//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions

//********************************** Function Prototypes

//********************************** Main Function


int main()
{
	time_t t;
	time(&t);
	yl;
	cls;
    
    srand((unsigned int)time(NULL));
    int data[45];  //<---take out the int
    int number;
    int i, j, k, high, low;
    high=206;
    low =147;
    int count=0;
	
	for(i=0;i<45;i++)
	{
          data[i] = 0;
        } // end for i

	
     for (i=93;i<=139;i++)
	{
        number=rand()% (high-low+1) + low;//
        data[i]=number; 
        cin >> data[i];  //<---you are overwriting what you did in the previous line
        i++;
       
    }//end for i 
Feb 19, 2008 at 6:50pm
Thanks, but then how do I get the numbers generated into an array?
Feb 20, 2008 at 6:20pm
well your code is completely screwed up.
you declare an array of 45 ints, but in the second "for" loop you just ignore top array bound. why don't you use ? just one loop, there is no need to fill array with 0s.
1
2
for (int i = 0; i < 45; i++)
   data[i] = rand() % (high - low + 1) + low;
Last edited on Feb 20, 2008 at 7:05pm
Topic archived. No new replies allowed.