Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Sorting lists (http://www.go4expert.com/forums/sorting-lists-t1537/)

928GTS 4Oct2006 11:03

Sorting lists
 
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.

shabbir 4Oct2006 11:43

Re: Sorting lists
 
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

928GTS 4Oct2006 12:07

Re: Sorting lists
 
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?

shabbir 4Oct2006 12:38

Re: Sorting lists
 
You have to write your bubbleSort routine probably.


All times are GMT +5.5. The time now is 03:49.