Using a pointer to assign random numbers to array.

Mar 10, 2010 at 8:20am
Hi!

I am currently working on an exercise for my Programing Foundations I class.

This was the prompt:

Write a program that queries the user for the size and then declares an integer array of that size. Create a pointer to the first element of the array and then use the pointer to fill the array with random integer values in the range 3-17. Then print each element of the array with its address to the screen.

And this is what I have so far:

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
int size = 0;
cout << "How big do you want the array to be?: ";
cin >> size;

int array[size];
int* parray = &array[0];

cout << *parray << endl;

for(int i=0;i<size;i++)
{
cout << "Array[" << i << "] " << array[i] << endl;
}

/* use rand%17+3? */

return 0;
}


I just need emphasis on the pointer and how to use it. I've been looking at websites, but it's all theory based. I'd do better with an example. Any help would be much appreciated. Thanks.
Last edited on Mar 10, 2010 at 8:24am
Mar 10, 2010 at 8:38am
If you need a pointer variable to point to the first thing in the array this is the line you are trying to do that with correct?
int* parray = &array[0];

An array is just a pointer, a special kind of pointer, but a pointer nonethless therfore to create a pointer to the first element in the array simply type:
int* parray = array


Last edited on Mar 10, 2010 at 8:38am
Mar 10, 2010 at 8:54am
I know somehow I have to make parray=rand%17+3 and then plug it back into array[], I just don't know how. Would I make a recursive loop like
1
2
3
for(int i=0;i<size;i++){
array[i] = parray;
}
Mar 10, 2010 at 8:56am
biiirrrdman wrote:
An array is just a pointer, a special kind of pointer, but a pointer nonethless therfore to create a pointer to the first element in the array simply type:
int* parray = array

After that you must also change the for loop.

for(int i=0; i<size; i++, parray++) cout << *parray << parray << endl;

Warning: Size of the array should be a constant value although some compilers will let you declare it with a non-const size.
Mar 10, 2010 at 9:04am
Connection between a pointer and an array

*(parray+n) == array[n]

ie.
*parray == array[0]
*(parray+1) == array[1]
*(parray+2) == array[2] etc.
Mar 10, 2010 at 9:13am
Danielsson wrote:
Connection between a pointer and an array

*(parray+n) == array[n]

ie.
*parray == array[0]
*(parray+1) == array[1]
*(parray+2) == array[2] etc.


can't you also use bracket notation on the parray just like you would on array(with the exception of the first one i.e.
parray == array[0]
parray[1] == array[1]
parray[2] == array[2]etc.
Mar 10, 2010 at 9:23am
I can plug that in, but everytime I try to make parray or *parray random, I am shot down. So we can't make parray = rand%17+3;?
Mar 10, 2010 at 9:25am
@biiirrrdman

No, you can't.

parray is a pointer, ie. it stores an address!
Last edited on Mar 10, 2010 at 9:26am
Mar 10, 2010 at 9:32am
@robertlenos

If you want to generate random numbers look here:
http://www.daniweb.com/forums/thread1769.html#

In any case you have to assign the value to *parray not to parray.
Mar 10, 2010 at 9:34am
So how would I use the pointer to fill the array with random values?

I get the *parray == array[0]

So can I make a for loop like this?
1
2
3
4
for(int i=0; i < size; i++)
{
*(parray+i) == array[i];
}



Sorry if I am just going in circles or something. This is my first year programming with no prior experience.
Mar 10, 2010 at 9:38am
Okay, so I can make an
1
2
3
int a;
a= (rand()%17)+3;
*parray = a;

Mar 10, 2010 at 9:45am
@ Danielsson are you sure about that this simple program illustrates my point and it is adopted form Absolute C++ (Savitch)

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
#include<iostream>
using namespace std;

int main()
{
  int arr[10];
  int* parr;
  int i;
  for(i = 0; i < 10; i++)
    {
      arr[i] = i;
    }
  parr = arr;
  for(i = 0; i < 10; i++)
    {
      cout << parr[i] << " ";
    }
 cout << endl;

 for(i = 0; i < 10; i++)
   {
     parr[i] = parr[i]+1;
   }
 for(i = 0; i < 10; i++)
   {
     cout << arr[i] << " ";
   }
 cout << endl;
 return 0;
}


0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10


@ robertlenos Sorry I ave gotten somewhat off topic and not very helpful
Last edited on Mar 10, 2010 at 9:47am
Mar 10, 2010 at 9:53am
Yeah, you're right.

It works in this case because arr (constant pointer to array) was assigned to parra.

I only wanted to say that it's not a general way how to work with pointers.
Mar 10, 2010 at 10:00am
I think I got it guys! Thank you so much! Very awesome! And now I know pointers somewhat more intimately!

Here's my final code if you guys just want to see what I did:
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
int size = 0;
cout << "How big do you want the array to be?: ";
cin >> size;

int array[size];
int* parray;

parray = array;

for(int i=0;i<size;i++)
{
parray[i]=(rand()%17)+3;
}

for(int i=0;i<size;i++)
{
*(parray+i) == array[i];
cout << "Array[" << i << "] " << array[i] << endl;
}

/* use rand%17+3? */

return 0;
}
Mar 10, 2010 at 10:15am
Glad to hear you got it working, wish i could say the same for my homework, one thing though, are you sure line 24 is necessary?

Last edited on Mar 10, 2010 at 10:20am
Mar 10, 2010 at 10:21am
No, I am not. Lol.
You just fixed something I was questioning myself for I think.
I was getting an 18 randomly every now and again.

EDIT: Nevermind, still getting a random 18 and 19.
Last edited on Mar 10, 2010 at 10:22am
Mar 10, 2010 at 10:50am
Yeah just tried out my own solution to your problem, and am getting random 18's and 19's as well. Tried using the more complex random number code from the forum linked by Danielsson but now it is only printing 3's

EDIT: You are probably making the same mistake I did look at this line in particular
parray[i]=(rand()%17)+3;
It's a pretty easy fix once you realize what range of numbers you are actually assigning to the array.
Last edited on Mar 10, 2010 at 11:01am
Mar 10, 2010 at 11:04am
I see the discussion is going on. :-)

You have to initialize the random generator with srand() function:

srand((unsigned)time(0)); (don't forget to include <ctime> header)

This code seeds the random generator depending on system time.

If you don't use it then the row of random numbers will be always the same.
Topic archived. No new replies allowed.