I have to find the last occurence of 'x' in an array of any given length. If the variable 'x' is not in the array then -1 should be displayed. This is what ive done so far...
Code:
#include <stdio.h>
#include <stdlib.h>
int SearchLastOccurrence(int x, int *p, int len);
int main()
{
int * p;
int i, len, x;
printf("Please enter how many integers are in the array?:");
scanf("%d", &len);
printf("Please enter the integer to search among array:\n");
scanf("%d", &x);
p = (int*)malloc(len * sizeof(int));
printf("Enter the numbers now please:\n");
for(i=0;i<len;i++)
{
scanf("%d", &p[i]);
}
printf("The index of the last occurrence is: %d\n", SearchLastOccurrence(x ,p ,len));
free(p);
return 0;
}
int SearchLastOccurrence(int x, int * p, int len)
{
int i, r;
r = -1;
for(i = 0 ; i <= len; i++)
{
if(x = *(p + i))
{
r = i;
}
}
return r;
}