Using a pointer to assign random numbers to array.

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
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
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;
}
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.
Connection between a pointer and an array

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

ie.
*parray == array[0]
*(parray+1) == array[1]
*(parray+2) == array[2] etc.
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.
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;?
@biiirrrdman

No, you can't.

parray is a pointer, ie. it stores an address!
Last edited on
@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.
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.
Okay, so I can make an
1
2
3
int a;
a= (rand()%17)+3;
*parray = a;

@ 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
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.
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;
}
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
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
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
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.