Code:
#include <math.h>
#include <stdio.h>
/* selection sort of an array */
#include <stdio.h>
#define NSIZE 1000
int get_min_range(int list[], int first, int last);
void select_sort(int list[], int n);
int search(const int arr[], //input - array to search
int target, int n) ; //input - value searched for
int main()
{
int array[NSIZE], size, num, i;
FILE *fp;
fopen("data.txt", "r");
size = 0;
while(fscanf(fp, "%d", &num) == 1)
array[size++] = num;
printf("\narray before sorting = ");
for (i = 0; i < size; ++i)
printf(" %d",array[i]);
select_sort(array, size); // selection sort
printf("\narray after sorting = ");
for (i = 0; i < size; ++i) {
printf(" %d",array[i]); }
int input;
printf("\nPlease enter a value (-1 = done)> ");
scanf("%d", &input);
while (input != -1) {
int x = search(array, input, size);
printf("%d is located at position in the array after %d comparisons \n", input, x);
printf("Please enter a value (-1 = done)> ");
scanf("%d", &input);
}
fclose(fp);
return 0;
}
/* sort array list[] by selection sort */
void
select_sort(int list[], int n)
{
int cur_index, temp, index_of_min;
for (cur_index = 0; cur_index < n-1; ++cur_index) {
/* Find position of smallest element in unsorted subarray */
index_of_min = get_min_range(list, cur_index, n-1);
/* Exchange elements at fill and index_of_min */
if (cur_index != index_of_min) {
temp = list[index_of_min];
list[index_of_min] = list[cur_index];
list[cur_index] = temp;
}
}
}
/* Find the position of the smallest element in the subarray */
int
get_min_range(int list[], int first, int last)
{
int i, index_small, temp;
temp = list[first];
index_small = first;
for (i = first; i <= last; ++i)
if (list[i] < temp){
temp = list[i];
index_small = i;
}
return (index_small);
}
int
search(const int arr[], //input - array to search
int target, //input - value searched for
int n) //input - number of elements to search
{
int i,
found = 0, //whether or not target has been found
where; //index where target found or NOT_FOUND
i = 0;
int max = n-1;
int min = 0;
int middle;
int count = 0;
while (!found && min < max) {
count = count + 1;
middle = min + (max-min)/2;
if (arr[middle] == target)
found = 1;
else if (target > arr[middle])
min = middle +1;
else
max = middle - 1;
++i;
}
if (found)
where = count;
else
where = -1;
return (where);
}
now its saying warning fp is used uninitialized in this function on line 21. Does this mean that my file is missing from my directory?