this is the sample program of selection sort, which is given the value. But, how to call the value from read file? anybody knows?

#include <iostream>
using namespace std;

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

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

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

void main()
{

	int y[]={9,6,5,23};   // array to sort 
    selectionSort(y,4);                 //call to selection sort  
	printElements(y,4);               // print elements 
}

Why are you still using void main?
http://www.research.att.com/~bs/bs_faq2.html#void-main
http://c-faq.com/ansi/maindecl.html
It's wrong pure and simple, and just because your compiler accepts it doesn't make it any more right.

Plus, you need to work on your indentation as well. If it looks better in your IDE (everything lined up nicely) than in your post above, then think about turning off hard tabs for indentation.

You open the file.
You use a loop to do say fin >> y[i]; You close the file

try this

int i;
ifstream infile//declaring a file pointer
infile.open("filename");
i=0;//integer i
int n;

while(infile>>n){
arr[i]=n;
}
//this will store all the integer  value from file  in the array named arr
  
infile.close();
//closing of file
//write this code in the main function and as salem said declare it as int  main()

and add return 0;
at last

I have done likethis , but cannot run the program, because there is some error.How to subscript require array or pointer? in line 43.

3.cpp
F:\300408\913.cpp(43) : error C2109: subscript requires array or pointer type
F:\300408\913.cpp(43) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

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

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

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

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

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

int main()
{
	int i;
	ifstream infile;
	infile.open("jes.txt");
	i=0;
	int y,n,arr;
	while(infile>>y)
	{
	arr[i]=y;
	}

	infile.close();
	return 0;

}

arr is not an array its and int. line 40.

So, What i suppose to do now?delete arr?

what i think you're supposed to do is, you need to read some stuff about datatypes in c++ and then revisit the code.

Ok. I have suucessful read the text file...but still have some error to do a selection sort and print elements,...can somebody help me? inline 55 and 56

F:\300408\913.cpp(55) : error C2664: 'selectionSort' : cannot convert parameter 1 from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
F:\300408\913.cpp(56) : error C2664: 'printElements' : cannot convert parameter 1 from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

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

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

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

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

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

int main()
{
	int i;
	ifstream infile;
	infile.open("jes.txt");
	if (!infile)
	{
        cout << "Unable to open file";
        exit(1); // terminate with error
    }
	
	i=0;
	int y;
	while(infile>>y)
	{
     cout<<" "<<y<<"\n";
    }

	infile.close();
	return 0;
	selectionSort(y,4);
	printElements(y,4);

}

y is a single integer, so it cannot be passed to the functions as such. You need an array of integers holding the data you have read in.

int main()
{
    enum { ARR_SIZE = 3 };
    int y[ARR_SIZE] = {0};  // Array of ints, at max ARR_SIZE elements
    int count = 0;  // number of elements read into the array

    ifstream infile("jes.txt");

    while(count < ARR_SIZE && infile >> y[count])
    {
        cout << " " << y[count] << "\n";
        ++ count;		
    }

    cout << "read in " << count << " numbers ...\n";

    // sort the numbers 
    selectionSort(y, count);

    return 0;
}

in your line 56 just write
see in your code ...
i told you to pass the value in an integer array as follows;

int array[100];//array of size 100
i=0;
while(infile>>y){
array[i]=y;
i++;
}

then your selection sort function should be like
selectionSort(array,4);
printElements(array,4);
try this if it does not work i will write the code for you

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.