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*/);
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.