dynamic arrays - return position

hi,

i've created a dynmaic array. our assignment is to write our own functions for things like push_back(), resize() etc. at the moment i'm stuck on trying to write my own at() function. this is what i've been using:


SomeClass.cpp
1
2
3
4
5
6
int ITIntVector::getPosition(int i)
{
	int *p; //pointer declaration
	p = &i; //p = the memory location of i
	return theArray[*p];
}


then in main.cpp
 
cout << v.getPosition(3) << endl; //to return index position 3 


the output i'm getting is -842150451? is this the actual memory address of p?

i'm fairly new to cpp and very new to pointers, dynamic arrays etc.

some advice/help would be much appreciated!
That works for me. Perhaps the value in theArray[*p] isn't what you think?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int theArray[] = {10, 20, 30, 40, 50 };

int getPosition(int i)
{
	int *p; //pointer declaration
	p = &i; //p = the memory location of i
	return theArray[*p];
}

int main(int argc, char* argv[]) {
	int val = getPosition(3);
	cout << "The value is " << val << endl;
	return 0;
}
thanks for the reply. here's what i have so far from the three classes of the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ITIntVector_H
#define ITIntVector_H

class ITIntVector
{
public:
	ITIntVector();
	void push_back(int val);
	int getPosition(int i);
	int getSize();
	int getCapacity();
	void resize(int i);
private:
	int theSize;
	int theCapacity;
	int *theArray;
};

#endif 


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
#include "ITIntVector.h"
#include <iostream>
using namespace std;

ITIntVector::ITIntVector()
{
	theSize = 10;
	theCapacity = 0;
	theArray = new int[theSize];
}

int ITIntVector::getSize()
{
	return theSize;
}

int ITIntVector::getCapacity()
{
	return theCapacity;
}
/*
void ITIntVector::push_back(int val)
{
	if(theSize == theCapacity)
	{
		theArray[theSize + 1] = val;
		theSize++;
	}
	//still working on this
}
*/
int ITIntVector::getPosition(int i)
{
	int *p;
	p = &i;
	return theArray[*p];
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "ITIntVector.h"
#include<iostream>

using namespace std;

int main()
{
	ITIntVector v;
	
	cout << "Capacity: " << v.getCapacity() << endl;
	cout << "Size: " << v.getSize() << endl;

	cout << v.getPosition(3) << endl;

	system("pause");
	return 0;
}


this is the way we have to 'set up' the program, .h file, a ITIntVector.cpp file and a main.cpp. we have to delcare the dynamic array in the .h file and then in the constructor of the ITIntVector.cpp file make it's size 10 (or any other size). then use each function we write in the main.cpp on an instance of ITIntVector, im my case 'v'. is it because there's nothing in the array i'm getting that output as in my first post? here's the actual question, if it makes things any clearer for you:

1. Write your own vector class ITIntVector to use as a “smart array” with integers.
Methods of your class will be things like:
push_back(int val) to add an element to the end of the vector
at(int i) to return the element at index position i in your vector
size() to return the no of elements in the array
capacity() to return the amount of space currently allocated for the array
resize(int i) to change the size of the array – setting new elements to zero if
i is larger than the present size of the array, or chopping off the end of the array if
smaller
Yes, your array's values are undefined. Once you put something in position (3), you will be able to retrieve it. At the moment, there is garbage in there.
sometimes when spending so long on something i miss the obvious!

so i got it working putting a loop in the constructor filling the array with 10 random ints, and my function getPosition() now works.

i'm working on my push_back() function which will take an int and put it to the end of the array if theSize == theCapacity, with the following function:

1
2
3
4
5
6
7
8
9
10
void ITIntVector::push_back(int val)
{
	int *p;
	p = &val;
	if(theSize == theCapacity)//if size = capacity
	{
		theArray[theSize + 1] = *p; //put the val in theSize+1 'th position of the array
		theSize++; //increase the size of the array, should this happen first?
	}	
}


and calling like this:

v.push_back(5);

which i'm hope will put the value '5' into the next available position, which will be theArray[10] in my case as [0] - [9] are full. i'm the same garbage output though when i use v.getPosition[10].

am i doing something wrong in the push_back() function?

Unfortunately, arrays aren't that flexible. Take a look at this:

http://www.cplusplus.com/forum/general/11111/

Synopsis: You're going to have to make a new array of the appropriate size, copy the values from the old to the new, destroy the old, then make the old variable point to the new array.
but from what i got from my lecturer, he wanted us to make a dynamic array which will act like a vector, and we can add an element to the end of that array, even if the array is full, just like a vector. i must have misunderstood him. anyway, i'll have a look at the link. thanks again.

basically my push_back() function should act just like the push_back() function one would use with vectors. is this possible?
Last edited on
i tried to copy, but i get a break point error?

i think i've done what you explained:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void ITIntVector::push_back(int val)
{
	/*int *p;
	p = &val;
	if(theSize == theCapacity)
	{
		theArray[theSize + 1] = *p;
		theSize++;
	}*/

	int *newArray = new int[theSize + 1];//make new array
	for(int i = 0; i < theSize; i++)
	{
		newArray[i] = theArray[i];//copy from old to new
	}
	theSize++;
	theArray = newArray;
	delete[] theArray;//destroy old

	int *p;
	p = &val;
	newArray[theSize] = *p;//make old variable point to new array
}


closed account (DSLq5Di1)
Swap these two lines,
1
2
	theArray = newArray;
	delete[] theArray;//destroy old deleting the memory allocated for newArray, previous memory is lost. 

What is the purpose of this?..
1
2
3
	int *p;
	p = &val;
	newArray[theSize] = *p; // why not.. newArray[theSize] = val ? 

Also, newArray[theSize] is out of bounds, arrays are zero based remember.
Topic archived. No new replies allowed.