Hi! I need help on writing a program that puts 10 "char" values into an array, the program have
to sort the values of the array into ascending order.

Sample output:

Array before sorting:

Array = [ E, F, Y, R, Q, A, T, O, W, X ]

Array after sorting:

Array = [ A, E, F, O, Q, R, T, W, X, Y ]

## All 3 Replies

search for "sort algorithms" and you will get a lot of help. There are a lot of algorithms, which one were you instructed to use ? The Bubble Sort is probably the easiest to code.

Hi! I managed to sort them in alphabetical order but it is in a descending not ascending order. Please HELP!

``````#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void sort(char a[], int alphabet);

int main(){

const int alpha = 10;
char x[alpha] = {'E', 'F', 'Y', 'R', 'Q', 'A', 'T', 'O', 'W', 'X'};

sort(x, alpha);

cout << endl;
cout << "Letter \n" << endl;
for (int i = 0; i < alpha; i++) {
if (x > 0) {
cout << x<< " "; }}
cout << endl;

system("pause");
return 0;
}
void sort(char a[], int alphabet)
{

char temp, i, j;
for (i = 0; i < (alphabet-1); i++){
for(j = (i+1); j < alphabet; j++) {
if ( a < a[j] )
{
temp = a;
a = a[j];
a[j] = temp;

}
}
}
}
``````

Please use CODE tags when posting code. Their usage is explained in the watermark that you typed over when you wrote your post, and in the announcements in this forum.

As for your problem, it's very simple. Change this line from:

``if ( a[i] < a[j] )``

To:

``if ( a[i] > a[j] )``
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.