Hey guys im having problems with this assignment. Thanks to all of you that helped with the sort. Now I have to put in a search function. The function I used works and the whole program works, the only problem I have is when you enter a two digit number into the array (I used 10) the search function will not return the right value. I have included my source code along with a couple of screen shots form the out put windows. In the first screen shot you can see that the search function works in the second screen shot I substitute 10 for 0 and the search function does not work. Any suggestions would be greatly appreciated.
Code:
// arr.cpp : Defines the entry point for the console application.
//
#include<stdio.h>
#include<stdafx.h>
#include"conio.h"
#define n 10
#define NOT_FOUND -1
void sort_arr(int arr[]);
int search(const int arr[]);
int main()
{
int arr[n];
printf("Enter the numbers in the array:\n ");
for(int k=0; k<n; k++)
scanf("%d", &arr[k]);
for(int t=0; t<n; t++)
{
printf("%d", arr[t]);
}
printf("\n");
sort_arr(arr);
for(int t=0; t<n; t++)
{
printf("%d", arr[t]);
}
printf("\n");
printf("Please enter the target value:\n");
search(arr);
getch();
return (0);
}
void sort_arr(int arr[])
{
int a, b, low;
for(a=0; a<n; a++)
{
b=a+1;
for(b; b<n; b++)
if(arr[a]>arr[b])
{
low=arr[a];
arr[a]=arr[b];
arr[b]=low;
}
}
}
int search(const int arr[])
{
int i, found = 0, target, where;
scanf("%d", &target);
i = 0;
while (!found && i < n) {
if (arr[i] == target)
found = 1;
else
++i;
}
/* Returns index of element matching target or NOT_FOUND */
if (found)
where = i;
else
where = NOT_FOUND;
printf("%d", where);
getch();
return (where);
}