Undetermined Array Element

What's going on guys. I got a question that I have been stuck on. So I'm trying to find a certain value in an array and swap it to the last element of the array. The problem is that the array size is determined by the program user. I know how to search and swap, so no code is needed, but my only issue is knowing what the last element is. Anybody got an answer?
Thank You.

Below is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int linsearch(int arr[], int limit, int key) /*Key = the value to be searched
for. Limit = the size of the array that was determined by the user */
{
	int index = 0;
	while (index < limit)
	{
		if (key == arr[index])
		{
			return index;
		}
		index++;
	}
	return -1;
}
swap(arr[index],/*the last element of the array*/);
Last edited on
The last element of the array has index of (size - 1), where size is the size of the array. In this case, the last element would be arr[limit-1].
Last edited on
Since the size of the array will not be known at compile time, you will have to dynamically allocate it and tell the linsearch function how big it is when you call it.
Cool. Thanks guys. And yes the array is dynamically allocated. I know it's hard to tell since I didn't display the entire program. Thanks again.
Topic archived. No new replies allowed.