Arrays with pointers and references (very basic)

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 700 Views - Last Post: 27 March 2012 - 11:26 PM Rate Topic: -----

#1 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Arrays with pointers and references (very basic)

Posted 25 March 2012 - 08:24 PM

I am in an Advanced Data Structures class and I am having a rough time getting used to C++.

Basically we have to have an insert, delete, and print methods. When inserting, it is supposed to automatically sort the array without a sorting algorithm (using 2 arrays). I keep doing trial and error to no success. I understand pointers and references but it is weird to me coming from Java.

Any help would be appreciated.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

void display(int* arr,int *numptr,int sz) {
	cout << "The array is: [ ";
	for (int i = 0; i < sz; i++) 
		cout << arr[i] << ' ';
	cout << "]" << endl;
}

int insert(int* arr, int*& numptr,int& sz) {
	int *arrNew = new int[sz+1];
	for (int i = 0; i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
		}
		else {
			arrNew[i] = *numptr;
			sz++;
		}
	}
	return *arrNew;
}

void removeIt(int* arr, int*& numptr, int& sz) {

	for (int i = 0; i < sz - 1; i++) {
			if (arr[i] == *numptr) {
				arr[i]=arr[i+1];
			}
			if (sz>i+2)
				arr[i+1] = arr[i+2];					
	}
	sz--;
}

int _tmain(int* argc, _TCHAR* argv[])
{
	int choice;
	int number;
	int *numptr = &number;
	int sz = 1; // you say in your comments to start with an array of 0 or 1 length, here it is. arr[sz] is arr[1] initially;
	int *arr = new int[sz];
L1:
	cout << "Press 1 to insert a number into an array" << endl
		 << "Press 2 to remove a number into an array" << endl
		 << "Press 3 to print the array" << endl
		 << "Press 0 to quit" << endl;
	cin >> choice;
	if (choice==1) {
		cout << "What number do you want to insert?" << endl;
		cin >> number;
		//sz++;
		int arr = insert(&arr,numptr,sz);
		goto L1;
	}
	if (choice==2) {
		cout << "What number do you want to delete?" << endl;
		cin >> number;
		removeIt(arr,numptr,sz); // make sure that if the number does not exist, dont freak out;
		goto L1;
	}
	if (choice==3)
		display(arr,numptr,sz);
	if (choice=0)
		return 0;
	else
		return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Arrays with pointers and references (very basic)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: Arrays with pointers and references (very basic)

Posted 25 March 2012 - 09:15 PM

You need to return a pointer to the new array to update the old one.
Also you only need to pass the value of the number because you don't need the value updated in the calling function (main).
The size sz is good.

int* insert(int* arr, int number, int& sz)




Your loop doesn't really work because if there is several values larger than number the these numbers will be substituted by the number.

	for (int i = 0; i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
		}
		else {
			arrNew[i] = *numptr;
			sz++;
		}
	}




Perhaps, loop (copying array) until new number is to be inserted, insert new number, then use another loop to copy the rest.

Once you have done that you can have a look at deleting the old array.
Was This Post Helpful? 0
  • +
  • -

#3 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 25 March 2012 - 11:23 PM

I took what you said and implemented the corrections. I understand what I fixed but I am still having issues.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;


void display(int* arr,int *numptr,int sz) {
	cout << "The array is: [ ";
	for (int i = 0; i < sz; i++) 
		cout << arr[i] << ' ';
	cout << "]" << endl;
}

int* insert(int* arr, int*& numptr,int& sz) {
	int *arrNew = new int[sz+1];
	int i = 0;
	for ( ;i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
		}
		if (arr[i] == *numptr) {
			arrNew[i] = *numptr;
			sz++;
		}
		if (arr[i] > *numptr) {
			arrNew[i] = *numptr;
			arrNew[i+1] = arr[i];
		}
	}
	sz++;
	for ( ; i < sz; i++) {
		arrNew[i+1] = arr[i];
	}
	return arrNew;
}

void removeIt(int* arr, int*& numptr, int& sz) {

	for (int i = 0; i < sz - 1; i++) {
			if (arr[i] == *numptr) {
				arr[i]=arr[i+1];
			}
			if (sz>i+2)
				arr[i+1] = arr[i+2];					
	}
	sz--;
}

int _tmain(int* argc, _TCHAR* argv[])
{
	int choice;
	int number;
	int *numptr = &number;
	int sz = 1; // you say in your comments to start with an array of 0 or 1 length, here it is. arr[sz] is arr[1] initially;
	int *arr = new int[sz];
L1:
	cout << "Press 1 to insert a number into an array" << endl
		 << "Press 2 to remove a number into an array" << endl
		 << "Press 3 to print the array" << endl
		 << "Press 0 to quit" << endl;
	cin >> choice;
	if (choice==1) {
		cout << "What number do you want to insert?" << endl;
		cin >> number;
		//sz++;
		arr = insert(arr,numptr,sz);
		goto L1;
	}
	if (choice==2) {
		cout << "What number do you want to delete?" << endl;
		cin >> number;
		removeIt(arr,numptr,sz); // make sure that if the number does not exist, dont freak out;
		goto L1;
	}
	if (choice==3)
		display(arr,numptr,sz);
	if (choice=0)
		return 0;
	else
		return 0;
}
	



	
		






This post has been edited by WickedBetrayal: 25 March 2012 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: Arrays with pointers and references (very basic)

Posted 25 March 2012 - 11:58 PM

If you want to use a pointer I wouldn't use int*& numptr use int* numptr. You don't need to use a pointer for the number.


You could get the value using the pointer and set a local variable.

int number = *numptr;




Here the code works ok, while arr[i] is less than the number. The arr[i] doesn't ever have to equal the number. When arr[i] is greater than the number you overwrite the data with arrNew[i] = *numptr;.
	for ( ;i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
		}
		if (arr[i] == *numptr) {
			arrNew[i] = *numptr;
			sz++;
		}
		if (arr[i] > *numptr) {
			arrNew[i] = *numptr;
			arrNew[i+1] = arr[i];
		}
	}



You could make one loop work, if you use a boolean to flag, when arr[i] >= *numptr the first time, and so know when to insert the new number.


To use two loops :-
You want the first loop to copy from the old array to the new array, while the numbers in the array are less than *numptr.

So you can add the condition to the for loop

    for ( ; i < sz && arr[i] < *numptr; i++) {


Was This Post Helpful? 0
  • +
  • -

#5 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 26 March 2012 - 08:38 AM

I do not really understand what you mean about having number and numptr as a pointer or something. I think I did what you said.. Not sure though.

I made it into one loop, i am not sure if it works because when i print (display) the array i just get -xxxxxx and I am not sure how to fix that.. I am guessing it is just some kind of null value because the insert did not work or something.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;


void display(int* arr,int* numptr,int sz) {
	cout << "The array is: [ ";
	for (int i = 0; i < sz; i++) 
		cout << arr[i] << ' ';
	cout << "]" << endl;
}

int* insert(int* arr, int* numptr,int& sz) {
	int *arrNew = new int[sz+1];
	bool flag = false;
	for (int i = 0;i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
		}
		if ((arr[i] > *numptr) && (!flag)) {
			arrNew[i] = *numptr;
			flag = true;
		}
		if (flag) {
			arrNew[i+1] = arr[i];
		}
	}
	return arrNew;
}

void removeIt(int* arr, int* numptr, int& sz) {

	for (int i = 0; i < sz - 1; i++) {
			if (arr[i] == *numptr) {
				arr[i]=arr[i+1];
			}
			if (sz>i+2)
				arr[i+1] = arr[i+2];					
	}
	sz--;
}

int _tmain(int* argc, _TCHAR* argv[])
{
	int choice;
	int number;
	int *numptr = &number;
	int sz = 1; // you say in your comments to start with an array of 0 or 1 length, here it is. arr[sz] is arr[1] initially;
	int *arr = new int[sz];
L1:
	cout << "Press 1 to insert a number into an array" << endl
		 << "Press 2 to remove a number into an array" << endl
		 << "Press 3 to print the array" << endl
		 << "Press 0 to quit" << endl;
	cin >> choice;
	if (choice==1) {
		cout << "What number do you want to insert?" << endl;
		cin >> number;
		//sz++;
		arr = insert(arr,numptr,sz);
		goto L1;
	}
	if (choice==2) {
		cout << "What number do you want to delete?" << endl;
		cin >> number;
		removeIt(arr,numptr,sz); // make sure that if the number does not exist, dont freak out;
		goto L1;
	}
	if (choice==3)
		display(arr,numptr,sz);
	if (choice=0)
		return 0;
	else
		return 0;
}
	



	
		






Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: Arrays with pointers and references (very basic)

Posted 26 March 2012 - 10:24 PM

Hi, you implement the boolean flag very well. The logic with the if statements is not quite right. You don't handle the condition when the arr[i] is equal to the number. That might be the cause of the incorrect output.
Was This Post Helpful? 0
  • +
  • -

#7 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 26 March 2012 - 10:53 PM

Okay I added the following code in:
if ((arr[i] == *numptr) && (!flag)) {
			arrNew[i] = arr[i];
			arrNew[i+1] = *numptr;
		}


The print array method always prints [ -840##### ] no matter how many numbers i insert. So I am thinking my inserting is not working right or that the new array is not being made or returned correctly so it can be passed to the print method.

Thoughts?

Thanks again.

This post has been edited by WickedBetrayal: 26 March 2012 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 26 March 2012 - 11:12 PM

Okay so I have thought a lot about the logic behind my if statements... There is no way they would work. Its all theoretical. When I write "arr[i] < *numptr" arr[i] might have no value in it, so how can it compute the comparison? I need to find a way to make sure there is a value stored in the array...


 if (sz==0) {
		arrNew[0] = *numptr;
	}
	for (int i = 0;i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
			cout << "ifa";
		}
		if ((arr[i] > *numptr) && (!flag)) {
			arrNew[i] = *numptr;
			cout << "ifb";
			flag = true;
		}
		if ((arr[i] == *numptr) && (!flag)) {
			arrNew[i] = arr[i];
			arrNew[i+1] = *numptr;
			cout << "ifc";
			}
		if (flag) {
			arrNew[i+1] = arr[i];
			cout << "ifd";
		}
	} 

This post has been edited by WickedBetrayal: 26 March 2012 - 11:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 05:44 AM

If your sigs are this:
void display(int *array, int size);
void insert(int *array, int &size, int value);
void remove(int *array, int &size, int value);



Then you could use a struct:
struct Array {
	int size;
	int *data;
};

void display(Array &array);
void insert(Array &array, int value);
void remove(Array &array, int value);



Better yet, if you're doing C++, use a struct with brains:
class Array {
public:
	Array();
	~Array();
	void display() const;
	int getSize() const;
	void insert(int value);
	void remove(int value);
private:
	void grow();
	int capacity, size;
	int *data;
};

//...

void Array::display() const {
	cout << "The array is: [ ";
	for (int i = 0; i < size; i++) {
		cout << data[i] << ' ';
	}
	cout << "]" << endl;
}



Note, capacity and grow. You don't have to always grow on insert or shrink on remove. Rather, you hold the size you have and the size you can hold.

And get rid of the goto. It is NEVER a good idea.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#10 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 01:48 PM

Yeah I am very iffy about the goto's because I have had issues with them in the past.

We were told specifically to use arrays... So using a struct.. I am not sure. I can email him and ask.

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 05:31 PM

	int sz = 0; 
	int *arr = new int[sz];


I think my if statements are messed up because lets say:

1) I insert the first number into position 0.
2) When I would insert a second one, I compare that position and the one after that. But the thing is: there is nothing in position 1. It just memory trash. So the if statements will not work.

What if i make a set array size? This seems much hard than it should be.
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 07:36 PM

View PostWickedBetrayal, on 28 March 2012 - 02:31 AM, said:

	int sz = 0; 
	int *arr = new int[sz];


I think my if statements are messed up because lets say:

1) I insert the first number into position 0.
2) When I would insert a second one, I compare that position and the one after that. But the thing is: there is nothing in position 1. It just memory trash. So the if statements will not work.

What if i make a set array size? This seems much hard than it should be.



Yes, this is good.
When the array size is zero, a number is inserted to the first position (in the insert function) :

if (sz==0) {
	arrNew[0] = *numptr;
}




In this case, the statements in the for loop are not implemented, because sz is 0 and i is not less than 0.

for (int i = 0; i < sz; i++) {



Then sz should be incremented and the arrNew returned to set arr.

The next time the insert function is called sz will be 1 and the for loop wil be implemented.



The if statements could be simpler. There is the >= operator

if (arr[i] >= *numptr)
  // number is greater or equal to array element




If you use else

  if (arr[i] < *numptr) 
  {
    // number is less than 
    // array element value
  }
  else
  {
    // number is greater or equal 
    // to array element value
    if(!flag)
    {
      // !flag signalling insert point
    }
    else
    {
      // flag signalling number already inserted

    }
  }




A better name for the flag maybe inserted or inserted_flag.

This post has been edited by #define: 27 March 2012 - 07:38 PM

Was This Post Helpful? 0
  • +
  • -

#13 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 08:38 PM

Okay. I made some good changes.

But what about my problem?

If nothing is in arr[1], it is some memory garbage. like '-8720283'. It triggers the first if statement because some - memory garbage is lower than the next inserted number and then puts that into arr[1] and it will print out.

How can i check to make sure that any position in arr is actually a real number that i inserted?

Thanks again
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 08:49 PM

If the initial size is zero it should be ok.

int sz = 0; 
int *arr = new int[sz];



If you still have problems please post the new code.
Was This Post Helpful? 0
  • +
  • -

#15 WickedBetrayal  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 16-February 11

Re: Arrays with pointers and references (very basic)

Posted 27 March 2012 - 08:55 PM

I am extremely close to fixing it. My arr[i]<*numptr is where my problem lies.

int* insert(int* arr, int* numptr,int sz) {
	int *arrNew = new int[sz+1];
	bool insertFlag = false;
	if (sz==0) {
		arrNew[0] = *numptr;
		return arrNew;
	}
	arr[sz]=0;
	for (int i = 0;i < sz; i++) {
		if (arr[i] < *numptr) {
			arrNew[i] = arr[i];
			cout << "ifa";
		}
		if ((arr[i] > *numptr) && (!insertFlag)) {
			arrNew[i] = *numptr;
			cout << "ifb";
			insertFlag = true;
		}
		if ((arr[i] == *numptr) && (!insertFlag)) {
			arrNew[i] = arr[i];
			arrNew[i+1] = *numptr;
			cout << "ifc";
			}
		if (insertFlag) {
			arrNew[i+1] = arr[i];
			cout << "ifd";
		}
	}
	return arrNew;
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2