You are here:

C++/Select random words from linked list

Advertisement


Question
I have a program that currently reads a text file of words into a linked list and then creates sequential files.  I need to randomly select words from the list and input them into the created text files.  Any help will be greatly appreciated.  Also, if there is a better way to do this please let me know.  Thank you for your time.

//---------------------------------------------------------
#include <vclcondefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <windows.h>

#pragma hdrstop

//---------------------------------------------------------
typedef struct TList{
       char * word;
       int count;
       TList * next;
       TList()
       {
         word = NULL;
         next = NULL;
         count = 0;
       }
}TList;

//---------------------------------------------------------
#pragma argsused
int main()
{
       // variables
       char fileName[40];
       int x = 1, length = 0, numWords = 0;
       int fileSize = 100 * 1024;
       char ch = 0, prevChar;
       char newWord[1024];
       int newWord_len = 0;

       // Open vocabulary file
       FILE * vocabFile = fopen("c:Test Filesvocabulary.txt", "r");

       // Get Length of File
       fseek (vocabFile, 0, SEEK_END);
       length = ftell (vocabFile);
       fseek (vocabFile, 0L, SEEK_SET);

       TList * start_ptr = NULL;
       TList * end_ptr = NULL;
       TList * temp = NULL;

       srand(time(0));

       // Read words from file into a structure
       for (int i = 0; i < length; i++)
       {
         // convert to lowercase and assign to variable
         ch = fgetc(vocabFile);

         // append characters to string
         if (!(ispunct(ch) || isspace(ch)))
         {
         newWord[newWord_len++] = ch;
         }

         // keep count of words
         if ((isalnum(prevChar)) && (isspace(ch) || ispunct(ch) && ch != ''' && ch != '-'))
         {
         // ensure null terminator is included in the string
         newWord[newWord_len++] = '0';

         // check list to see if it already exists
         for (temp = start_ptr; temp != NULL; temp = temp->next)
         {
         if(strcmpi(newWord, temp->word)==0)
         {
         temp->count++;
         break;
         }
         }
         
         if (temp == NULL)
         {
         // reserve block of memory for structure and initialize variables
         temp = new TList;
         temp->count = 1;
         temp->next = NULL;
         temp->word = new char[newWord_len];

         // copy the word to the structure instance
         strcpy(temp->word, newWord);

         // head has no value - assign word to head
         if (start_ptr == NULL)
         {
         start_ptr = temp;
         end_ptr = temp;
         }
         // head has value - assign word to next node
         else
         {
         end_ptr->next = temp;
         end_ptr = temp;
         }
         }

         // increase word count
         numWords++;

         // set new word length back to zero for next word
         newWord_len = 0;
       }
       // assign character to a variable for comparison in the loop
       prevChar = ch;
       }

       // show word list on screen
       for(temp = start_ptr; temp != NULL; temp = temp->next)
       {
      printf(""%s"n", temp->word);
       }

       // Create files
       for (x; x <=3; x++)
       {
         // create sequential file names
         sprintf(fileName,"C:Test FilesPerformanceTEST%d.txt", x);
         printf("%sn", fileName);
         FILE * textFile = fopen(fileName, "wt");

         int r = rand() % 10;

         // randomly select words from list
         for(temp = start_ptr; temp != NULL; temp = temp->next)
         {

         temp->word = r;
         }
         
         // close random file
         fclose (textFile);

         // increase file size
         fileSize += 200 * 1024;
       }
       fclose (vocabFile);
       getch();
       return 0;
}
//---------------------------------------------------------


Answer
The Create files section has problems.

The for loop just has x; - probably needs x=1;

I'm not sure why you set temp->word to r. And where is textFile written to?  Why is r rand() % 10?  You should count nodes in the list so you can pick one node randomly.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.