Accessing an array outside of a For loop

Sep 11, 2011 at 8:09pm
I am working on an assignment for school and I'm stuck.

Generate 50 non-repeating numbers between 1 - 1000. Then, sort numbers using a bubble sort. Then sort the same numbers using an insertion sort. I've generated my random numbers using a for loop, but when I try to access the array outside of the for loop it only contains the last random number...HELP!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>

using namespace std;

int main ()
{
	int i;
	int n = 0;
	int * p;
  	/* initialize random seed: */
  	srand ( time(NULL) );
	p = new int[50];
  	/* generate random numbers */
  	cout << "Generate 50 non-repeating random numbers between 1 - 1000.\n";
	for ( n=0; n < 50; n++ )
        {
          p[i] = rand() % 1000 + 1;
	  cout<< p[i] << "\n";	  
	}
        cout<< "Random numbers are: \n" << p[i] << "\n";
  return 0;
}


This is what I get(The numbers are different each time);

Generate 50 non-repeating random numbers between 1 - 1000.
254
407
549
516
996
133
350
570
202
509
518
392
22
967
781
927
159
227
817
329
358
321
415
474
37
657
484
681
769
544
360
127
262
388
642
114
36
848
104
885
310
440
552
974
707
363
59
924
223
901
Random numbers are: 
901


This is only my second assignment in C++ so any help would be greatly appreciated.
Sep 11, 2011 at 8:30pm
cout<< "Random numbers are: \n" << p[i] << "\n";

Please think about what this statement does.
Sep 11, 2011 at 8:35pm
Well I've tried it with p[0] and get the same output, if I use p[1] or above I get 0's. I have never used arrays in C++ before. It seems that my for loop is only replacing the first element in the array after each itteration. Any suggestions on how I can get it to store each element in a different location after each itteration?
Last edited on Sep 11, 2011 at 8:36pm
Sep 11, 2011 at 8:39pm
No really, read your code. Really think about what it does. Pretend you are a computer executing that program, and see what result you will get. I am sure you can find your mistake on your own.
Sep 11, 2011 at 8:43pm
The loop is correct. There's nothing wrong with it.
Look at this:
for ( n=0; n < 50; n++ )
And now look at this:
p[i] = rand() % 1000 + 1;
Last edited on Sep 11, 2011 at 8:44pm
Sep 11, 2011 at 9:08pm
n != i...
Sep 11, 2011 at 9:27pm
alright, I changed the loop to look like this...
 
for ( i=0; i < 50; i++)

and the print statement to
 
cout<<"Random numbers are: \n"<< *p <<"\n";

and now it will print the first number generated instead of the last, I can also go through use p[1], p[2], and so on to get the corresponding element, but I still can't seem to figure out how to print all the elements at once outside the loop. I'm sure that I'm making this harder than it is, but I am really just struggling here.
Sep 11, 2011 at 9:30pm
If you want to print the numbers, you do that with another loop. There is no way to just output all numbers at once.

I suppose you could overload ostream::operator<< for arrays, but really that would just mean shifting the same problem somewhere else
Last edited on Sep 11, 2011 at 9:30pm
Topic archived. No new replies allowed.