I'm working on a bunch of stuff with arrays. At the moment I'm trying to come up with with an insert function that allows me to put as many elements as I want into an array, only keeping the five recent most elements.

So I guess what I want to do is drop the first element and push the rest back one position in the array. Will this do the job?

if ( Size < MAX_SIZE )
		Data[ Size++ ] = NewData;
	
	// Only the five recentmost numbers are subject to testing
	// so, drop the first and pushback the rest, and insert
	// newest element into spot five.
	if ( Size >= MAX_SIZE ) 
		for ( Index = 0 ; Index < MAX_SIZE - 1 ; ++Index ) 
			Data[ Index ] = Data[ Index + 1 ];
			Data[ MAX_SIZE - 1 ] = NewData;

>>Will this do the job?
Don't know -- why don't you compile and test it to see if it will work correctly. add a print statement on every insertion to see if the array was shifted correctly.

I would have done so beforehand, but my instructor gave me a "skeleton" so to speak, of a program. So we're required to make all the functions that main calls.

The problem is... the program isn't going to work unless all the functions are complete, and I'm not experienced enough to tamper with his main. So I can't test them one at a time.

I'd just like to know if I'm in the ballpark.

well you can write empty functions for now and implenent/test them one at a time. Don't try to code them all at one time or you will just get overwhelmed by all the compiler errors that you will get. As another DaniWeb member said (I don't recall who), use the Divide And Conquor method. Do one function at a time and soon you will have the program complete.

Didn't think of that, at least in that way.
Thanks.

Don't be afraid to make liberal use of { and } braces. And pay attention to spacing. Both these will help you greatly in understanding the logic of your program.

if ( Size < MAX_SIZE )
{
    Data[ Size++ ] = NewData;
}
else
{	
    // Only the five recentmost numbers are subject to testing
    // so, drop the first and pushback the rest, and insert
    // newest element into spot five.
    for ( Index = 0 ; Index < MAX_SIZE - 1 ; ++Index ) 
   {
               Data[ Index ] = Data[ Index + 1 ];
   }
   Data[ MAX_SIZE - 1 ] = NewData;
}

Okay, now I'm trying to find the newest element in an array of size 5. That is the one that was put in the array last. How would I go about doing that?

All I know is that the recentmost element is put in last, so it will have the greatest index?

In the function you say: Data[ MAX_SIZE - 1 ] = NewData; So your newest data will always be in element Data[ MAX_SIZE - 1 ]; (so if MAX_SIZE = 5 it will be in Data[4], which is the last element (0-4 = 5 elements))

hey i dont understand why do u need to develop such an application when u have this things done already in java???

hey i dont understand why do u need to develop such an application when u have this things done already in java???

I don't understand why you are replying to a two-year-old thread?

Because he's a weenie and thinks people want to use Java.

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.