cProgHW2Part3

cProgHW2Part3 - fclose(infile); fgetc(stdin);...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
C PROGRAMMING HOMEWORK 2 PART 3 /******************* Homework Assignment 2 Part3 array of pointers to character strings file input By: /******************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define INPUT_FILE "inputfile.in" typedef char *string; void sortfunc(string array[], int , int ); void printfunc(string array[], int , int ); void swapper(string array[], int , int ); int main() { char buffer[16]; FILE *infile; infile= fopen(INPUT_FILE, "r"); if (infile == NULL ) { printf("Cannot open file, " INPUT_FILE "\n"); printf("Terminating program. ..\n"); system("PAUSE"); } char w0[16], w1[16], w2[16], w3[16], w4[16]; char w5[16], w6[16], w7[16], w8[16], w9[16]; string buffer[]={w0, w1, w2, w3, w4, w5, w6, w7, w8, w9}; int counter; for (counter=0; counter<10; counter++) { fgets(buffer[counter], 16, infile); } printf ("\n\nThe strings are sorted as follows: \n\n"); sortfunc(buffer[], 0, 9);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: fclose(infile); fgetc(stdin); printf(&quot;\n&quot;); system (&quot;PAUSE&quot;); return 0; } void sortfunc(string array, int block, int lblock) { int counter, nextBlock, swapnum=1; for (; block&lt;lblock &amp;&amp; swapnum; lblock--) //; used for syntax rules. { swapnum = 0; for (counter=block; counter&lt;lblock; counter++) if (strcmp (array[counter], array[counter+1]) &gt; 0) { nextBlock= counter+1; swapper(array, counter, nextBlock); swapnum = 1; } printfunc(array,0,9); } } void printfunc(string array, int block, int lblock) //first-last blocks { int counter; //declared outside loop for (counter = 0; counter &lt;= lblock; counter++) //to avoid errors. printf (&quot;%6s&quot;, array[counter]); printf(&quot;\n&quot;); } void swapper(string array, int i, int j) { string store; store = array[i]; array[i]= array[j]; array[j]= store; }...
View Full Document

Page1 / 2

cProgHW2Part3 - fclose(infile); fgetc(stdin);...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online