0

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[10] = [ E, F, Y, R, Q, A, T, O, W, X ]

Array after sorting:

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

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by John A
0

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;


}
}
}
}

Edited by happygeek: fixed formatting

0

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] )
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.