show_int_vector method RETURNS MEMORY LOCATION FOR EVERTHING EXCEPT FOR LAST VALUE.


main

#include "my_int_vector.h"
#include <iostream>

using namespace std;
int main()
{
	my_int_vector b(0);
	b.push_back(10);
	//b.show_int_vector(b);
	b.push_back(122);
	b.show_int_vector(b);
	//b.push_back(20000);
	return 0;
}

Interface

class my_int_vector
{
public:
	//constructor to create an array
	my_int_vector(int intial);
	
	//prints out the vector that contains the numbers
	void show_int_vector(my_int_vector& b);

	//returns value at arrayindex
	int at(size_t arrayindex);
	
	//returns value at arrayindex
	int operator[](size_t array_index);

	//add value to end of array
	void push_back(size_t user_number);

	//delete value from end of array
	void pop_back();

	//returns total number of elements that can be stored in the list
	int capacity();

	
private:

	//array to store numbers
	int* numbers;
	//array of temp values
	int* temp;
	//array of values which need to be printed which would be the valid ones
	int* print;
	//
	size_t array_index;
};

Implementation

#include <iostream>
#include "my_int_vector.h"
using namespace std;

my_int_vector::my_int_vector(int intial)
{
	if(intial==0)
	{
		array_index=intial;
		numbers=NULL;
		temp=NULL;
	}
	else if(intial>0)
	{
		array_index=intial;
		numbers=NULL;
		numbers=new int[array_index];
		temp=NULL;
	}
}

int my_int_vector::at(size_t arrayindex)
{
	return numbers[arrayindex];
}

int my_int_vector::operator [](size_t array_index)
{
	return numbers[array_index];
}

void my_int_vector::push_back(size_t user_number)
{
	if(array_index==0)
	{
		array_index=array_index+1;
		numbers=new int[array_index];
		numbers[0]=user_number;
	}
	else
	{
		temp=new int[array_index];
		for(size_t a=0; a<array_index; a++)
		{
			temp[a]=numbers[a];
			
		}
		delete[] numbers;
		numbers=NULL;
		array_index=array_index+1;
		numbers=new int[array_index];
		for(size_t b=0; b<array_index-2; b++)
		{
			numbers[b]=temp[b];
		}
		delete [] temp;
		temp=NULL;
		numbers[array_index-1]=user_number;
	}
}

void my_int_vector::pop_back()
{
	
	temp=new int[array_index];
	for(size_t c=0; c<array_index; c++)
	{
		temp[c]=numbers[c];
	}
	array_index=array_index-1;
	numbers=new int[array_index];
	for(size_t h=0; h<array_index; h++)
	{
		numbers[h]=temp[h];
	}
	delete [] temp;
	temp=NULL;
}

int my_int_vector::capacity()
{
	return array_index;
}

void my_int_vector::show_int_vector(my_int_vector& b)
{
	cout<<numbers[0]
		<<numbers[1]<<"n";

}

Recommended Answers

All 9 Replies

Please tell me what is wrong.

Not enough?
How about looking at your needlessly complicated push_back()?

commented: Just told me the function was wrong didn't really explain anything. +0
commented: Counter. This is actually good advice +29

Maybe a bit more explanation would help. STILL CONFUSED ABOUT WHAT IS WRONG WITH FUNCTION.

please tell me what is wrong with my push_back function?

Lemme get this straight, you wrote some code but you don't have a really good idea of how it's "SUPPOSED" to work?

This is basically voodoo programming; write some mystic runes, sacrifice a goat, compile it and when it crashes scream "the gods have spoken!".

Get a debugger, put a breakpoint at the start of the code and MAKE SURE for yourself that it:
a) copies the old vector in its entirety,
b) appends exactly one element,
c) has exactly the right amount of space for all of the above.

This is a trivial program, if you can't debug this, then you're basically S-o-L as a programmer.

Sure I could tell you what's wrong, but next week you'd be lying at the bottom of the next hole you've dug for yourself wondering exactly the same things (wtf happened, where's my daniweb, post and relax).

Hehe, I guess SALEM's Right As Always ;)

Btw, For the OP, Here's a Tip that would help you out.

Check out how "Pointers work." If you figure that out, I guess you will find that its absolutely unnecessary to use a member temp and Also recognise that you are complicating the code unnecessarily.

thanks for the help. I figured it out.

commented: Excellent - that's the spirit! +17

JUST WONDERING IF THIS IS BETTER CODE.
test

#include "my_int_vector.h"
#include <iostream>

using namespace std;
int main()
{
	my_int_vector b(0);
	b.push_back(10);
	//b.show_int_vector(b);
	b.push_back(122);
	b.show_int_vector(b);
	//b.push_back(20000);
	return 0;
}

Implementation

#include <iostream>
#include "my_int_vector.h"
using namespace std;

my_int_vector::my_int_vector(int intial)
{
	if(intial==0)
	{
		array_index=intial;
		numbers=NULL;
		//temp=NULL;
	}
	else if(intial>0)
	{
		array_index=intial;
		numbers=NULL;
		numbers=new int[array_index];
		//temp=NULL;
	}
}

int my_int_vector::at(size_t arrayindex)
{
	return numbers[arrayindex];
}

int my_int_vector::operator [](size_t array_index)
{
	return numbers[array_index];
}

void my_int_vector::push_back(size_t user_number)
{
	int* temp;
	if(array_index==0)
	{
		array_index=array_index+1;
		numbers=new int[array_index];
		numbers[0]=user_number;
		cout<<"from if"<<numbers[0]<<"\n";
	}
	else
	{
		array_index=array_index+1;
		temp=numbers;
		numbers=new int[array_index];
		for(size_t i=0; i<=array_index-2; i++)
		{
			numbers[i]=temp[i];
		}
		numbers[array_index-1]=user_number;

	}

	
}

/*void my_int_vector::pop_back()
{
	
	temp=new int[array_index];
	for(size_t c=0; c<array_index; c++)
	{
		temp[c]=numbers[c];
	}
	array_index=array_index-1;
	numbers=new int[array_index];
	for(size_t h=0; h<array_index; h++)
	{
		numbers[h]=temp[h];
	}
	delete [] temp;
	temp=NULL;
}*/

int my_int_vector::capacity()
{
	return array_index;
}

void my_int_vector::show_int_vector(my_int_vector& b)
{
	cout<<numbers[0]<<numbers[1]<<"\n";

}

Interface

class my_int_vector
{
public:
	//constructor to create an array
	my_int_vector(int intial);
	
	//prints out the vector that contains the numbers
	void show_int_vector(my_int_vector& b);

	//returns value at arrayindex
	int at(size_t arrayindex);
	
	//returns value at arrayindex
	int operator[](size_t array_index);

	//add value to end of array
	void push_back(size_t user_number);

	//delete value from end of array
	//void pop_back();

	//returns total number of elements that can be stored in the list
	int capacity();

	
private:

	//array to store numbers
	int* numbers;
	//
	size_t array_index;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.