dynamic allocated array

hey can someone one help me do this
i need to write a dynamic allocated array using pointer operations.
NO array subscripting.

i need to fill array using random numbers from 1 to 100 and use function to display it.
so far i got


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
void display(int *, int)
int main()
{
int *data = nullptr;
cout << "array size";
cin >> arraySize;

data = new int =[arraySize]
for(int i=0;i< arraySize; i++)
{
*data = rand() % 100 +1;
data++;
}
// i need to reset data to point to beginning of array

display(data, arraySize);
delete [] data;
data = nullptr;
return 0;
}

void display(int *random, int size)
for(int i =0; i < size; i++)
{
cout *random++ << " " << endl;
}


it doesnt work, can someone help me please thankyou
Last edited on
Lots of compiler errors.
line 1: missing semicolon.
line 6: arraySize not defined
line 8: misplaced = symbol, missing semicolon
line 22: missing {
line 25: missing <<
line 27: missing }

Logic error:
loop at line 9 to 13 should not be changing the value of data by incrementing it. Use a separate pointer variable instead.
The compile error most of them are not there, it was just the way I was typing the code from my phone on here, sorry let me rewrite the code so you can tell me what exactly is wrong with my code

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
void display(int*,int);


int main()
{
	int arraySize;			// size of the dynamically allocated array

	int *data = nullptr;
	cout << "Array size: ";
	cin >> arraySize;

	data = new int[arraySize];

	// Write a loop to fill the "data" array with random numbers from 1 - 100 (inclusive)
	//  this code will use pointer incrementing/decrementing and dereferencing
	for (int i = 0; i < arraySize; i++)
	{
		*data = rand() % 100 + 1;
	}

	//how to  Reset "data" to point to the beginning of the array
	 

	// Call display() to print the "data" array
	display(data, arraySize);


	
	delete[] data;
	data = nullptr;
	 
	 return 0;
}

void display(int random*, int size)
{
	
	for (int i = 0; i < size; i++)
	{
		cout << *random++ << " ";
	}
	cout << endl;
}
1
2
3
4
	for (int i = 0; i < arraySize; i++)
	{
		*data = rand() % 100 + 1;
	}


data is a pointer to the first element of the array. So all you're doing here is changing the value of the first element, over and over again. You need to modify a different element of the array on each iteration of the loop.

Since you're not allowed to use array indices, you'll have to use pointer arithmetic to point to the appropriate array element each time. You clearly already know how to do that, since you do it in your display() function.

You'll also need to keep a copy of the original pointer to the start of the array, so that you can use it later on in the program.
Thank you for the updated version of the code.

Line 35:
 
void display(int random*, int size)

compiler error,
35	24	[Error] expected ',' or '...' before '*' token

correct code should be:
 
void display(int * random, int size)


Ok now we have some code which we can run, let's see what happens when it is tested.
Array size: 2
68 4064064
The array should contain values in the range 1 to 100, but that isn't what was displayed.

Here, I repeat what I said earlier,
loop at line 16 to 19 should not be changing the value of data by incrementing it. Use a separate pointer variable instead.

Here's one way:
1
2
3
4
5
6
    int * p = data;
    for (int i = 0; i < arraySize; i++)
    {
        *p = rand() % 100 + 1;
        ++p;
    }

Here's another way:
1
2
3
4
    for (int * p = data; p < data + arraySize; p++)
    {
        *p = rand() % 100 + 1;
    }

And a variation on that last one,
1
2
3
    int * p = data; 
    while (p < data + arraySize)
        *p++ = rand() % 100 + 1;


But what about the function display(), doesn't that also need to be changed to use a separate pointer? Well, no it doesn't.

In this case, the parameter int * random is passed by value, which means it is already a separate copy of the pointer data in main.
Last edited on
another dynamic allocated "array" with one element larger than "data" array. I need to add 1 to first element and the rest same as "data" array. The thing is i can copy one array into another but cant seem to add 1 on first element. Hope i made it clear. Thanks for all the help
Topic archived. No new replies allowed.