After read this file , i want to do a selection sort...below are the selection sort programming that i get from google

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

void sortData( string fId[], int noOfRows);

int main ()
{
	string Id[100];
	int total[100],  i = 0, num = 0;

	ifstream inFile; 
	ofstream outFile; 

	inFile.open("jes.txt");

	if (!inFile) 
	{
		cout << "Cannot open the input file." << endl;
		return 0;
	}

	outFile.open("jes2.txt");

	while (!inFile.eof())
	{
		inFile >> Id[i]  >> total[i] ;
		i++;
	}

	for (i = 0; i < 3; i++)
	{
		cout<<" "<<Id[i]<<"  "<<total[i] <<endl;
	}
	
	return 0;
}

selection sort

void selectionSort(int *array,int length)//selection sort function 
{
	int i,j,min,minat;
	for(i=0;i<(length-1);i++)
	{
		minat=i;
		min=array[i];

      for(j=i+1;j<(length);j++) //select the min of the rest of array
	  {
		  if(min>array[j])   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element 
			  min=array[j];
		  }
	  }
	  int temp=array[i] ;
	  array[i]=array[minat];  //swap 
	  array[minat]=temp;

		
	}

}



void printElements(int *array,int length) //print array elements
{
	int i=0;
	for(i=0;i<10;i++)
    cout<<array[i]<<endl;
}


void main()
{

	int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort 
    selectionSort(a,10);                 //call to selection sort  
	printElements(a,10);               // print elements 
}

but my problem is...how to call the input file that i was read into selection sort?
the example given is the number that has created.

#include <fstream>
#include <iostream>
using namespace std;

void selectionSort(int *array, int length);
int main() {
	ifstream inFile;
	int array[100];
	inFile.open("jes.txt");
	if (inFile.is_open()) {
		int tmp;
		size_t cnt = 0;
		while (inFile >> tmp) {
			array[cnt] = tmp;
			++cnt;
			if (cnt >= 100) {
				cout << "Error more than 100 elemets.";
				return 2;
			}
		}
		selectionSort(array, cnt);
		for (size_t i = 0; i < cnt; ++i) {
			cout << array[i] << ' ';
		}
	} else {
		cout << "Unable to open file jes.txt";
		return 1;
	}
	return 0;
}

void selectionSort(int* array, int length) {
	for (int i=0; i<length-1; ++i) {
		int min = i;
		for (int j=i+1; j<length; ++j) {
			if (array[min]>array[j]) {
				min = j;
			}
		}
		if (i < min) {
			array[i]^=array[min]^=array[i]^=array[min];
		}
	}
}

I have interesting solution other than Selection Sort, just for knowledge.
You fetched the numbers from the stream and put it in the array, then sort the array using the desired algorithm.
What if you make a sorted link list and insert in it directly?. just compare the complexities of two algorithm, will give you the in depth knowledge regarding selection sort. [little mathematics required].

check whether you are doing the same thing?

you can use std vector and std sort and it's fast over this solution ;)

how to make correction of this error in line 41 and 63


.5.08.cpp
f:\300408\3.5.08.cpp(41) : error C2601: 'selectionSort' : local function definitions are illegal
f:\300408\3.5.08.cpp(63) : error C2601: 'printElements' : local function definitions are illegal
Error executing cl.exe.

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

int main ()
{
	string a[100];
	int total[100],  i = 0, num = 0;

	ifstream inFile; 
	ofstream outFile; 

	inFile.open("jes.txt");

	if (!inFile) 
	{
		cout << "Cannot open the input file." << endl;
		return 0;
	}

	outFile.open("jes2.txt");

	while (!inFile.eof())
	{
		inFile >> a[i]  >> total[i] ;
		i++;
	}

	for (i = 0; i < 3; i++)
	{
		cout<<" "<<a[i]<<"  "<<total[i] <<endl;
	}
	
	return 0;	
	
	void selectionSort(int *total)//selection sort function 
	{
	int i,j,min,minat;
	for(i=0;i<3;i++)
		{
		minat=i;
		min=total[i];

		for(j=0;j<(3);j++) //select the min of the rest of array
		{
		if(total[j]>min)   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element 
			  min=total[j];
		  }
		}
	  int temp=total[i] ;
	  total[i]=total[minat];  //swap 
	  total[minat]=temp;
		}
	}

	void printElements(int *total,int n) //print array elements
	{
	int i=0;
	for(i=0;i<3;i++)
    cout<<" "<<a[i]<<" "<<total[i]<<endl;
	}

}

how to make correction of this error in line 41 and 63


.5.08.cpp
f:\300408\3.5.08.cpp(41) : error C2601: 'selectionSort' : local function definitions are illegal
f:\300408\3.5.08.cpp(63) : error C2601: 'printElements' : local function definitions are illegal
Error executing cl.exe.

#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

int main ()
{
	string a[100];
	int total[100],  i = 0, num = 0;

	ifstream inFile; 
	ofstream outFile; 

	inFile.open("jes.txt");

	if (!inFile) 
	{
		cout << "Cannot open the input file." << endl;
		return 0;
	}

	outFile.open("jes2.txt");

	while (!inFile.eof())
	{
		inFile >> a[i]  >> total[i] ;
		i++;
	}

	for (i = 0; i < 3; i++)
	{
		cout<<" "<<a[i]<<"  "<<total[i] <<endl;
	}
	
	return 0;	
	
	void selectionSort(int *total)//selection sort function 
	{
	int i,j,min,minat;
	for(i=0;i<3;i++)
		{
		minat=i;
		min=total[i];

		for(j=0;j<(3);j++) //select the min of the rest of array
		{
		if(total[j]>min)   //ascending order for descending reverse
		  {
			  minat=j;  //the position of the min element 
			  min=total[j];
		  }
		}
	  int temp=total[i] ;
	  total[i]=total[minat];  //swap 
	  total[minat]=temp;
		}
	}

	void printElements(int *total,int n) //print array elements
	{
	int i=0;
	for(i=0;i<3;i++)
    cout<<" "<<a[i]<<" "<<total[i]<<endl;
	}

}

You have no closing bracket for the main () function after line 39 so your selectionSort function is inside your main () function. That's what the error is. Get rid of the bracket on line 70. You already have a bracket that ends the printElements function on line 68.

i have done a correction, but the programming said..x and y are used without initialized..inline 50

How to initialized x and y?

F:\300408\905.cpp(51) : warning C4700: local variable 'y' used without having been initialized
F:\300408\905.cpp(51) : warning C4700: local variable 'x' used without having been initialized

905.obj - 0 error(s), 2 warning(s)

#include <iostream>
#include <fstream>
using namespace std;

int main()
{

int x, y;
ifstream inFile("jes.txt");

for (int i = 0; i < 3; i++) 
{
 if (inFile >> x >> y) 
 {  // Reading is successful
if ( inFile.fail() )
{
 cout << "Error! Cannot open file! \n";
  	return -1;
}

     cout <<" "<< x << "  " << y << endl;
 } 
}
	return 0;
}

void selectionSort(int *y)//selection sort function 
{
int i, min, minat;
for( i = 0 ; i < 3 ; i++)
{
minat = i;
min = y [ i ] ;
if ( y [ i ] > min )   //ascending order for descending reverse
{
minat = i;  //the position of the min element 
	min = y [ i];
}
}
  int temp = y [ i ] ;
  y [ i ] = y [ minat ];  //swap 
  y [ minat ] = temp;
}


void printElements(int *total,int n) //print array elements
{
int i = 0 , x , y;
for( i = 0 ; i < 3 ; i++ )
    cout << " " << x << " " << y << endl;
}

i have done a correction, but the programming said..x and y are used without initialized..inline 50

How to initialized x and y?

F:\300408\905.cpp(51) : warning C4700: local variable 'y' used without having been initialized
F:\300408\905.cpp(51) : warning C4700: local variable 'x' used without having been initialized

905.obj - 0 error(s), 2 warning(s)

#include <iostream>
#include <fstream>
using namespace std;

int main()
{

int x, y;
ifstream inFile("jes.txt");

for (int i = 0; i < 3; i++) 
{
 if (inFile >> x >> y) 
 {  // Reading is successful
if ( inFile.fail() )
{
 cout << "Error! Cannot open file! \n";
  	return -1;
}

     cout <<" "<< x << "  " << y << endl;
 } 
}
	return 0;
}

void selectionSort(int *y)//selection sort function 
{
int i, min, minat;
for( i = 0 ; i < 3 ; i++)
{
minat = i;
min = y [ i ] ;
if ( y [ i ] > min )   //ascending order for descending reverse
{
minat = i;  //the position of the min element 
	min = y [ i];
}
}
  int temp = y [ i ] ;
  y [ i ] = y [ minat ];  //swap 
  y [ minat ] = temp;
}


void printElements(int *total,int n) //print array elements
{
int i = 0 , x , y;
for( i = 0 ; i < 3 ; i++ )
    cout << " " << x << " " << y << endl;
}

I have no idea what x and y are supposed to represent, so I don't know what to initialize them to. I do notice that you pass the printElements function total, but don't use it.

>>how to call the input file that i was read into selection sort?

Pass both Id and total to selectionSort() function, instead of just a one array. To sort by the values in Id use Id in place of array in selectionSort(). Then, every time you swap elements of Id by doing this:

int temp=array ;
array=array[minat]; //swap
array[minat]=temp;

also swap the elements with the same index in total. So, using Id instead of array you end up with this:

int temp=Id ;
Id=Id[minat]; //swap
Id[minat]=temp;

temp=total ;
total=total[minat]; //swap
total[minat]=temp;

Now, after the arrays have been sorted in a parallel fashion pass both sorted arrays to the print function and print them out in parallel fashion as well.

i change the a to x and total to y ..so it easy to read..

the x and y are the data from a read file

jes.txt
1 20
2 30
3 25


or must i add one code like i post firstly in this thread.?

void main()
{ 	
int a[]={20,30,25};   // array to sort     
selectionSort(a,10);     //call to selection sort  	
printElements(a,10); // print elements 
}

why the output only produce
1 20
2 35
3 15

but not produce for the selection sort output?

#include <iostream>
#include <fstream>
using namespace std;

int main()
 {
	int x, y;
	ifstream inFile("jes.txt");

	for (int i = 0; i < 3; i++) 
	{
		 if (inFile >> x >> y) 
		 {  // Reading is successful
			if (inFile.fail())
			{
			cout << "Error! Cannot open file! \n";
			return -1;
			}
		cout <<" "<< x <<"  "<<y<< endl;
	
	}
	
	}	
		return 0;
} 
	void selectionSort(int *y,int n)//selection sort function 
{
int i, min,minat,x ;
{
for(i=0;i<3;i++)
{
minat=i;
min=y[i];

  if(min<y[i])   //ascending order for descending reverse
  {
  minat=i;  //the position of the min element 
  min=y[i];
  }
}
  int temp=y[i] ;
  y[i]=y[minat];  //swap 
  y[minat]=temp; 
  
}cout<<"After Descending Order";
cout<<" "<< x <<" "<<y<<endl;

}

You haven't called selectionSort from within main(), the x and y in main() are single variables, not a group of variables, and hence cannot be sorted, the x in selection sort has nothing to do with the x in main() and the y in selectionSort probably has nothing to do with the y in main().

so I must write like this?add the selectionsort in line 48?

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

void SelectionSort(int *y,int n)//selection sort function 
{
int i, min,minat ;
{
for(i=0;i<3;i++)
{
minat=i;
min=y[i];
if(min<y[i])   
{
minat=i;  
min=y[i];
}
}
  int temp=y[i] ;
  y[i]=y[minat];  //swap 
  y[minat]=temp; 
	  
}
}

int main()
{
int x, y;
ifstream inFile("jes.txt");
for (int i = 0; i < 3; i++) 
{
 if (inFile >> x >> y) 
{  // Reading is successful
if (inFile.fail())
{
cout << "Error! Cannot open file! \n";
return -1;
}
cout <<" "<< x <<"  "<<y<< endl;
}
  // Print out sorted list.
       
 } 	

selectionsort()
cout<<"After Descending Order"<<"\n";
			
cout << x <<" "<< y <<" " ; 
cout << endl; 
	
return 0;
}
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.