Hi, I created an array of numbers which converts into letters through a number generator. I need help sorting my array letters[j] so that the output can be printed in alphabetical order. After, I am storing it in a Linked List and printing it out. So far, it just prints out in different orders. Please help!

#include <iostream>
#include <cstdlib> 
#include <ctime> 

using namespace std;


struct nodeType
{
    int info;
    nodeType *link;
};


void createList(nodeType*& first, nodeType*& last);
//void alphasort (nodeType*& first, nodeType*& last);
//void printList(nodeType*& first);


int main()
{
    
    nodeType *first, *last;

    
    createList(first,last);
  //  alphasort(first,last);
  //  printList(first); 
    
    
    
    
system ("PAUSE");
return 0;
}


void createList(nodeType*& first, nodeType*& last)
{
     
srand(time(NULL)); 

int numbers[50];
int count = 0;
int i = 0 ;
int random;
int lowest;
char letters[50];

while(count < 50)
{
random = rand() % 90+65;

if(random >= 65 && random <= 90 || random >= 97 && random <= 122)
{
numbers[i] = random;
count++;
i++;
}


}

for(int j = 0; j < 50; j++)
{
letters[j] = numbers[j];

    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     if (letters[j] < numbers[j])
     
     letters[j] = lowest;
     
         newNode = new nodeType;
         newNode->info = numbers[j];
         newNode->link = NULL;
    
         if (first == NULL)
          {
            first = newNode;
            last = newNode;
          }
         else
          {
           last->link = newNode;
           last = newNode;
          }
  
  
        nodeType *current;
        current = first;  
          
        while (current != NULL)
        {  
          cout <<char(current->info)<<endl;
          current = current->link;
        
        }
        
   }
}

Recommended Answers

All 6 Replies

You should google, " sorting arrays".

There are many algorithms for this task.

Or you can let std::sort do the job.

Do I use std::sort like this

std::sort(letters[j]);

Is there another way of using it? Please advise.

Member Avatar for iamthwee

I think you want to sort your struct, not just an array?

Is there another way of using it? Please advise.

Did you bother to even google it at all like firstPerson said? You should have http://www.cplusplus.com bookmarked, or some very similar site. There are loads of them. Do some research yourself before posting here. Post here when you've done the research and have gotten STUCK after trying it. Don't just post here right away. The link is below. Read it carefully and experiment. It has sample code. Or google "sorting arrays" like firstPerson said, and you'll quickly come to Bubble Sort, which is the simplest sort out there to write. Loads of links with sample code.

As far as generating the letters with no repeats, I told you one way of doing it in the last thread. It doesn't hurt my pride if you choose not to follow my method, and there is more than one way to do it, but I gave you something that would work and you haven't implemented it, nor have you implemented it in any other way, nor did you ask any specific question on how to implement it, so I'm wondering if you're actually reading the responses people give you.

http://www.cplusplus.com/reference/algorithm/sort/

Do I use std::sort like this

std::sort(letters[j]);

Just to answer your question :

const int SZ = 10;
int Array[SZ] = {1,6,-2,0,5,54,71,24,70,31 };
std::sort(Array, Array + SZ);

Note you need to include <algorithms>

Be a part of the DaniWeb community

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