Overview This assignment provides an example of using pointe

Overview:

This assignment provides an example of using pointers for the extraction, recording and organization of data from a text file. The attached file \'words2.zip\' contain a list of English-language words words2.txt which is to be processed according to instructions in the problems below.

Problem 1: sort1main.c, sort1fns.c, Makefile

Read \'words2.txt\' into a buffer, then sort words into the order by-length, by-alphabetical, using a simple in-place bubble-sort on the original buffer, saving the sorted result as words2_sort1.txt. Do not use any functions from string.h.

Problem 2: sort2main.c, sort2fns.c, Makefile

Read \'words2.txt\' into a buffer, then generate an array of word-pointers in a separate array. Sort words into order by-length, by- alphabetical, using a bubble-sort on the word-pointer array. Save the sorted result as words2_sort2.txt. Do not use any functions from string.h.

***the original words2.txt is too large, about 250,000 words, so here are the first 30 words:

a
aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardvarks
aardwolf
aardwolves
aargh
aarrgh
aarrghh
aas
aasvogel
aasvogels
ab
aba
abac
abaca
abacas
abaci
aback
abacs
abacterial
abactinal

Solution


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ROWS (256)
#define MAX_COLUMNS (256)
#define FILE_NAME \"myInputFile\"

// prototypes
void bubbleSortWordsArray( int wordCount );
void printWordsArray( int wordCount );

static char words[MAX_ROWS][MAX_COLUMNS] = {{\'\\0\',\'\\0\'}};

int main(void)
{
FILE *fp = NULL;

if( NULL == (fp = fopen( FILE_NAME, \"r\") ) )
{
perror( \"fopen failed\" );
exit( EXIT_FAILURE );
}

// implied else, fopen successful

// read each line from file into entry in words array
int i = 0;
while( fgets(words[i], MAX_COLUMNS, fp ) )
{
// remove trailing newline from string
words[i][strlen(words[i])-1] = \'\\0\';
i++;
}

// \'i\' contains number of valid entries in words[][]
// sort the array of strings
bubbleSortWordsArray(i);

printWordsArray(i);

return(0);
} // end function: main


void bubbleSortWordsArray( int wordCount )
{
int c; // outer index through rows
int d; // inner index through rows
char swap[MAX_COLUMNS] = {\'\\0\'};

for (c = 0 ; c < ( wordCount - 1 ); c++)
{
for (d = 0 ; d < (wordCount - c - 1); d++)
{
if( 0 > strcmp( words[d], words[d+1] ) )
{ // then words need to be swapped
strcpy( swap, words[d] );
strcpy( words[d], words[d+1]);
strcpy( words[d+1], swap );
} // end if compare/swap
} // end for
} // end for each row
} // end function: bubbleSortWordsArray


void printWordsArray( int wordCount )
{
int i; // loop index

printf( \"\ \" ); // start on new output line
for( i=0; i<wordCount; i++ )
{
printf( \"%s\ \", words[i] );
}
} // end function: printWordsArray

Overview: This assignment provides an example of using pointers for the extraction, recording and organization of data from a text file. The attached file \'wor
Overview: This assignment provides an example of using pointers for the extraction, recording and organization of data from a text file. The attached file \'wor
Overview: This assignment provides an example of using pointers for the extraction, recording and organization of data from a text file. The attached file \'wor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site