This C program needs to read a file containing integers up t
This C program needs to read a file containing integers (up to 1000 ints). The file will be classified as an array and the data needs to be sorted using an odd-even sort (smallest to largest). After that, a user will search the index and the output should return the integer that is inside that index which the user search. (i.e. a file containing integers(4 18 1 24 12)----> This file will be sorted using odd-even sort(1,4, 12, 18, 24)---->The program will ask the user to enter an index --> User enters index --> Output should display what number is in that index --> enter another index again ---> display --> If user enters -1, program will exit).
Solution
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *numFile;
numFile = fopen(\"numbers.txt\", \"r\");
//reading numebrs into array from file
int numbers[1000];
int i,j,temp,index;
if (numFile == NULL)
{
printf(\"Error occured in reading file\ \");
exit (0);
}
// reading each number
for (i = 0; i < 1000; i++)
{
fscanf(numFile, \"%d,\", &numbers[i] );
}
// sorting numbers ascending order
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (numbers[j] > numbers[j+1]) /* For decreasing order use < */
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
//reading index and dispaly value
while(1){
printf(\"Enter index value (-1 to exit): \");
scanf(\"%d\",&index);
if(index < 0){
break; // for negative values
}
else{
printf(\"Value at index %d is %d\",index,numbers[index]);
}
}
// closing each file
fclose(numFile);
return 0;
}

