I understand the algorithm SelectionSort, but once again my problem is the easy part( i think the college life is affecting my mind ).

How in the world do you read in an unknown number of ints from a file? I thought i did it right but when i run selectionsort it prints out from A[0] to A[100000] when 100000 is the maximum amount of numbers. The file i created only has about 20.

heres my code

do{
    fscanf( fin, "%d", &A[i] );
    printf( "%d ", A[ i ] );
    i++;
  }while( A[i] != EOF );

Through overboard your snippet.

int n; /* elements counter */
...
for (n = 0; fscanf(fin,"%d",&A[n])==1; ++n)
    ;

;)

thanks, when i run the program it just prints out millions of zeros... i dont understand how since theres only 10 ints that are going to be ouput.

heres my code

#include <stdio.h>

void SelectionSort( int A[], int n );

int main(int argc, char *argv[]){

  int A[100000];
  int i = 0, size;
  FILE *fin = fopen(argv[1], "r");

  printf("\nNumbers to be sorted:\n");

  for (i = 0; fscanf(fin, "%d", &A[i]) == 1; ++i)

  size = i - 2;

  SelectionSort( A, size );

  printf("\nFirst five numbers:\n");
  for(i = 0; i < 5; i++) printf("%d\n", A[i]);

  printf("\nLast five numbers:\n");
  for(i = size; i >= size - 5; i++) printf("%d", A[i]);
 fclose(fin);
  return 0;
}

void SelectionSort( int A[], int n ){
  int i, j, min_index, temp;

  for( i = 0; i < ( n - 1 ); i++ ){

    min_index = i;

    for( j= i + 1; j < n; j++)
      if( A[j] < A[min_index] )min_index = j;

    temp = A[i];
    A[i] = A[min_index];
    A[min_index] = temp;
  }
}

It turned out that you can't copy/paste two lines of code from my post (and don't understand simplest C constructs).
Look at your "improvement":

for (i = 0; fscanf(fin, "%d", &A[i]) == 1; ++i)   
    size = i - 2;
/* instead of */
for (n = 0; fscanf(fin,"%d",&A[n])==1; ++n)    
    ;

Feel the difference. After your loop variant the size variable has value "numbers_counter-2".
Well, now you sort these elements. Look at the loop below marked as "Last file numbers":

for(i = size; i >= size - 5; i++) 
    printf("%d", A[i]);

Of course, it's "loop forever": you INCREMENT i and test i >= size.

Take your C textbook and read about for loops again and again and again... ;)

for (n = 0; n >= 0; ++n)
    read_me_please();

sorry, i cant believe i was that dumb,
I got it to work heres working code.

#include <stdio.h>

void SelectionSort( int A[], int n );

int main(int argc, char *argv[]){

  int A[100000];
  int i = 0, j;
  FILE *fin = fopen(argv[1], "r");

  printf("\nNumbers to be sorted:\n");

  for (j = 0; fscanf(fin, "%d", &A[j]) == 1; j++)

  SelectionSort( A, j + 1 );

  printf("\nFirst five numbers:\n");
  for(i = 0; i < 5; i++) printf("%d, ", A[i]);

  printf("\nLast five numbers:\n");
  for(i = j - 6; i < j; i++) printf("%d, ", A[i]);

  fclose(fin);
  return 0;
}


void SelectionSort( int A[], int n ){
  int i, j, min_index, temp;

  for( i = 0; i < ( n - 1 ); i++ ){

    min_index = i;

    for( j= i + 1; j < n; j++)
      if( A[j] < A[min_index] )min_index = j;

    temp = A[i];
    A[i] = A[min_index];
    A[min_index] = temp;
  }
}

Yet another free advice: don't declare (very) large arrays as automatic (local, as usually allocated on the program stack). Better use malloc and don't forget to free obtained pointers:

#define HUGE_SIZE 1000000
...
int* arr = malloc(sizeof(*arr)*HUGE_SIZE);
... arr[i] - same code as for array
free(arr);

It's impossible to define the strict boundary for "huge" array size, of course.

This article has been dead for over six months. Start a new discussion instead.