Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger Fall2018 Lecture 28 P6 overview

2 ECE Application Programming: Lecture 28
Lecture outline Announcements/reminders Program 6 due Monday, 11/19 Program 5 regrades due Monday, 11/26 Today’s lecture Program 6 overview 6/12/2019 ECE Application Programming: Lecture 28

3 ECE Application Programming: Lecture 28
Program 6 overview Read lines of text from input Use array to track # times each letter occurs in input text Use array contents to generate bar graph showing relative frequencies of each letter Gives you practice using arrays and functions Does not require the use of strings 6/12/2019 ECE Application Programming: Lecture 28

4 Overall program structure
main() should contain Array to track letter frequency: int myHist[26] Maximum value in array: int myMax Used to determine height of histogram output Program uses four commands ‘R’, ‘r’: Read a single line of input Call ReadText(myHist, &myMax); to read line ‘P’, ‘p’: Print histogram Call DrawHist(myHist, myMax); to print histogram ‘C’, ‘c’: Clear histogram (and max value) ‘Q’, ‘q’: Quit program Only error checking: invalid command All other input: reading characters, so no formatting errors You may ignore some characters, but they’re not errors 6/12/2019 ECE Application Programming: Lecture 28

5 ECE Application Programming: Lecture 28
ReadText() algorithm Read a single character Do not use a string for the input If that character is a letter, update the appropriate entry in the histogram If that character is not a newline, return to step 1 and read another character Function should also update max value, either as it reads characters or after reading all input characters 6/12/2019 ECE Application Programming: Lecture 28

6 ECE Application Programming: Lecture 28
ReadText() hints <ctype.h> functions will help in ReadText() isalpha(ch): returns “true” if ch is letter, “false” otherwise Directly applies to one step on previous slide Converting each letter to same case makes it easier to find appropriate entry in histogram toupper(ch): returns uppercase letter if ch is lowercase letter; returns original ch otherwise toupper('x') = 'X'; toupper('A') = 'A' tolower(ch): returns lowercase letter if ch is uppercase letter; returns original ch otherwise tolower('x') = 'x'; tolower('A') = 'a' 6/12/2019 ECE Application Programming: Lecture 28

7 ReadText() hints (continued)
Finding appropriate entry in histogram does not require conditional statement You shouldn’t need to compare your input character to anything to find correct array index Very basic “transformation” between ASCII value of letter and histogram index Can treat a char variable as either Character to be printed, or Integer value corresponding to printed character ASCII values Uppercase and lowercase letters separate Each set of letters is consecutive ‘A’ = 65, ‘B’ = 66, … ‘Z’ = 90 ‘a’ = 97, ‘b’ = 98, … ‘z’ = 122 6/12/2019 ECE Application Programming: Lecture 28

8 What not to do in ReadText()
Using brute force method to find appropriate histogram index will incur -10 deduction Brute force methods basically compare input letter to all possible letters Prohibited brute force method #1: giant conditional statement switch(ch) { // ch = input char case ‘A’: case ‘a’: // modify histo[0] break; case ‘B’: case ‘b’: // modify histo[1] etc … } 6/12/2019 ECE Application Programming: Lecture 28

9 What not to do in ReadText() (cont)
Using brute force method to find appropriate histogram index will incur -10 deduction Brute force methods basically compare input letter to all possible letters Prohibited brute force method #2: loop char test = ‘A’; for (i = 0; i < 26; i++) { if (ch == test) // modify histo[i] test++; // Change test } // to next letter 6/12/2019 ECE Application Programming: Lecture 28

10 ECE Application Programming: Lecture 28
DrawText() algorithm For each row of output For each entry in histogram If current entry is at least row #, print “| “ (bar & space) Otherwise, print “ “ (two spaces) Must print bar graph from top to bottom # rows based on max value in histogram Printing spaces necessary to get everything to line up 6/12/2019 ECE Application Programming: Lecture 28

11 ECE Application Programming: Lecture 28
Final notes Next time Review structures PE3 Reminders: Program 5 regrades due Monday, 4/9 Program 6 due today Program 7 due Friday, 4/13 6/12/2019 ECE Application Programming: Lecture 28


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google