HELP with file input with array, selection sort...

This is a discussion on HELP with file input with array, selection sort... within the C++ Programming forums, part of the General Programming Boards category; I'm basically trying to get this to read in a file called something like foo.dat using at the command line ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    HELP with file input with array, selection sort...

    I'm basically trying to get this to read in a file called something like foo.dat using at the command line ./foo foo.dat foo.txt, where ./foo is the compiled program, foo.dat is the input file, and foo.txt is the output file. I'm trying to read the doubles from foo.dat and sort them using selection sort, and also print the results and store them in a file called foo.txt. I'll be honest some of this code probably makes no sense becuase i'm not too sure on what i'm doing, but if you can provide me any help at all I appreciate it.
    Code:
    #include <iostream> 
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <cctype> 
    using namespace std;
    
    const int MAXSIZE = 56; 
    
    void selectionSort(int arr[], int size); 
    void swap(int& x, int& y); 
    
    int main(int argc, char *argv[]) 
    { 
      const int MAXSIZE = 56;
      int numbers[MAXSIZE] = {0}; 
      int k; 
      int ImANumber;
      char ch;
    
    ifstream fin(argv[1]);
    ofstream fout(argv[2]);
     fin >> ImANumber;
     
    
    cout << endl << "BEFORE SORT: "; 
    for (k = 0; k < MAXSIZE; k++) 
      cout << numbers[k] << endl; 
        
    selectionSort(numbers, MAXSIZE); 
    
    cout << endl << endl; 
    cout << "AFTER SORT: "; 
    for (k = 0; k < MAXSIZE; k++) 
      cout << numbers[k] << " "; 
      
    cout << endl << endl << endl;  
    
     fin.close();
     fout.close();
    return 0;
     
    }// end main() 
    
    void selectionSort(int arr[], int size)
    { 
      int indexOfMin, pass, j; 
      
      for (pass = 0; pass < size - 1; pass++) 
        { 
          indexOfMin = pass; 
          
          for (j = pass + 1; j < size; j++) 
    	if (arr[j] < arr[indexOfMin]) 
    	  indexOfMin = j; 
          
          swap(arr[pass], arr[indexOfMin]); 
        } 
    }// end selectionSort() 
    
    void swap(int& x, int& y) 
    { 
      int temp; 
      temp = x; 
      x = y; 
      y = temp; 
    }// end swap()

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    everything looks fine but one thing... you arent taking in anything from the file you open, you just open and close it. you need to assign the numbers from the file to your array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21