Sort Numbers in Ascending order using Array | C++

This is an assignment My teacher gave me and to be true I am a dumbo.
Last time she thought me how to Find Largest number in a array of five. Now she has given me the task to sort numbers in Ascending order using array and she has give me guidelines that I have to code it in the same kinda format of finding a largest no. in a array of five.

This is the code of finding largest no. in array of five she gave me :-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream.h> 
#include<conio.h>
void main()

{    
clrscr();
int a[5],max,i;
cout<<"enter the no. "<<endl;
for (i=0,i<5,i++)
{
 cin>>a[i];
}
max=a[0];
for (i=0;i<5;i++)
{
if (a[i]>max)
max=a[i]
}
cout<<"the larger no. is " <<max;
getch();
}

Please Help me by providing a similar looking code for sorting ascending numbers using array!

It would help me practice

Thanx
Help me by providing a similar looking code

No can do, for it will do the opposite of helping you.

You can already find the max element. Too bad it is written directly in the main() and not as a function. Second, you store the maximum value, rather than the position of the maximum value. For sorting, the position is useful.

There is a (crude) way to sort with the code that you almost have already:
1. Find the position of the largest element in the array.
2. Swap the values of the largest element and the last element.
3. Repeat the steps 1 and 2 for an array, except the last element.
4. End repeats when the array to consider covers only one element, (the first element of the initial array).
Please Provide me the code :'(
I tried lots but I am not able to do!
It will surely help me and Brother She does not give me attention while teaching that is why I am asking you guys! Please Help me
You don't want any code. You want to learn by doing the assignment yourself. You want to think and try.


However, I will now deliberately hurt you by giving a link that leads to a code that does sort, but not the way that your teacher will accept: http://www.cplusplus.com/reference/algorithm/
The choice is yours.
@NafeesOnly, please use code indentation.
http://en.wikipedia.org/wiki/Indent_style
preferably the Stroustrup variant.
and remove unneeded parts such as line 6 and 20.
it will help focus at right place.


have you learned variable swapping?
such as, int a=5, b=10;
now swap a's value with b ?
closed account (yR9wb7Xj)
Hey NafeesOnly, since you are in college you know what's a great way to learn c++ form a study group with your class it does not have to be big it can 3 or 4 people. Make sure the people are interested and willing to sacrifice their day time to become computer programmers. It's a good way to learn c++ programming and a good way to work as team and you develop good communication skills, and multiple people can have different solution to your problem and you will learn alot by knowing that there is more than 1 solution to solving it. Which is a good thing, I wish you the best of luck, the semester is probably over but it's never too late to learn c++ on your own with a group of friends that are interested.

I agree with keskiverto you really do not want the code not right away at least, you will not learn like that, that is the best advice someone can give you. Actually think about it, and if you don't want to and just want to go the easy way maybe this is not for you. I learn the hard way that good programmers, do not ask question when they are stuck, instead they try to figure it out for themselves. When they cannot find the solution they are looking for then they ask for help, but first give it a try. Here's a tip that I learned here on this site don't jump right into solving the problem. Think about it and actually plan it out first on paper. Once you know how to solve the solution then you can code. Coding should always be the last thing to do when solving any problems. Good luck! :)
Last edited on
Topic archived. No new replies allowed.