### The first step in every computer algorithm

• The Text Formatting problem

• Example:

• You are given the following text file:

 ``` WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the Pursuit of Happiness. ```

• You must produce the following result:

 ``` WE hold these Truths to be certain unalienable Rights, self-evident, that all Men are that among these are Life, created equal, that they are Liberty and the Pursuit of endowed by their Creator with Happiness. ```

• Before we start: A crucial difference between humans and machines

• There are 3 major differences between humans and machines:

 Flexibility Flexibility Flexibility

(Yeah, I got this sales pitch from my real estate agent :-))

• Manifestation of the differences between humans and computers:

• Humans can work with information stored in any form or shape

• Computers must arrange the storage of the information in a form that is adequate (suitable) for the algorithm

 The complexity (difficulty) of a computer algorithm depends heavily on the data structure used to stored the information that is used by the algorithm.

• The first step in every computer algorithm

• The first step in every computer algorithm is a direct result of the differences between humans and machines

• The first step in every computer algorithm is:

 Read in the input data needed to solve the problem Store the input data in such a way that will help make the algorithm easier to write.

• Data structure to store in the input of the Text Formatting problem

• First thing we must do is:

 Design (decide on) a data structure (way to store information) for the input (text) for the text formatting problem The way that the information is stored must help us write/formulate the text formatting algorithm !!!

• Observation:

 The text formatting algorithm will manipulate words (We will not break a word with hyphenization... E.g., we will not break iceberg into ice- and berg)

Therefore:

 Store the input text as an array of String (Each word in the input is a String stored in an array element)

• Example:

• Input file:

 ``` WE hold these Truths to be self-evident, that all Men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the Pursuit of Happiness. ```

• Stored as:

 ``` String[] words = new String[10000]; int numWords = 35 word[0] = "WE" word[1] = "hold" word[2] = "these" word[3] = "Truths" word[4] = "to" word[5] = "be" word[6] = "self-evident," word[7] = "that" word[8] = "all" word[9] = "Men" word[10] = "are" word[11] = "created" word[12] = "equal," word[13] = "that" word[14] = "they" word[15] = "are" word[16] = "endowed" word[17] = "by" word[18] = "their" word[19] = "Creator" word[20] = "with" word[21] = "certain" word[22] = "unalienable" word[23] = "Rights," word[24] = "that" word[25] = "among" word[26] = "these" word[27] = "are" word[28] = "Life," word[29] = "Liberty" word[30] = "and" word[31] = "the" word[32] = "Pursuit" word[33] = "of" word[34] = "Happiness." ```

• Reading in the input data

• Algorithm in pseudo code:

 ``` Open the file Create a "Scanner" to read data Read the input file into an array of String Note: we must remember the number of words read ! ```

• Program in Java:

 ``` import java.io.*; import java.util.Scanner; public class FormatText1 { public static String[] words = new String[10000]; // Hold words in input file public static int numWords; // Count # words in input /* =========================================================== readInput(in, w): read words from input "in" into array w and return the #words read ============================================================ */ public static int readInput( Scanner in, String[] w ) { int nWords = 0; String x; while ( in.hasNext() ) { x = in.next(); // Read next word w[nWords] = x; // Store it away nWords++; // Count # words AND use next // array element to store next word } return ( nWords ); } public static void main(String[] args) throws IOException { if ( args.length == 0 ) { System.out.println( "Usage: java FormatText1 inputFile"); System.exit(1); } File myFile = new File( args[0] ); // Open file "inp2" Scanner in = new Scanner(myFile); // Make Scanner obj with opened file /* ---------------------------- Read input from data file ---------------------------- */ numWords = readInput( in, words ); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac FormatText1.java To run:          java FormatText1 inp1 Or:                 java FormatText1 inp2