the using statement isn't necessary if you use the std:: prefix - force of habbit..
Main Topics
Browse All Topicshi!
iam trying to sort numbers form an array in ascending order.. and then writing them to a file or on to a new array.
for e.g
if i have an array of [ 5,6,2,7,4,3,54,133]
i want to sort them in ascending order ...
any help will be appreciated
thankyou
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
in your above program..
are u assumimg that the array contains only these values {4,3,5,4,1}
i mean if the array has some other data in it.. then will it be able to sort in ascending...?
if u can could u please make use of a lets say.. a simple for loop or a while loop or something/..
cause i have never used namespace std and algorithm header. before..
also
<<<<< std::sort(v, &v[4] + 1);
i dont quite understand what the above line does.. if u could kindly explain it to me.. i'd be greatful
thank you
The standard sort algorithm can sort anything that can be compared by the opeartor < in c++. It can also compare other stuff, for example user defined types, if you supply a comparing function for them.
To your question, no, I won't use a loop when the algorithm is already there implemented for me. Besides, I don't think the usage could be any simpler. So please take a look at the documentation about the std::sort algorithm and see what it does.
Usually it takes two paramteres: an iterator (or pointer) to the first element in the array you wish to sort and an iterator to the last one (actually, the one after the last one). It sorts from 'begin' to 'end'.
So:
std::sort(v, &v[4] + 1)
tells the function to sort the array starting from the first element (v) upto the end of the array (which is &v[4]+1 in this example with a 5 element array).
See also this example of sort:
http://www.josuttis.com/li
Again, I recommend that you use vectors instead of arrays: http://www.parashift.com/c
With a vector it would have been simpler:
std::sort(yourVector.begin
Isn't that nicer?
Please take the time to learn about the standard containers and algorithms - it's good for you. ;)
// Author: Reborn Assassin
// Name: Sort Program
#include "iostream.h"
void main()
{
char ans = 'y';
while (ans =='y')
{
int n[10], tp, x, y, total;
total = sizeof(n)/4; // Meaning you can change the size of the array
for (x=1; x<=total; x++)
{
cout << "\nEnter number " << x << ": ";
cin >> n[x];
}
for (x=1; x<=total-1; x++)
{
for (y=x+1; y<=total; y++)
{
if (n[x] > n[y])
{
tp = n[x];
n[x] = n[y];
n[y] = tp;
}
}
}
for (x=1; x<=total; x++)
cout << endl << n[x];
cin >> ans;
}
}
here is a sample code using list.
i would prefer using list instead of vector as it has sort member func.
int main( )
{
using namespace std;
list <int> c1;
list <int>::iterator c1_Iter;
c1.push_back( 20 );
c1.push_back( 10 );
c1.push_back( 30 );
cout << "Before sorting: c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( );
cout << "After sorting c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
c1.sort( greater<int>( ) );
cout << "After sorting with 'greater than' operation, c1 =";
for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
cout << " " << *c1_Iter;
cout << endl;
}
Output
Before sorting: c1 = 20 10 30
After sorting c1 = 10 20 30
After sorting with 'greater than' operation, c1 = 30 20 10
thanks for all ur efforts..
i tried to impliment reborn_assassin's program in to my program which is below
--------------------------
// AUTHOR--SAM
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
//include <stdlib.h>
fstream datafile;
// declaring the file object so that it can communicate with the operating system
int max=0,min=9999,i;
float ave,sum2;
long sum=0;
int* contents; // declares variable to read data from file
char filename[16],words[80];
double input;
int keypress,anykey, count=0;
void exit_prog()
{
cout<<"program terminated"<<endl;
}
//------------------- Welcom Screen Function------------------
int welcome()
{
gotoxy(15,5);
cout<<"*******************
gotoxy(15,6);
cout<<"*\t\t ------- COLLEGE\t *";
gotoxy(15,7);
cout<<"*\t FILE PROCESSING SYSTEM *";
gotoxy(15,8);
cout<<"*\t\t PRESS C TO CONTINUE\t *";
gotoxy(15,9);
cout<<"* *";
gotoxy(15,10);
cout<<"*******************
gotoxy(34,9);
keypress=getche();
stop:
return 0;
}
//----------------------Me
int menu()
{ clrscr();
gotoxy(15,5);
cout<<"*******************
cout<<"\t *\t\tFile Processing System\t *"<<endl;
cout<<"\t * \t Please input option *"<<endl;
cout<<"\t * \t *"<<endl;
cout<<"\t * 1----------Create new data file *"<<endl;
cout<<"\t * 2----------Input data to a file *"<<endl;
cout<<"\t * 3----------View numbers form a file *"<<endl;
cout<<"\t * 4----------Display maximum\t *"<<endl;
cout<<"\t * 5----------Display minimum\t *"<<endl;
cout<<"\t * 6----------Display total\t *"<<endl;
cout<<"\t * 7----------Display the Average value *"<<endl;
cout<<"\t * 8----------Ascending sort \t *"<<endl;
cout<<"\t * 9----------EXIT \t\t *"<<endl;
cout<<"\t * \t *";
gotoxy(15,18);
cout<<"*******************
gotoxy(34,8);
keypress=getche();
return 0;
}
//-------------OPTION 1 CREATE FILE----------------------
int create_file()
{
// asking user to input name of the file to create
cout<<"please enter the name of the file that you wish to create"<<endl;
cout<<"use .txt as your file extension\n";
cin>>filename;
cout<<"\nA file named "<<filename<<" was created"<<endl;
datafile.open(filename,ios
datafile.close(); // close file
return 0;
}
//-------------OPTION 2 INPUT DATA TO A FILE----------------------
int input_data()
{
// asking user to input name of the file to input data to
cout<<"please enter the name of the file that you wish to input data to"<<endl;
cout<<"please use the correct extension\n";
cin>>filename;
do
{
count++;
datafile.open(filename,ios
clrscr();
cout<<"input number";
cin>>input;
datafile<<input<<endl;
datafile.close();
cout<<("do you want to input another number... press anykey to continue or N to exit\n");
keypress=getch();
}while(keypress!=110&&keyp
clrscr ();
cout<<"You entered "<<count<<" numbers\n"<<endl;
return 0;
}
//-------------OPTION 3 VIEW DATA FORM A FILE----------------------
int view_file()
{
// asking user to input name of the file from which the data needs to reviewed
cout<<"please enter the name of the file which you wish to view the contents"<<endl;
cout<<"please use the correct extension\n";
cin>>filename;
datafile.open(filename,ios
while(!datafile.eof()) // keeps reading till the end of file
{
datafile.getline(words,80,
cout<<words<<endl; // displays characters on the screen
};
datafile.close(); // close file
return 0;
};
//-------------OPTION 4 Maximum-------------------
int maximum()
{ contents= new int[count];
datafile.open(filename,ios
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// find and display max
for(i=0;i<count;i++)
{
if(contents[i]>max)
max=contents[i];
}
cout<<"max is "<<max<<"\n";
datafile.close();
return 0;
}
//-------------OPTION 5 MINIMUM-------------------
int minimum()
{ contents= new int[count];
datafile.open(filename,ios
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
//find and display min
for(i=0;i<count;i++)
{
if(contents[i]<min)
min=contents[i];
}
cout<<"min is "<<min<<endl;
datafile.close(); // close file
return 0;
}
//-------------OPTION 6 TOTAL---------------------
int total()
{
contents= new int[count];
datafile.open(filename,ios
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// calcualte and display sum
for(i=0;i<count;i++)
sum=sum+contents[i];
cout<<"sum is "<<sum<<"\n";
datafile.close(); // close file
return 0;
}
//-------------OPTION 7 AVERAGE-------------------
int average()
{ contents= new int[count];
datafile.open(filename,ios
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// calclate and display average
sum=sum2;
ave=sum2/count;
cout<<"average is "<<ave<<endl;
datafile.close(); // close file
return 0;
}
//-------------OPTION 8 SORT----------------------
int sort()
{
char ans;
contents= new int[count];
int tp, i, j, total;
total = sizeof(contents)/4; // Meaning you can change the size of the array
for (i=1; i<=total-1; i++)
{
for (j=i+1; j<=total; j++)
{
if (contents[i] > contents[j])
{
tp = contents[i];
contents[i] = contents[j];
contents[j] = tp;
}
}
}
for (i=1; i<=total; i++)
cout << endl << contents[i];
cin >> ans;
return 0;
}
//-------------MAIN PROGRAM-------------------
int main()
{
welcome();
while(keypress!=67||keypre
{
if (keypress==99||keypress==6
{
menu();
while(keypress!=49||keypre
keypress!=54||keypress!=55
{
if(keypress==49||keypress=
keypress==54||keypress==55
{
if(keypress==49)
{ //----- OPTION 1------//
clrscr();
create_file();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==50)
{ //----- OPTION 2------//
clrscr();
input_data();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==51)
{ //----- OPTION 3------//
clrscr();
view_file();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==52)
{ //----- OPTION 4------//
clrscr();
maximum();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==53)
{ //----- OPTION 5------//
clrscr();
minimum();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==54)
{ //----- OPTION 6------//
clrscr();
total();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==55)
{ //----- OPTION 7------//
clrscr();
average();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==56)
{ //----- OPTION 8------//
clrscr();
sort();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==57)
{ //----- OPTION 9------//
clrscr();
cout<<"\a"<<endl;
cout<<"Thank you for using our program"<<endl;
cout<<"We hope you enjoyed it";
cout<<"\n\n\t\t\tPress Any Key exit the program";
anykey=getch();
break;
//exit(0);
}
}
cout<<"\a";
clrscr();
menu();
}// end while of menu validation
}
cout<<"\a";
clrscr();
welcome();
}//end while of welcome validation
return 0;
}
--------------------------
there are 3 problem with my program...
1) the sort code provided by reborn_Assasin---- i ttried but i could'nt integrate his proram into mine.. i tried to look for the problem but iam having difficulty.
2) the exit function dosent work.. in the opt9.. i tried to puta break; but it only goes back to the welcome screenn. .
also i tried to put exit(0); there fore included stdlib.h as header.. but for no apparent reason it gives me 9 errors.. . please could soemone provide me with a solution for this.
3) the average option---. initially my ave was an int .which gave me an int value. .but when i changed it to float .all it gives me is 0..
please help
thank you..
Business Accounts
Answer for Membership
by: AssafLaviePosted on 2003-06-12 at 10:34:17ID: 8710924
use the standard algorithm std::sort, although I really recommend that you use vectors instead of arrays, this is c++ not c.
example:
#include <algorithm>
using namespace std;
int main()
{
int v[5] = { 4, 3, 6, 4, 1 };
std::sort(v, &v[4] + 1);
return 0;
}