hi im doing a project using the bubblesort feature. I have to print out an inputted array of 26 characters sorted by ascii characters. i need to print them out ascending and descending, and also print out the top 3 occuring characters and their counts. i also need the largest and smallest character by ascii code.

#include <iostream>
using namespace std;
void input (char ulist[26], int& n);
void Bubblesort (char ulist[26], char slist[26], int n);
void print(char list[26], int n);
double average;int n,sum;
void main()
    char ulist[26], slist[26];
    input(ulist, n);
    cout << "Unsorted" ;
    print(ulist, n);
    cout << "Sorted";
    Bubblesort (ulist,slist,n);
void input(char ulist[26], int& n)            
    int i(0); 
    char value;
    cout << "enter character : \n";
    cin >> value ;

    while (i < 26 && value != '#')
        ulist[i] = value;
        if (i<26)
        cin >> value ;
void Bubblesort(char unlist[26], char sortlist[26], int n)
    int i,j,temp;
    for (i=1;i<=n;i++)
            sortlist[i] = unlist [i];
    for (j=1;j<=n-1;j++)
        for (i=1;i<=n-j;i++)
            if (sortlist[i] > sortlist[i+1])
                temp = sortlist[i];
                sortlist[i] = sortlist[i+1];
                sortlist[i+1] = temp;
void print(char list[26], int n)
    int i;
    sum = 0;
    cout << " list of characters by ASCII code are : \n";
    for (i=1; i<=n; ++i)
        cout << list[i] << '\n';
        sum = sum + list[i];


so far it prints out the unsorted and the sorted (ascending)

any help would be appreciated

Edited by Nick Evan: Fixed formatting

8 Years
Discussion Span
Last Post by NjNetsFan1

how to write a program which inputs the grade of five students name are users input in 3 subject (English,Math, Science). the program should calculate the average of every students and rank the students from 1st place to the 5th place.


>i need to print them out ascending and descending
To sort descending, it's a simple matter of reversing the comparison in your bubble sort function. You can either write a second function, or modify your current one to use a flag passed in as a parameter:

void Bubblesort (char ulist[26], char slist[26], int n, bool descending=false);

>print out the top 3 occuring characters and their counts.
Then you need a frequency array. Personally, I would combine this with your ulist and slist, but you can have it separate if you want either by having an array of structures (where each structure has a character and a count), or two parallel arrays:

#include <cctype>
#include <iostream>

int main()
  char chars[10] = {0};
  int n[10] = {0};
  char ch;

  while (std::cin.get(ch)) {
    if (std::isdigit(ch)) {
      int i = ch - '0';

      chars[i] = ch;

  for (int i = 0; i < 10; i++) {
    if (chars[i] != '\0')
      std::cout<< chars[i] <<": "<< n[i] <<'\n';

For the parallel arrays, take care to swap both in the same way when sorting.

>i also need the largest and smallest character by ascii code.
Easy greasy. Once the characters are sorted, slist[0] and slist[25] will be the smallest and largest, respectively.


thanks alot that really helped. if i wanted to instead input 5 words and print the characters out in the words ascending and descending how would i do that?? i read the directions completely wrong =(


It's not really any different. Just make sure you can handle characters with a zero count and have enough array space for all of the potential characters in the words.


ok so i could put something like 100 in for the array values? sorry i have 0 idea how to do this stuff on my own lol

This topic has been dead for over six months. 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.