WRITE PROGRAMME TO SORT AN ARRAY

HELLO , EVERY ONE
I HAD EXAM SINCE WEEK AGO
AND THE QUESTION WAS ,( WRITE A PROGRAMME TO SORT AN ARRAY )
CAN ANY ONE WRITE THIS PROBLEM CORRECTLY PLEASE !!
mostafa yahia wrote:
SORT AN ARRAY

Ascending,descending,...???
Also, let's see what you did, and try to help if possible.

Thanks,
Aceix.
the nth user (who asked this kinda question)
any array not condition
i did like that it is wrong
but i will show you
but please give me the correct answer
that what i did
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include <iostream>
using namespace std ; 
int main ()
{ int x,y 
cout <<"enter the number " ;
if (x>y) 
cout <<x<<"\n"<<y ;
else 
cout <<y<<"\n"<<x ;
return (0) ;
}

closed account (o3hC5Di1)
Hi there,

Your code doesn't do any sort of sorting at all (pun not intended), and you don't have an array.

Some info to get you started:

* You will need to create an array
* Sorting would at least involve comparing two elements of the array, as you do with x and y
* Sorting would also involve moving one of x or y to a new position in the array, this depends on the sorting algorithm you're using.

If you had an exam about sorting arrays, I expect you did at least see one or more sorting algorithms in class?

All the best,
NwN
i tried this , it is working :D

#include<iostream>
using namespace std;
void main()
{

int a[20];
int i,n,j,temp;
cout<<"Enter the value of n:"<<endl;
cin>>n;
cout<<"\nEnter the values:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Sorted array:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}

}
but, i don't think it's a fully effective program due to your objective... but, your on the right track, keep going...
thanks
if you have good idea for my programme , tell me
sorry, i didn't noticed that you're using 2 "for" loops, for iterating through the array length, and the 2nd is for iterating as long as the rest of the array. it's pretty much good program, not bad. unless, it contain some logic errors (i think). try to look at this link: http://en.wikipedia.org/wiki/Bubble_sort
Topic archived. No new replies allowed.