You are on page 1of 6

/** * * * * * * * * * * * */

Usage: mywc < fileName Reports the number of lines, words, and characters in the file named by fileName.

// Note 1.

A word is any sequence of non-blank characters that - begins at the start of a line or immediately after a blank character, and - ends at the end of a line or is followed by a blank character. // Note 1. // Note 2. // Note 2. // not inside a word // inside a word // // // // // // current character current word state: notIn or in characters so far words so far lines so far // Note 3. // Note 3. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note Note 4. 5. 5. 5. 5. 5. 5. 6. 6. 6. 6. 1. 7. // Note 8. 1. 9. 1. 10. 9. 10. 1. 11. // Note 12. 9. 6. 12. 11. 13. 6. 13. 11. 7. 8. 14. 14. 15.

#include <stdio.h> #include <ctype.h> const int notIn = 0; const int in = 1; int main(void) { char ch; int wordState; int numChars; int numWords; int numLines;

wordState = notIn; numChars = 0; numWords = 0; numLines = 0; // for each character in the input ch = getchar(); while (ch != EOF) { // count characters numChars++; // count lines if (ch == '\n') { numLines++; } // count words and adjust word state if (wordState == notIn) { if (!isspace(ch)) { numWords++; wordState = in; } } else { if (isspace(ch)) { wordState = notIn; } } ch = getchar(); } printf("%d %d %d\n", numLines, numWords, numChars); return 0;

// int main(void)

// Note 4.

Compilation Command
g++ -Wall -o mywc mywc.C

Execution Command
mywc < mywc.C

Program output
59 303 1652

/** * Usage: fact * * Prints a number and its factorial. */ #include <stdio.h> /** * factorial(n) returns n*(n-1)*...*2*1 for * small values of n. */ int factorial(int n) { int fact; fact = 1; while (n > 1) { fact *= n; n--; } return fact; // int factorial(int)

// Note 1. // Note 2.

int main(void) { int x; // an integer int f; // the factorial of x x = 5; f = factorial(x); printf("x = %d, f = %d.\n", x, f); return 0; // int main(void) // Note 3. // Note 4.

Compilation Command
g++ -Wall -o fact fact.C

Execution Command
fact

Program Output
x = 5, f = 120.

Page URL: http://www.d.umn.edu /~gshute/C/examples/fact.C.html Page Author: Gary Shute Last Modified: Saturday, 16-Oct-1999 13:21:13 CDT Comments to: gshute@d.umn.edu

include <stdio.h> int A[] = { 99, 43, 22, 17, 57, 32, 43, 19, 26, 48, 87, 12, 75, 0 }; const int numEntries = sizeof(A)/sizeof(int); void qksort(int ilo, int ihi) { int pivot; // pivot value for partitioning array int ulo, uhi; // indices at ends of unpartitioned region int ieq; // least index of array entry with value equal to pivot int tempEntry; // temporary entry used for swapping if (ilo >= ihi) { return; } // Select a pivot value. pivot = A[(ilo + ihi)/2]; // Initialize ends of unpartitioned region and least index of entry // with value equal to pivot. ieq = ulo = ilo; uhi = ihi; // While the unpartitioned region is not empty, try to reduce its size. while (ulo <= uhi) { if (A[uhi] > pivot) { // Here, we can reduce the size of the unpartitioned region and // try again. uhi--; } else { // Here, A[uhi] <= pivot, so swap entries at indices ulo and // uhi. tempEntry = A[ulo]; A[ulo] = A[uhi]; A[uhi] = tempEntry; // After the swap, A[ulo] <= pivot. if (A[ulo] < pivot) { // Swap entries at indices ieq and ulo.

} } // Now, all entries from index ilo to ieq - 1 are less than the pivot // and all entries from index uhi to ihi + 1 are greater than the // pivot. So we have two regions of the array that can be sorted // recursively to put all of the entries in order. qksort(ilo, ieq - 1); qksort(uhi + 1, ihi); } void qksort(void) { qksort(0, numEntries - 1); } void printArray(void) { for (int i = 0; i < numEntries; i++) { printf(" %d\n", A[i]); } printf("\n"); } int main(void) { printf("Before sorting, the entries of A are:\n"); printArray(); qksort(); printf("After sorting, the entries of A are:\n"); printArray(); }

} // Once again, we can reduce the size of the unpartitioned // region and try again. ulo++;

tempEntry = A[ieq]; A[ieq] = A[ulo]; A[ulo] = tempEntry; // After the swap, A[ieq] < pivot, so we need to change // ieq. ieq++; // We also need to change ulo, but we also need to do // that when A[ulo] = pivot, so we do it after this if // statement.

Compilation Command
g++ -Wall -o quicksort quicksort.C

Execution Command
quicksort

Program Output

Before sorting, the entries of A are: 99 43 22 17 57 32 43 19 26 48 87 12 75 0 After sorting, the entries of A are: 0 12 17 19 22 26 32 43 43 48 57 75 87 99

/** * Usage: copy < srcFile > destFile * * Copies srcFile to destFile. */ #include <stdio.h> int main(void) { char ch; // current character // Note 1. // Note 2. // Note 3. // Note 4. // Note 2.

ch = getchar(); while (ch != EOF) { putchar(ch); ch = getchar(); } // int main(void)

Compilation Command

g++ -Wall -o copy copy.C

Execution Command
copy < copy.C > chcount.C

Program effect
In the above command, the contents of the file copy.C are copied to the file chcount.C, which is created if necessary. The command does not send any output to the screen. Page URL: http://www.d.umn.edu /~gshute/C/examples/copy.C.html Page Author: Gary Shute Last Modified: Saturday, 16-Oct-1999 13:21:14 CDT Comments to: gshute@d.umn.edu

You might also like