Monday 15 June 2015

c - Bubblesort list of words alphabetically. I have an array of pointers pointing to each word -


Then I have a program that takes you to a list and points them in the array of pointers pointing to each word Puts. So far if I just print the line portion of my program with the get_lines function, it prints the .txt file completely. I need help to flush these words alphabetically but I'm not sure how to help!

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; Int Watchfile (FILE * fp, char ** cbuf); Char ** get_lines (four * cbuf, int bufsize, int num_word); Int reddigit (file * tmp); Zero Type A (four lines, int buffess, int new_word); Int main (int argc, char * argv []) {int i, bufsize, num_word; Four * cbuf; Four ** lines; File * fp; File * tmp; File * outfile; Four * new-lines; Outfile = fopen ("words2.txt", "w"); If ((FP = FOPAN (AGR [1], 'R')) == faucet () (mirror ("error: bad / no file name"); exit (0);} TMP = FOPN (RGR [ 1], "R"); Bufsize = readfile (FP, & amp; cbuf); Num_word = Reddit (TMP); lines = join (cbuf, bufsize, num_word); i = 0; while (newline [i]! = Faucet) {FPrint (Outfile, "% s \ n", Newline [i]); i ++;} Return 0;} Int readfile (FILE * fp, char ** cbuf) {int i; four c; fasec (FP, 0L, SEEK_END); Int bufsize = ftell (fp); Fseek (FP, 0L, SEEK_SET); * Cbuf = (four *) molk (size (four) * buffesses +1); = 0; i & lt; Bufsize; i ++) {c = fgetc (FP); (* cff) [i] = c;} return buffes;} int read (file * tmp) {int word = 0; four c; while ((c = Fgetc (tmp)! = EOF) {if (c == '\ n') word ++;} return word;} Char ** get_lines (char * cbuf, int bufsize, int num_lines) {char ** line = Molok (lines (num_lines + 1) * sizeof *); int line = 0; while (line & lt; num_lines) {lines [line ++] = cbuf; cbuf = strchr (cbuf, '\ n'); if ( ! Cbuf break; * Cbuf ++ = '\ 0'; } Lines [line] = null; Return lines; } Zero sorting (four lines, int buffes, int new_word) {four t [buffessis]; Int i, j; (I = 1; i & lt; num_word; i ++) {for (j = 1; j & lt; num_word; j ++) {if (strcmp (line [j - 1], line [j] ) Gt 1) {strcpy (t, line [j-1]); Strakpy (line [j-1], line [ja]); Strcpy (lines [j], t); }}}}  

I got two errors in your code before argv [1 ] before using argc not checking newlines is not being used instead of lines that has not been used otherwise.

I also rewritten sorting () and given it the less logic I corrected the loops, and instead of swapping the stars, I swap pointers you Can not swap the stars - they do not take the same number of bytes, and they sit in your buffer sequentially.

  ... zero SortA (four ** lines, integer number_word); Int main (int argc, char * argv []) {... sorted (line, num_word); I = 0; While (line [i]! = Null) {fprintf (outfile, "% s \ n", line [i]); I ++; } Return 0; } Zero sorting (four ** lines, enter number_word) {four * w; Int i, j; (I = 0; i & lt; num_word-1; i ++) {for (j = i + 1; j & lt; num_word; j ++) {if (strcmp (line [i], line [ J]] = lines [i] lines [i] = line [ja] lines [j] = w;}}}}  

input file:

  one two three four five six seven eight nine ten  

Output file:

  eight five four nine one seven seven ten ten three two  

No comments:

Post a Comment