Sorting lists

Discussion in 'C' started by 928GTS, Oct 4, 2006.

  1. 928GTS

    928GTS New Member

    Joined:
    Oct 4, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello I have a project in which I must merge two text files which contain a variable number of numbers. Both of these text files are sorted from lowest to highest value and when they are merged into one list must retain this property.

    My friend and I were working on developing code for this but we have no idea if this is right. This is what we have so far...
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    #include <vector>
    #include <list>
    
    int main(int argc, char argv[])
    {
    
      using namespace std; 
      
      ifstream in_stream1;
      ofstream out_stream;
      ifstream in_stream2;
    
      in_stream1.open("prob2list1.txt");
      in_stream2.open("prob2list2.txt");
      out_stream.open("prob2merged.txt");
      
      int num1;
      int num2;
      int i;
      int j;
    
      int mergeList[10000];
     
     
      while(in_stream1.eof!=0)
        {
          in_stream1>>num1;
          mergeList[i]= num1;
          i++;
        }
      
      while(j<i)
        {
          in_stream2>>num2;
          if(mergeList[j]>num2)
    	j++;
          else 
    	{
    	  if(j==0)
    	    {
    	      mergeList.insert(j,1);
    	      mergeList[0]=num2;
    	    }
    	  else
    	    {
    	      mergeList.insert(j,1);
    	      mergeList[j-1]=num2;
    	    }
    	  j++;
    	}
        }
    
      return 0;
     }
    We're really quite confused and so we just tried to sort if using a really,really large array but when we try to compile it we receive these errors:

    Line 30 - Error:Invalid use of member<did you forget the &?>
    Line 46 - Error:Request for member 'insert' in 'mergeList', which is of non-class type 'int[10000]'
    Line 51 - Error:Request for member 'insert' in 'mergeList', which is of non-class type 'int[10000]'

    I know this code probably won't do the job in the first place and so any assistance would be greatly appreciated,thanks.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are trying to insert an element into an array but thats not allowed you can assign a value to the array at any of the index.
    E.g.
    mergeList[j]=1;// at index j have the value 1
     
  3. 928GTS

    928GTS New Member

    Joined:
    Oct 4, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your suggestions,I'll try working on them tomorrow morning and I'll tell you how they go.

    If we wanted to do the exact same thing as the above but this time sort a list of a KNOWN size(say 1000 numbers in each file),could we use a bubble sort function to do so?

    We have this code...

    Code:
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    #include <vector>
    #include <list>
    
    int main( )
    {
    
      using namespace std; 
      
      ifstream in_stream1;
      ofstream out_stream;
      ifstream in_stream2;
    
      in_stream1.open("prob2list1.txt");
      in_stream2.open("prob2list2.txt");
      out_stream.open("prob2merged.txt");
      
      int numbers[2000];
      int array_size;
      int num;
      int i,j;
    
        while(i<2000)
        {
          in_stream1>>num;
          numbers[i]=num;
          i++;
        }
      
    
      void bubbleSort(int numbers[], int array_size);
      {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
      }
    
       bubbleSort(numbers, 2000);
    
        while(j<2000)
          {
    	numbers[j]=num;
    	out_stream<<num<<"\n";
    	  }
      
      return 0;
     }
    
    We only have one error and its this...

    "undefined reference to 'bubbleSort<int*, int>'
    collect2: ld returned 1 exit status"


    I have never seen this error before,whats up?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have to write your bubbleSort routine probably.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice