sorting arrays

This is a discussion on sorting arrays within the C++ Programming forums, part of the General Programming Boards category; i need to sort these arrays after they've read the input file. in that file (i'll post it at the ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    sorting arrays

    i need to sort these arrays after they've read the input file. in that file (i'll post it at the bottom) the first column of numbers needs to be sorted numerically, ascending. (and all the rows need to kept together as well) i know i need to use a temporary variable to pass information from one array to another, but i don't know what to do with multiple arrays and mulitple columns and rows of data. i figure i would need to use a for loop in the process, possibly two... but really i'm quite confused about how to do this. i've got the basic function in there but i don't know what to do.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    const int LENGTH = 30;
    
    int family[LENGTH];
    
    int item[LENGTH];
    
    int quantity[LENGTH];
    
    double price[LENGTH];
    
    int buildArrays (int family[], int item[], int quantity[], double price[])
    {
    double num;
    ifstream inFile;
    inFile.open( "data7.txt" );
    
    if( inFile.fail() )
    {
    	cout << "text failed to open";
    	exit( -1 );
    };
    int i=0;
    while (inFile)
    {
    
    	inFile>>num;
    	family[i]=num;
    	inFile>>num;
    	item[i]=num;
    	inFile>>num;
    	quantity[i]=num;
    	inFile>>num;
    	price[i]=num;
    	i++;
    	
    	}
    	inFile.close();
    	return (i);
    	
    }	 
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	
    
    	cout<<"Annual Garage Sale";
    	cout<<"\nFamily    Item Id #    Sale Price    Quantity Sold    Sale Amount";
    	cout<<"\n-------------------------------------------------------------------";
    	int i;
    	for(i=0;i<30;i++)
    	{
    		family[i];
    		item[i];
    		price[i];
    		quantity[i];
    	
    	
    		cout<<"\n"<<family[i]<<"     "<<item[i]<<"    "<<price[i]<<"    "<<quantity[i]<<"     ";
    	};
    	
    }	 
    void sortarrays(int family[], int item[], int quantity [], double price[])
    {
    
    
    
    }
    
    
    
    
    int main()
    {
    	buildArrays(family, item, quantity, price);
    	printArrays(family, item, quantity, price);
    	
    
    	
    	
    	
    	
      
    return 0;
    }

    this is the text file

    Code:
    3 12345 1 15.95
    2 67800 3 50.00
    4 32145 2 4.50
    6 98765 1 75.00
    1 44496 2 11.30
    0 67356 4 3.50
    5 54862 1 52.50
    4 14541 1 4.25
    2 64488 2 1.00
    0 34945 3 7.40
    6 74322 1 6.50
    1 39240 10 0.75
    3 52525 15 1.00
    0 60002 1 12.00
    5 31579 2 2.50

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Keeping the data from the rows together gets much harder to do when the data is stored in individual arrays. You REALLY need to use an array of structs/classes instead of multiple arrays:
    Code:
    struct record
    {
        int family;
        int item;
        int quantity;
        double price;
    };
    
    record recs[LENGTH];
    The easiest way to sort would be to then overload operator< for the struct and then the sorting would be done with a simple one-line call to the templated std::sort function declared in the <algorithm> header.

    And you still have the problem in your read loop I mentioned twice earlier... processing of an extra row of data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    thanks. yeah i'm still working on getting this all sorted out. there's too much that i'm terribly confused about at the moment, i'll fix the more minor things when i get this mostly worked out. thanks for the help.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Maybe it will help you : ))

    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    struct record
    {
      int family;
      int item;
      int quantity;
      double price;
    };
    bool operator< (const record& lhs, const record& rhs)
    {
      return lhs.family < rhs.family;
    }
    
    void FillRecords(vector<record>& records, 
                     const vector<double>& data)
    {
      record rec;
      for(size_t i = 0; i < data.size(); i += 4)
      {
        rec.family = (int)data[i];
        rec.item = (int)data[i+1];  
        rec.quantity = (int)data[i+2];
        rec.price = data[i+3];
        records.push_back(rec);
      }
    }
    
    int main()
    {
      vector<double> data;
      copy(istream_iterator<double>(ifstream("data7.txt")), 
        istream_iterator<double>(), 
        back_inserter(data));
    
      vector<record> records;
      FillRecords(records, data);
    
      sort(records.begin(), records.end());
    
      cout << "Records out:" << endl;
      cout << "Family \t Item \t Quantity \tPrice" << endl;
    
      for(size_t i = 0; i < records.size(); ++i)
      {
        cout << records[i].family   << "\t"
             << records[i].item     << "\t\t"
             << records[i].quantity << "\t"
             << records[i].price    << endl;
      }
       
      return 0;
    }

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Quote Originally Posted by Vasya View Post
    Code:
    void FillRecords(vector<record>& records, 
                     const vector<double>& data)
    {
      record rec;
      for(size_t i = 0; i < data.size(); i += 4)
      {
        rec.family = (int)data[i];
        rec.item = (int)data[i+1];  
        rec.quantity = (int)data[i+2];
        rec.price = data[i+3];
        records.push_back(rec);
      }
    }
    
    int main()
    {
      vector<double> data;
      copy(istream_iterator<double>(ifstream("data7.txt")), 
        istream_iterator<double>(), 
        back_inserter(data));
    
      vector<record> records;
      FillRecords(records, data);
    I hope you're trying to obfuscate your code. Copying from the ifstream instead of opening it and extracting from it just makes reading from it a greater leap of faith. Just use a more conventional file-reading idiom, they're easier to debug; that's more help than simply showing people how you can, imo.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    I hope you're trying to obfuscate your code. Copying from the ifstream instead of opening it and extracting from it just makes reading from it a greater leap of faith. Just use a more conventional file-reading idiom, they're easier to debug; that's more help than simply showing people how you can, imo.
    You are right, my code is redundant. More properly be so :

    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    struct record
    {
      int family;
      int item;
      int quantity;
      double price;
    };
    bool operator< (const record& lhs, const record& rhs)
    {
      return lhs.family < rhs.family;
    }
    
    void FillRecords(vector<record>& records,
                     ifstream& fin)
    {
      record rec;
      while(fin) {
        fin >> rec.family;
        fin >> rec.item;
        fin >> rec.quantity;
        fin >> rec.price;
        records.push_back(rec);
      }
    }
    
    int main()
    {
      ifstream fin("data7.txt");
    
      if (fin.fail()) {
        cout << "text failed to open" << endl;
        return -1;
      }
    
      vector<record> records;
      FillRecords(records, fin);
    
      sort(records.begin(), records.end());
    
      cout << "Records out:" << endl;
      cout << "Family \t Item \t Quantity \tPrice" << endl;
    
      for(size_t i = 0; i < records.size(); ++i)
      {
        cout << records[i].family   << "\t"
          << records[i].item     << "\t\t"
          << records[i].quantity << "\t"
          << records[i].price    << endl;
      }
    
      return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Vasya
    You are right, my code is redundant.
    Incidentally, it also should not have compiled, unless you were using a compiler extension. The reason is that you cannot initialise an istream_iterator with a temporary istream because the istream_iterator's constructor requires a non-const reference to an istream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User shyrenee89's Avatar
    Join Date
    Feb 2010
    Location
    NIU
    Posts
    3
    joeman, did you figure out the sortArray? if so can you like pm me or something? im kinda lost there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

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