Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Programming > C-C++

Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 10-04-2006, 11:03 AM   #1
Newbie Member
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
928GTS is on a distinguished road

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.
928GTS is offline   Reply With Quote
Old 10-04-2006, 11:43 AM   #2
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,744
Thanks: 130
Thanked 294 Times in 228 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

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
shabbir is offline   Reply With Quote
Old 10-04-2006, 12:07 PM   #3
Newbie Member
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
928GTS is on a distinguished road

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?
928GTS is offline   Reply With Quote
Old 10-04-2006, 12:38 PM   #4
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,744
Thanks: 130
Thanked 294 Times in 228 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Sorting lists


You have to write your bubbleSort routine probably.
shabbir is offline   Reply With Quote
Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

 

All times are GMT +5.5. The time now is 02:06 AM.