DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Posts
    10

    read file then sort. need help sorting. c++

    Hi Everyone,
    I want to reference count in the ReadFile function. That works fine, however I also want to pass th my sort function is not working. Please help.

    Code:
    //Functions.h
    #include <iostream>
    #include <fstream> 
    #include <string>
    
    using namespace std;
    
    bool ReadFile (int myArray[], int &t);
    int sortArray(arr, count);
    
    int main()
    {
    int i = 0;
    int &count = i;
    int arr[300];
    
    fileFound=ReadFile(arr, count);
    sortArray(arr, count);
    
    cin.get();
    exit(1);
    }
    
    //Functions.cpp
    #include <fstream> //for ifstream
    #include <string>
    
    using namespace std;
    #define FILE_IN "Numbers.txt"
    
    bool ReadFile (int myArray[],int &t)
    {
    
        ifstream rdFile;	//Input Object
    	rdFile.open(FILE_IN);
        int n;
    	if (!rdFile)
    	{
    		return false;
    	}
    	else  
    	
    	while(!rdFile.eof())
    	{
    		rdFile >> n;
    		myArray[t]= n;
    		++t;
    	
    	}
    	   rdFile.close();
    	   return t;
    		return true;
         
    }
    
    
    void sortArray(int myArray[], int &t)
    {
    	
    int i , j, temp;
    
    for (i = 0; i < t; i ++)
    {
    	for (j = i; j< t; ++j)
    	{
    	if (myArray[j-1] >myArray[j])
    	{
    		temp = myArray[j];
    		myArray[j] = myArray[j-1];
    		myArray[j-1] = temp;
    	}
    
    	}
    }
    
    
    
    }

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    your algorithm assumes that after 1 pass of changing adjacent elements, your smallest possible value is in the first location. That can't be right.
    Try it with j = 1, which I think works.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    not sure but it may be that your largest value is in the last location, a similar smart loop of 1...N, 1...n-1, 1...n-2 etc may also work.

  4. #4
    Join Date
    Apr 2007
    Posts
    10
    Thanks it is working now.

Similar Threads

  1. Replies: 3
    Last Post: 08-22-2007, 01:58 PM
  2. Replies: 4
    Last Post: 03-14-2007, 08:27 AM
  3. Using ADO to read tab-delimited file
    By suvi65 in forum VB Classic
    Replies: 1
    Last Post: 03-01-2006, 10:10 AM
  4. Replies: 1
    Last Post: 04-02-2002, 10:45 PM
  5. How to use Java to lock a file
    By Cynthia Leslie in forum Java
    Replies: 3
    Last Post: 06-09-2001, 06:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links