Hello

I am trying to figure out how to create a number generator to generate a number between 65-90 (ascii capital letters) and 97-122 (ascii lowercase letters). I need to convert the number to a char and then put the char in the linked list in order while ignoring duplicates. So far, I am having trouble getting my random generator to work with the range of numbers that I want.. I am currently working in the createList function of my program.

For example:

65 converts to A
66 converts to B
67 converts to C

97 converts to a
98 converts to b
99 converts to c

#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;
    int num;
   
    
    
    createList(first,last);
    alphasort(first,last);
    printList(first); 
    
    
    
    
system ("PAUSE");
return 0;
}


void createList(nodeType*& first, nodeType*& last)
{
     
    int number = rand() % 100 + 1;
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     
         
          while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
     {
         newNode = new nodeType;
         newNode->info = char(number);
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }   

    } 
}


void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void printList(nodeType*& first)
{
        
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << char(current->info)<<endl;
          current = current->link;
        }
}

Edited 7 Years Ago by NinjaLink: n/a

#include <time.h>
#include <iostream>

srand(time(NULL));
	
char randChar = rand()%(90-65)+65;
int makeLower = rand()%2;
if( makeLower == 0 )
{
	randChar += 'a'-'A';
}

This generates a capital char then converts it to a lowercase if makeLower is equal to 0.

1>

int number = rand() % 100 + 1;

produces no. from 1 to 100.

u need no. in the range 65-122
so u have to do

int number = rand() % (122-65) + 65;

2>

while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
     {
         newNode = new nodeType;
         newNode->info = char(number);
         newNode->link = NULL;
          if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }
}

where have u modified "number".

You could even use

rand()%('Z'-'A')+'A';

and this would give you a random char between them.

Is this the correct change you wanted me to do? When I compile, it shows a blank black screen. Do you know where I can solve this problem?

void createList(nodeType*& first, nodeType*& last)
{
     
    srand(time(NULL)); 
    int number = rand()%(90-65)+65;
    int makeLower = rand()%2;
    if( makeLower == 0 )
    {
   	number += 'a'-'A';}
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     
         
          while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
     {
         newNode = new nodeType;
         newNode->info = char(number);
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }   
    } 
}

Edited 7 Years Ago by NinjaLink: n/a

Explain what you are doing here. Do you want to make a whole list of x size or just insert the 2?

1) I want to create a number generator that does not give duplicates for the number range of 65-90 and 97-122. Anything outside of those ranges including between 91-96 will not be in the generator.

2) Since 65-90 and 97-122 are ascii numbers, I want to convert them to their appropriate chars/letters.

For example:

Ascii numbers Letters
97 a
98 b
99 c
100 d
101 e
.
.
.
122 z

65 A
66 B
67 C
68 D
69 E
70 F
.
.
.
90 Z


3) After converting them to their appropriate char, I want to store the chars/letters into a linked list in alphabetical order.

Currently my program runs, the output just shows a black screen with a blinker.


Here is my current program:

#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;
    int num;
    
    createList(first,last);
    alphasort(first,last);
    printList(first); 
    
    
    
    
system ("PAUSE");
return 0;
}


void createList(nodeType*& first, nodeType*& last)
{
     
    srand(time(NULL)); 
    int number = rand() % (122-65) + 65;
   // int number = rand()%(90-65)+65;
   // int makeLower = rand()%2;
   // if( makeLower == 0 )
   //{
   // 	number += 'a'-'A';
   //}
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     
         
          while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
     {
         newNode = new nodeType;
         newNode->info = char(number);
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }   
    } 
}


void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void printList(nodeType*& first)
{
        
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
        }
}

Edited 7 Years Ago by NinjaLink: n/a

Your create list makes 1 random number. How many characters do you want in your list.

Your create list makes 1 random number. How many characters do you want in your list.

I want to use the number generator to generate 50 numbers in the range of 65-90 and 97-122. After, I want to convert the numbers to its char. Then I want to put the chars in a Linked List and print the chars in alphabetical order.

Example of the conversion:

97 a
98 b
99 c
100 d
101 e
102 f
.
.
.
122 z


65 A
66 B
67 C
68 D
69 E
70 F
.
.
.
90 Z

Edited 7 Years Ago by NinjaLink: n/a

So I made a version of this that makes 50 unique chars from A-Z and a-z, stores them, sorts them and then outputs them. I am using the list container and not a structure like what you made. Ask if you want to see some code if this is what you are looking for.

Should I continue creating the generator in my createList function or is it suppose to be in main?

Is this what you are trying to do? Or are you putting it into a custom list type?

#include <iostream>
#include <time.h>
#include <list>

using namespace std;

void makeList(list<char> &inList)
{
	list<char>::iterator it;
	int j;
	for( int i = 0; i < 50; i++ )
	{
		bool isUnique = false;
		while(!isUnique)
		{
			char randChar = rand() % 26 + 'A';
			int makeLower = rand() % 2;
			if( makeLower == 0 )
				randChar += 'a' - 'A';
				
			for( it = inList.begin(), j = 0; it != inList.end(); it++, j++ )
			{
				cout << j << endl;
				if( *it == randChar )
				{
					break;
				}
				else if( j == inList.size()-1 )
				{
					inList.push_back(randChar);
					isUnique = true;
				}
			}
			if( inList.empty() )
			{
				inList.push_back(randChar);
				isUnique = true;
			}
		}	
	}	
}

void printList(list<char> inList)
{
	list<char>::iterator it;
	for( it = inList.begin(); it != inList.end(); it++ )
	{
		cout << *it << endl;
	}
}

int main()
{
	srand(time(NULL));
	list<char> myList;
	
	makeList(myList);
	myList.sort();
	printList(myList);
	
	system("PAUSE");
	return 0;
}

The output is very similar to how I want the program to be except that I want the numbers to range between 65-90 and 97-122.

Right now, my program randomizes only 1 number in the output without converting it to a letter. Is there anything I am doing wrong in my random generator code? 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;
    int num;
    
    createList(first,last);
    alphasort(first,last);
    printList(first); 
    
    
    
    
system ("PAUSE");
return 0;
}


void createList(nodeType*& first, nodeType*& last)
{
     
srand(time(NULL)); 
bool picked[10];


for (int i = 0; i < 10; i++)

picked[i] = false;

int array[10]; 
int number;

for (int i = 0; i < 10; i++)
{
    
number = rand () % (122-65) + 65;

if (picked[number])

i--;

else

{

array[i] = number;

picked[number] = true;

}
}
    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     
     
         newNode = new nodeType;
         newNode->info = char(number);
         newNode->link = NULL;
    
         if (first == NULL)
         {
            first = newNode;
            last = newNode;
         }
         else
         {
           last->link = newNode;
           last = newNode;
         }   
 
}


void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last)
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void printList(nodeType*& first)
{
        
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout << current->info<<endl;
          current = current->link;
        }
}

What's the point of creating an array of random numbers without repeats if you never use the array? Second, you can't just cut and paste the code in the snippet I posted and expect it to work. That code created an array of size ten and made sure that the number created would never be greater than 9, so it worked. You're not creating ten random numbers, you're creating 50, and they don't range from 0 to 9, they range from 65 to 90 or 97 to 122, so since you defined your array as size 10, when you try to use an index of, say, 102, seg fault. Re-read my last post as to what to do and treat the snippet I listed as a guide, not verbatim code. And actually USE the array you create.

How do you add an array to the link list? Do I need to put everything in a for loop in the createList function and store array in current->info?

I created a random number generator that prints out the letters in lowercase and capitalized letters.

Is my numbers ranging from 65-90 and 97-122 in my random number generator?

How do i I use my alphasort function to print out the numbers in alphabetical order with no duplicates? I can't find out what is the problem. Please help


Example of current output:

a
q
R
i
b
g
K
Z

#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;
    //int number;
    
    createList(first,last);
    alphasort(first,last);
    printList(first); 
    
    
    
    
system ("PAUSE");
return 0;
}


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

int numbers[50], count = 0, i = 0, random;
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];
}
for(int j = 0;j < 50; j++)
cout<<letters[j]<<endl;



    nodeType *newNode;
    
    first = NULL;
    last = NULL;
     
     
     
         newNode = new nodeType;
         newNode->info = numbers[i];
         newNode->link = NULL;
    
         if (first == NULL)
          {
            first = newNode;
            last = newNode;
          }
         else
          {
           last->link = newNode;
           last = newNode;
          }
         
}


void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last)
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void printList(nodeType*& first)
{
  
  
        nodeType *current;
        current = new nodeType;
        current = first;    
        while (current != NULL)
        {
          cout <<char(current->info)<<endl;
          current = current->link;
        
        }
}

Edited 7 Years Ago by NinjaLink: n/a

How do I save my createList and be able to print out everything in my printList function. Currently my printList function only prints out one letter instead of the whole list.

#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], count = 0, i = 0, random;
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;
     
     
     
         newNode = new nodeType;
         newNode->info = letters[j];
         newNode->link = NULL;
    
         if (first == NULL)
          {
            first = newNode;
            last = newNode;
          }
         else
          {
           last->link = newNode;
           last = newNode;
          }
    } 
}


void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last)
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void printList(nodeType*& first)
{
  
  
        nodeType *current;
        current = first;    
        while (current != NULL)
        {
          cout <<char(current->info)<<endl;
          current = current->link;
        
        }
}

Read my posts again, as well as the code snippet I listed. Generate 50 numbers in the range of 0 to 51, inclusive, using the logic of my code snippet. Then go through the array element by element and add either 65 or 71. See below. There are only ten and they aren't random. Change them to be random and have 50 of them. But print out the lettters[] array below and you'll see they are all 'A' to 'Z' or 'a' to 'z'.

You can then either sort the array itself, THEN stick them in the Linked List (which is probably easier - no node swaps), or stick them in the Linked List as is and sort the Linked List.

Good luck on this. If you still have problems afer this, it may be time to start a new thread as this one's gotten long.

#include <iostream>
using namespace std;


int main ()
{
    char letters[10] = {5, 28, 34, 12, 50, 44, 47, 23, 13, 10};

    for (int i = 0; i < 10; i++)
    {
        if (letters[i] < 26)
            letters[i] += 65;
        else
            letters[i] += 71;
    }
    
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.