Why the selection sort output not appear? Anybody knows how to correct it?

#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; 
	  
		}

}
void printElements ( int *y , int x, int n ) //print array elements
{
	int i = 0 ;
	for ( i = 0 ; i < 3 ; i++ )
    cout << " " << x << " " << y << endl;
}
	
void 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";
			}
			cout <<" "<< x <<"  "<<y<< endl;
		}
	 } 	
}

look at line 8. It wants an int array and the number of items in the array. In main() you have declared neither.

in main() you need to declare an int array then fill it with some numbers. After that you can call the selection sort function to re-arrange the numbers in either ascending or descending sequence.

but, i want to call the data from the text file...because, i have 100-300 data, So it it another way, to do?

delare the array then fill it from the data that is read from the text file. I'm certain that you know how to do that by now.

int array[255] = {0};
// now read the values from the text file

delare the array then fill it from the data that is read from the text file.

fill it from the data that is read mean if I have 3 data so must write..and if i have 100 data, so i must write also..is it like that?

Edited 3 Years Ago by happygeek: fixed formatting

Here's an example.

const int max = 255;
int array[max] = {0};
ifstream in("filename.txt");
int i = 0;
while( i < max && in >> array[i] )
   i++;

i have change like this, but the output only display for i=0 only.

and it said in is using without initialized.
in is for what actually? cin?

output
1 20
1 20
Press any key to continue

void main ()
{
    const int max = 4;
	int x,y, in;
    int array[4] = {0};
	ifstream inFile("jes.txt");
    int i=0;
	while( i < 4 && in >> array[i] )
	 i++;
	
	{
		 if (inFile >> x >> y) 
		 {  // Reading is successful
			if (inFile.fail())
			{
			cout << "Error! Cannot open file! \n";
			}
			cout <<" "<< x <<"  "<<y<< endl;
		 }
	}	 

    cout << " "<<x<<" "<<y<< endl;
}
This article has been dead for over six months. Start a new discussion instead.