Presentation is loading. Please wait.

Presentation is loading. Please wait.

6/28/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper recapHistory Paper recap Finish the movieFinish the movie.

Similar presentations


Presentation on theme: "6/28/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper recapHistory Paper recap Finish the movieFinish the movie."— Presentation transcript:

1 6/28/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper recapHistory Paper recap Finish the movieFinish the movie Recap Character I/ORecap Character I/O The getline() functionThe getline() function Converting stringsConverting strings Plus PlusPlus Plus

2 The History Paper Assignment A “short research paper”A “short research paper” –I’m expecting about 2 pages (1 is too short, 3 may be too long) –Do RESEARCH. In other words, LOOK FOR INFORMATION You will report on a dispute in computer historyYou will report on a dispute in computer history –The field is new enough that people don’t agree on what happened, who was first, or what people did. –Find a dispute, document both sides of it, and write it up A good grade = doing the assignmentA good grade = doing the assignment 6/28/2015 2R. Smith - University of St Thomas - Minnesota

3 Now, the movie Pay attention – it’s less reading you’ll have to do for the paper.Pay attention – it’s less reading you’ll have to do for the paper. The MovieThe Movie –It’s from 1995 – so it misses some modern things 6/28/2015 3R. Smith - University of St Thomas - Minnesota

4 Recap: Character I/O Character variables - declarationCharacter variables - declaration char c; // a character variable char str[] = “A string”;// string var Character constants – formatCharacter constants – format –Single quotes, NOT double quotes: c = ‘3’; // literal 3 character c = ‘\n’; // newline character c = ‘\’ ’; // single quote Two basic I/O functionsTwo basic I/O functions –getchar() – returns next character (no arguments) –putchar(c) – outputs character passed as argument (no return) 6/28/2015 4R. Smith - University of St Thomas - Minnesota

5 6/28/2015 5R. Smith - University of St Thomas - Minnesota An I/O Loop char ch; // scratch variable /* copy chars - @ as sentinel */ ch = getchar(); while (ch != ‘@’) { putchar(ch); ch = getchar(); }

6 6/28/2015 6R. Smith - University of St Thomas - Minnesota Char I/O Flow Chart The first block and the diamond form the ‘while’The first block and the diamond form the ‘while’ –We do the calculations and perform the test FIRST Inside of Loop: 2 blocksInside of Loop: 2 blocks –Repeat the blocks after performing the ‘while’ test –First block prints latest char –Second block reads next char ch = getchar(); ch != ‘@’ putchar(ch); False True Done ch = getchar();

7 Using the getline() function Function fills the next line into a stringFunction fills the next line into a string Arguments:Arguments: –String array –Integer: #chars in the array Return: nothingReturn: nothing How does it return a string?How does it return a string? –it doesn’t – the string is an argument How are we changing an argument?How are we changing an argument? –an array is a special type of variable How do we write getline() ?How do we write getline() ? 6/28/2015 7R. Smith - University of St Thomas - Minnesota

8 Types and Conversion We’ve done ‘casting’We’ve done ‘casting’ –Put the type you want in front of the variable you have int x; double y; y = 100 / (double) x; We need to do more when converting stringsWe need to do more when converting strings –This does NOT work: y = (double) “100” / x; 6/28/2015 8R. Smith - University of St Thomas - Minnesota

9 6/28/2015 9R. Smith - University of St Thomas - Minnesota Converting Text to Integer How does ‘getint()’ work?How does ‘getint()’ work? –Let’s write our own: ‘ch2int()’ that takes a string as input The calculation is simpleThe calculation is simple –Convert a number from left to right; use a running total –Multiply the running total by 10 for the ‘tens place’ –Get the ‘ones place’ by subtracting ‘0’ from the character –Add them together for the new running total It looks like the calculation does nothingIt looks like the calculation does nothing –In fact, it’s internally converting the sequence of characters to an integer value

10 6/28/2015 10R. Smith - University of St Thomas - Minnesota Coding the conversion Variables: total, ones, tensVariables: total, ones, tens Loop while there are digitsLoop while there are digits –Multiply last result by 10 to get ‘tens’ –Subtract ‘0’ from next digit to get the ‘ones’ –Add ‘ones’ and ‘tens’ to get the running total When out of digits, return the totalWhen out of digits, return the total Simplification: how do we make the algebra easier? Can we simplify the calculation?Simplification: how do we make the algebra easier? Can we simplify the calculation?

11 Plus Plus OPTIONAL shortcuts in COPTIONAL shortcuts in C We can add doubled operators to increment or decrement variables:We can add doubled operators to increment or decrement variables:X++X-- All operators work in assignment statementsAll operators work in assignment statements X += 10 // adds 10 to x Y /= 10 // divides Y by 10

12 6/28/2015 12R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "6/28/2015 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper recapHistory Paper recap Finish the movieFinish the movie."

Similar presentations


Ads by Google