scis.regis.edu ● CS-361: Control Structures Week 6 Dr. Jesús Borrego Lead Faculty, COS Regis University 1
Topics Homework #4 review Key Terms Chapter 5 (pp ) ▫Programs using loops and sample programs Activity 1 Chapter 7 (pp ) ▫Functions and file processing Homework #5 2
Homework 4 Review Program 8, page 359 in the textbook Use coding standards Must implement at least 4 functions Review inputs and outputs Submit Word documentation Submit to WorldClass/Desire to Learn before week 6 3
Key Terms Compound statements – sentencias compuestas Nested IF – IF anidado Order of precedence – órden de precedencia Short circuit evaluation – evaluación cortocircuito 4
scis.regis.edu ● C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition) Chapter 7: User Defined Functions II 5
Problem Would like to write a program that asks a user for a large integer number (> 0) The program would then print the digits in reverse order The program must read the number as integer and NOT convert to string Must use at least one function 6
Define the algorithm to be used Together in class: Prompt user for number and verify it is > 0 Process the number evaluating the right most digit and display it to the screen Move the index to the next number from the right until we run out of numbers 7
Solution ReverseDigit0 ReverseDigit1 ReverseDigit2 Convert ReverseDigit2 to use DO WHILE ▫How do we convert from WHILE to DO WHILE? ReverseDigit3 8
Odd or Even? Write a program that receives an integer and determines if it is ODD or EVEN Program OddEven.cpp 9
Count lines and characters in a file Write a program that asks the user for a file name ▫Ensures file exists Reads the file one character at a time ▫Counts number of lines and characters in file Asks the user if the program should run again 10
Solution CountLinesInFile Program Highlights ▫Note validation for answer (Continue, validAns) ▫Note while (More) to determine if user wants to continue ▫Note DoWhile to process the file ▫Note adjustment of characters to account for EOF character ▫Note call to runAgain 11
What is a Prime number? 12
Activity 1 As a single group in class: ▫Write a program to print the prime numbers between 1 and n, where n is received from user ▫Write algorithm on the whiteboard ▫Write pseudocode on the whiteboard ▫Write the program and test it ▫Upload to WorldClass (Week 6) with names of team members Time: 1 hour ListOfPrimes.cpp 13
Review of Char and String functions CharFunctions.cpp Palindrome ▫A string is a Palindrome if it is the same read backwards or forwards palindrome-words-sentences html palindrome-words-sentences html ▫Write a program that determines if a sentence is a palindrome or not PalindromeNoRec.cpp 14
Find a character in a string Want to enter a string with no spaces Want to see if a character is present in the string FindCharInString.cpp See StrFunctions.cpp – array of char See StrFunctions1.cpp for string equivalent ▫See different way of handling Strings versus array of characters 15
Activity 2 Write a program to accomplish the following: ▫Asks the user for a double number with at least 5 digits ▫Includes a function that receives a double number and displays the number of odd, even, and zero digits in the number ▫Includes a function to ask the user to enter a number with at least five digits ▫Calls both functions ▫Test with at least 3 numbers 16
Activity 2 (Continued) Test your program and capture the output Do not worry about coding standards Submit to WorldClass (Week 6) Time: 45 minutes 17
Homework #5 Write a program that asks the user for a file name The program reads the file a character at a time The program will count the following: ▫Number of characters ▫Number of lines ▫Number of digits ▫Number of special characters ▫Number of vowels ▫Number of consonants The program will allow the user to try again 18
Homework #5 (Continued) Program must clear counters after each file is read Test your program with the programs you created for Homework 3 and 4 Follow coding standards Review input and output ▫Capture output of each execution ▫One of the execution must be for invalid file name Submit to WorldClass before week 8 19