0

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;
        
        }
        
   }
}

Edited by NinjaLink: n/a

4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by firstPerson
0

You should google, " sorting arrays".

There are many algorithms for this task.

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

0

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/

Edited by VernonDozier: n/a

0

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>

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.