Hi ..

anyone can help me out to solve this problem...
coding to read the file...

1. this coding is well working... can list down all the data in dataM.txt

ifstream OpenFile("D:\\dataM.txt");    
 int a;
 if(!OpenFile)
    {
  cout << "While opening a file an error is encountered" << endl;
     }
  else
    {
  cout << "File is successfully opened" << endl;
     }
 while(!OpenFile.eof())
     {
       OpenFile >> a;               
       cout << a << endl;         
       }
 }

Than i want all the dataM.txt can be readable in standard algorithm sorting..

void selectionSort(int numbers[], int array_size)
    {
      int i, j;
      int min, temp;

      for (i = 0; i < array_size-1; i++)
      {
        min = i;
        for (j = i+1; j < array_size; j++)
        {
          if (numbers[j] < numbers[min])
            min = j;
        }
        temp = numbers[i];
        numbers[i] = numbers[min];
        numbers[min] = temp;
      }
    }

my problem is.. my data is stuck and not readable in sorting algorithm...

i have my coding to display all the sorting data after that....
anyone can help me out...tq

In order to sort the data read in from the file, you'll need to store it into an array, which you then pass to your sort function.

Just a little fixing to your code:

ifstream OpenFile("D:\\dataM.txt");    
	int a[1000];  //make this big enough to hold the data
	int i = 0;
	if(!OpenFile)
	{
		cout << "While opening a file an error is encountered" << endl;
	}
	else
	{
		cout << "File is successfully opened" << endl;

		while( OpenFile >> a[i] )
		{
			i++;
			cout << a << endl;  //you don't need this, 
                                      //but it will verify your reading       
		}
	}

	selectionSort( a, i );

	//display sorted data now

Avoid using while( !OpenFile.eof( ) ) for loop control - it has it's weaknesses, and you didn't set it up quite right. It needs to read outside the loop before the first test, and then again at the end of the loop for the next iteration.

Hi vmanes

thanks for yr kind help... i got it now.. im just take this course and a beginner like me will take time to be like you (salute).
i've compile my coding and i got abnormall output.. seems like the memory located for the data are appear...

0x00012ed60
0x00012ed60
0x00012ed60
0x00012ed60

ok

i able to run this...but something wrong somewhere...
coz the output of sorting is weird! do i excidently altered the algorithm,,,,??

#include <iostream.h>
#include <conio.h>
#include <fstream.h>

class sorting
{
	private:
   	int a[20];

   public:
   	void r(int []);
      void sort(int[]);

};

void sorting::read(int a[])
{
int i;
int x=0;
int y=20;
  ifstream inputFile;
  inputFile.open("d:\\dataM.txt");
  if (!inputFile)
  {
		cout << "While opening a file an error is encountered" << endl;
      return;
  }
  while(x<y && inputFile>>a[x])
  x++;
  for(int i=0;i<x;i++)
  cout<<a[i]<<endl;
  inputFile.close();
 }

void sorting::sort(int a[])
{
  int max,n=20;
   for(int i=0;i<n-1;i++)
   {
   	for(int j=i+1;j<n;j++)
      	if(a[i]>a[j])
         	{
            max=a[i];
            a[i]=a[j];
            a[j]=max;
            }
    }

    cout<<"\nSorted list\n";
    for(int i=0;i<n;i++)
      	cout<<a[i]<<endl;
}

int main()
{
sorting sd;
const int size=50;
int b[size];
int c[size];

sd.read(b);
sd.sort(c);
getch();
return 0;
}

OUTPUT

99
6
35
23
44
55
2
33
5
7
8
9
5
6
77

Sorted list
0
0
0
0
0
0
0
4
1244660
1703936
1703936
1703936
1703936
1727776
1727776
1727776
1727784
1073807458
1991016636
1991017220

You're juggling apples, oranges and pears. You have array a[] as a member of the class - but you do nothing with it. You tell class instance d to read into main's array b, then ask class instance d to sort main's array c. And all the while, you have no idea how many data items are actually in any of the arrays.

First tell us what exactly you're trying to accomplish, then we can better help you sort it out.

im shame on this :)

ok.. this is what i want to do

1. read all the the dataM.txt from void sorting::read(int a[])
(dataM.txt is unsorted data)
2. past the data to void sorting::sort(int a[]) to sort it out using the standard algorithmm in this case insert sorting ...
3. and display the result..

i did it since 3 days ago.. OMG! help me...

thanks a lot

If your intent is that the data read in is to be part of the class object's data, then your read function does not need the array paramter. It will simply read into the array a[] that is a member of the class.

The counter x should also be a class member, which will store the number of items read in. And give it a better name than x - how about num_items?

Now, with the data stored in the class's array a[], and the count of data in the class's member num_items, those values are directly available to the sort function. So no parameters needed, it also just acts upon array a[] and uses num_items to limit the portion of the array that gets sorted. What you use n for in the sort should be the num_items - don't sort the whole array, only the part that has data.

Your main( ) will become much simpler

int main()
{
    sorting sd;

    sd.read( );
    sd.sort( );

    getch();
    return 0;
}

thanks vmanes

finally i managed to solve this coding..
its look more simple than im expected...

but i have another 3 algorithm to do..
i think i got the idea ... :)

Hello,

I am also a beginer programmer. I think the problem with using

while(OpenFile.eof())

is that it reads the last data entry in the file twice.

What I think is happening is that your sorting::read function is reading the values into the array but then you use a blank array for the sorting sort function instead of the one from the sorting::read function. What i think you can do is make the sorting::sort function function use the sorting::read function's array b. I dont think you need the array c cause arrays are sort of passed by reference so b will get updated. what I would do in main would be

sd.read(b);
sd.sort(b);

Please let me know if this sort of helped.

hi all,

when i run this code, i ll get ascending order as output... now i want descending order as output for my data... anybody know how to altert this code..uhuhuh

void QuickSorting::sort(int a[], int lo, int up)
{
  {
   int i, j, pivot;
     while ( up>lo )
     {
	  i = lo;
	  j = up;
	  pivot = a[lo];

	  while ( i<j )
	  {
		  for ( ; a[j] > pivot; j-- );
		  for ( a[i]=a[j]; i<j && a[i]<=pivot; i++ );
				  a[j] = a[i];
	  }
	  a[i] = pivot;

	  if ( i-lo < up-i )
		  { sort(a,lo,i-1);  lo = i+1; }
	  else
		  { sort(a,i+1,up);  up = i-1; }
     }
  }
}

anyone...?

Have a look here to see how you need to modify your sort for descending order

yeah, i found that link before... thanks...

anyway.. im not very sure about some 'term'of algorithm

yeah, i found that link before... thanks...

anyway.. im not very sure about some 'term'of algorithm

Please point out what's confusing you.

For making your quicksort work in descending order, it's mostly a matter of reversing the comparisons.

void quicksort(apvector <int> &array, int top, int bottom)
{
      // top = subscript of beginning of vector being considered
      // bottom = subscript of end of vector being considered
      // this process uses recursion - the process of calling itself
     int middle;
     if (top < bottom)
    {
          middle = partition(array, top, bottom);
          quicksort(array, top, middle);   // sort top partition
          quicksort(array, middle+1, bottom);    // sort bottom partition
     }
     return;
}


//Function to determine the partitions
// partitions the array and returns the middle index (subscript)
int partition(apvector <int> &array, int top, int bottom) <--  how to call this  function? in term of declaration in public:: .. :$  
{
     int x = array[top];
     int i = top - 1;
     int j = bottom + 1;
     int temp;
     do
     {
           do     
           {
                  j - -;
           }while (x >array[j]);

          do  
         {
                 i++;
          } while (x <array[i]);

          if (i < j)
         { 
                 temp = array[i];    // switch elements at positions i and j
                 array[i] = array[j];
                 array[j] = temp;
         }
     }while (i < j);    
     return j;           // returns middle index
}

You make that partition function also a method of your class. Thus when you tell your object to sort itself, the sort function has access to the partition function.

Or just use the sort that you had, changing the direction it operates. You could, if you want, have both an ascending_sort( ) and descending_sort( ) methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.