I printed the sorted array and modified some things and it seems to be working fine now but I'm still not sure. I received an error after it worked one time but I accidentally closed it before reading it. Other than that I have no errors and It's working. I'm hoping it's fine. This is the code. Thank you for your response.
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10], i, j, x, n, inf, supp, mid, M = 0, flag=0;
printf("How many elements does your vector have?\n");
scanf("%d", &n);
printf("What are those elements?\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("What value do you need to find?\n");
scanf("%d", &x);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (a[i] < a[j])
{
M = a[i];
a[i] = a[j];
a[j] = M;
}
}
}
printf("your sorted vector is:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
printf("\n");
flag = 0;
inf = 0;
supp = n;
while (flag == 0 && inf <= supp)
{
mid = (int)((inf + supp) / 2);
if (x == a[mid])
{
printf("the value you seek is at position: %d", mid);
flag = 1;
}
else
if (x > a[mid])
inf = mid;
else
supp = mid;
}
getch();
}