Archived

This topic is now archived and is closed to further replies.

Destroyer

Copying integer arrays and animation

Recommended Posts

Hi, This is one of those "I think there''s an error in this piece of code" posts. I was trying to write a function for a game I''m writing that would recieve an integer array, then check if the arrays are the same, and it would return if they were the same. Then it would go about copying all the values of array passed to the function to int* declared in the class. Anyhoo I layed out a console app that anyone can copy and compile and run it.
  
#include <windows.h>

#include <iostream.h>
#include <string.h>
#include <stdio.h>

int sequence[5] = { 2, 4, 6, 7, 0};
int sequence2[6] = { 3, 1, 8, 9, 34, 0};
int sequence3[5] = { 3, 5, 4, 2, 0};

int anim_index = 0;

void Print_Results(void);
bool Set_Animation(int* sequence);

int *animsequence;

int main(void)
{
	animsequence = new int[2];
	animsequence[0] = 1;
	animsequence[1] = 0;

	Set_Animation(sequence);

	for(int index = 0; index < 5; index++)
	{
		cout << animsequence[index] << '' '';
	}
	cout << ''\n'';

	delete[] animsequence;
	return(0);
}

bool Set_Animation(int* sequence)
{
	int nofeinseq;
	int nofeinanimseq;

	nofeinseq = sizeof(sequence) / sizeof(int);
	nofeinanimseq = sizeof(animsequence) / sizeof(int);

	if(nofeinseq == nofeinanimseq)
	{
		for(int index = 0; index < nofeinseq; index++)
		{
			if(sequence[index] == animsequence[index])
			{
				return false;
			}
		}
	}

	// so the animation has changed...


	anim_index = 0;

	delete[] animsequence;
	
	animsequence = new int[nofeinseq];

	for(int index2 = 0; index2 < nofeinseq; index2++)
	{
		animsequence[index2] = sequence[index2];
	}

	return true;
}
  
I just wrote a quick for loop there to print out the results of the array I just passed it. When I run it, it seems it only prints off the first digit properly, the rest is pure garbage. Any help? Any speed ups, ways to improve the code (and getting it to work!) ? Thanks, Destroyer

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I suggest using a vector, easy to use, resizes when it needs to, etc..
  
#include <vector> //note: no .h on the end
using namespace std; //avoids namespace issues


vector<int> betterArray; //declares a vector, starts at size 0

betterArray.push_back(5); //puts a number in it, size==1 now

//push_back some more numbers or get them in some other way

int i = betterArray[0];//i==5 now

betterArray[0]=3; //sets the value

vector<int> array2;
array2 = betterArray; //copies everything over

if(betterArray==array2) cout<<"same"; else cout<<"different";

void DoSomething(vector<int>& a){}//passes by reference

void DoSomething(vector<int> a){}//passes by value

Share this post


Link to post
Share on other sites
std::vector does not provide operator= or operator==. Sorry, AP, but don''t misinform others.

quote:

	nofeinseq = sizeof(sequence) / sizeof(int);
nofeinanimseq = sizeof(animsequence) / sizeof(int);


This is your first problem. Performing sizeof on a pointer returns 4 on 32-bit systems - always. Sizeof an int is also 4, so you get 1 for both of these values. There is no way to dynamically determine the size of an array other than iterating across its elements - and it must have a "marker" value to signifiy its end. For this reason, it may be adviseable to use std::vector (it has a size() method).

Otherwise, you''ll have to keep track of the size of animsequence and pass the size of sequence as a parameter to Set_Animation. You''d then compare those sizes to see if they''re equal.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
according to http://www.sgi.com/tech/stl/Vector.html it does.
copy and pasted:

vector& operator=(const vector& The assignment operator

bool operator==(const vector&,
const vector&

Tests two vectors for equality. This is a global function, not a member function.

Share this post


Link to post
Share on other sites
Hi,

Well thanks - I thought I maybe able to tell the size of the array dynamically - damn

Oh well...

Then could someone please tell me why this doesn''t work...

  
#include <windows.h>

#include <vector>

#include <iostream.h>
#include <string.h>
#include <stdio.h>

int sequence[5] = { 2, 4, 6, 7, 0};
int sequence2[6] = { 3, 1, 8, 9, 34, 0};
int sequence3[5] = { 3, 5, 4, 2, 0};

int anim_index = 0;

void Print_Results(void);
bool Set_Animation(vector<int> sequence);

//int *animsequence;


vector<int> animsequence;

int main(void)
{
animsequence = new int[2];
animsequence[0] = 1;
animsequence[1] = 0;

Set_Animation(sequence);

for(int index = 0; index < 5; index++)
{
cout << animsequence[index] << '' '';
}
cout << ''\n'';

delete[] animsequence;
return(0);
}

bool Set_Animation(vector<int> sequence)
{
int nofeinseq;
int nofeinanimseq;

nofeinseq = sizeof(sequence) / sizeof(int);
nofeinanimseq = sizeof(animsequence) / sizeof(int);

if(nofeinseq == nofeinanimseq)
{
for(int index = 0; index < nofeinseq; index++)
{
if(sequence[index] == animsequence[index])
{
return false;
}
}
}

// so the animation has changed...


anim_index = 0;

delete[] animsequence;

animsequence = new int[nofeinseq];

for(int index2 = 0; index2 < nofeinseq; index2++)
{
animsequence[index2] = sequence[index2];
}

return true;
}

void Print_Results(void)
{
int numofeinanimseq = sizeof(animsequence) / sizeof(int);

for(int index = 0; index < numofeinanimseq; index++)
{
cout << animsequence[index] << '' '';
}
cout << ''\n'';

return;
}


It''s only when I inserted the #include piece of code...
I also don''t want to have to use that stupid "using namespace std" is there any way around it? If I try #include it says the file was not found (This was on MSVC++). Also is there anyway to declare a vector like vector whatever = { 1, 2 }; I know that won''t work, but is there anyway to initialize it without having to set the individual values one at a time? I''m sorry this is a really dumb and probably very easily answered question - I''m not too good at C++ nor it''s stl. That''s becuase I read the books "The C++ Programming Language (Bjarner Stroustrup)" and "C++ for dummies" possibly the 2 most awkward books to learn C++ from, I gotta different C++ book - in fact the one recommended here on GameDev And I still have yet to go through it after I finish the Windows Programming book ;P

Oh - I gotta learn when to stop blabing...

Thanks.


Eric

//----------------------------
In a world without boundaries -
who needs gates or windows ?
----------------------------\\

Share this post


Link to post
Share on other sites
Oh and another quick question - is there anyway to change my user name on the gamedev forums without making a new account?

Heh

Thanks

Eric

//----------------------------
In a world without boundaries -
who needs gates or windows ?
----------------------------\\

Share this post


Link to post
Share on other sites