CSE 5A Introduction to Programming I (C) Homework 4

Similar documents
CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 5 Formatting and Flow Control

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CIT 590 Homework 5 HTML Resumes

Programming Style Guide v1.1

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CS150 Assignment 7 Cryptography

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Tips from the experts: How to waste a lot of time on this assignment

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS 211 Programming Practicum Fall 2018

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

BLM2031 Structured Programming. Zeyneb KURT

CS Homework 10 p. 1. CS Homework 10

EECE.2160: ECE Application Programming Spring 2018

CS2 Practical 1 CS2A 22/09/2004

The Caesar Cipher Informatics 1 Functional Programming: Tutorial 3

printf( Please enter another number: ); scanf( %d, &num2);

Programming Assignment 2 ( 100 Points )

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

Using C++, design an Abstract Data Type class named MyGrades. The class must have the following private members :

This is a combination of a programming assignment and ungraded exercises

Fundamentals of Programming Session 4

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

More examples for Control statements

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Project#3 [40 points] COP 2220 Fall 2017

1. Introduction. 2. Deliverables

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

EECS2031 Winter Software Tools. Assignment 2 (15%): C Programming

ECE15: Lab #4. Problem 1. University of California San Diego

Tips from the experts: How to waste a lot of time on this assignment

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

EECE.2160: ECE Application Programming

SPRING 2017 CSCI 304 LAB1 (Due on Feb-14, 11:59:59pm)

Fundamental of Programming (C)

Fundamentals of Programming. Lecture 3: Introduction to C Programming

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Introduction to C Programming

Tips from the experts: How to waste a lot of time on this assignment

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

CS50 Supersection (for those less comfortable)

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

LESSON 4. The DATA TYPE char

Tail recursion. Decision. Assignment. Iteration

Homework 1 - Extracting Data from a CSV file

Chapter 4 C Program Control

CS Homework 11 p. 1. CS Homework 11

CS 470 Operating Systems Spring 2013 Shell Project

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Use of scanf. scanf("%d", &number);

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 4 (document version 1.1) Sockets and Server Programming using C

Programming Project 5: NYPD Motor Vehicle Collisions Analysis

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Engr 123 Spring 2018 Notes on Visual Studio

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Installing and Using Dev-C++

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C

Hands on Assignment 1

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Question 2. [2 points] True False By default, structures are passed-by-reference.

Chapter 4: Making Decisions

Functions BCA-105. Few Facts About Functions:

CSCE 206: Structured Programming in C++

Functions. Arash Rafiey. September 26, 2017

CS Homework 11 p. 1. CS Homework 11

Programming Assignment 1 (PA1) - Display Bowtie

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Assignment 5: MyString COP3330 Fall 2017

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

COMP 321: Introduction to Computer Systems

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

Project #1: Tracing, System Calls, and Processes

CS 2316 Pair 1: Homework 3 Enigma Fun Due: Wednesday, February 1st, before 11:55 PM Out of 100 points

Programming Assignment 8 ( 100 Points )

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS 241 Data Organization. August 21, 2018

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CpSc 1111 Lab 9 2-D Arrays

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 9 EECE.3220 Data Structures Fall 2017

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS16 Exam #1 7/17/ Minutes 100 Points total

ECE2049 Embedded Computing in Engineering Design. Lab #0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

CS 241 Data Organization using C

Transcription:

CSE 5A Introduction to Programming I (C) Homework 4 Read Chapter 7 Due: Friday, October 26 by 6:00pm All programming assignments must be done INDIVIDUALLY by all members of the class. Start early to ensure you have enough time to submit your program before the deadline. All assignments will be automatically collected from your computer account at precisely 6:00 pm on the due date shown above. These due dates and the time will be rigidly applied. An assignment that is not in place at 6:00pm on the due date will not be collected and therefore will not be graded, receiving a score of 0. If you have any questions/problems with the turn-in, see one of the tutors, TA, or the instructor. All your programs must be neat and legible. Indent and comment your programs as shown in the text and as described in class. Make sure to thoroughly understand the assignment before you start on it. It is a good idea to verify your understanding of the problem with the tutors, TA, or instructor. For maximum credit, read and follow the style rules given in the CSE 5A Program Grading document (http://www.gregmiranda.com/wp-content/uploads/2018/08/cse-5a- Program-Grading.pdf). Getting Started In the lab, log into the Windows computer using your cs5af account (not your UCSD account). Using Visual Studio 2017, create a new project (File->New->Project ) and select the Visual C++ Empty Project. Change the Name of the project to HW4 (note the uppercase HW4). Change the Location of the project to your cs5af Home Directory (H:\). After changing the name and the location, click on the OK button. In the Solutions Explorer window, create a new file by right clicking on the HW4 solution and selecting Add->New Item from the context menu. Next, select C++ File (.cpp). Change the Name of the file to hw4.c. You do not need to change the location, it should default to your HW4 folder inside the HW4 project (H:\HW4\HW4\). After changing the name, click the Add button. Make sure to save everything (File->Save All). Link to a video for creating a new project and source file: http://www.gregmiranda.com/cse5a-new-project Feel free to ask the tutors on duty in the lab for help. Program Description Homework 4 (hw4.c) will be a program that implements a Caesar cipher. The program will also have functions, user input (getchar()/scanf()), conditional (switch/if-else), and looping (while/do while & for loop). This program should be modular, with logical tasks broken up into functions. In this assignment you will write a program using functions to display output to the console (stdout). Do NOT use global variables in CSE 5A. Function headers are required for the function bodies. The function declarations should be placed before the main() function, while the function bodies should be placed after the main() function.

a) Your program must loop so that the user can continue to generate and view the output until the user enters the integer 0 to choose the Exit option from the menu (described later). NOTE: Your program will end ONLY with the input of 0. Use the following code as your main() driver: int main(void) { int choice; //Stores user input int shift = 3; //Number of letters to shift _Bool lowercase = 1; //The case of the character to display menu(); do { printf("\nenter a number (0-5) to choose an option from the menu above: "); scanf("%d", &choice); getchar(); switch(choice) { case 1: printcipher(shift, lowercase); case 2: encrypttext(shift, lowercase); case 3: shift = readshift(); case 4: lowercase = readcase(); case 5: //encryptline(shift, lowercase); case 0: printf("exiting...\n"); default: } } while (choice!= 0); } return 0; b) Create a function called menu() with a return type of void which takes no parameters. Your function declaration should look like: void menu(void); In this function, you will display the menu of options to the user using printf(). The menu should appear to the user as:

Menu: 1) Print Cipher 2) Encrypt Text 3) Change Shift 4) Change Case 5) Extra Credit: Encrypt Line 0) Exit c) Create a function called printcipher() with a return type of void which takes an int and a _Bool as parameters. Your function declaration should look like: void printcipher(int, _Bool); Use a loop to display the alphabet in the proper case (the _Bool parameter). The _Bool parameter is set to 1 (true) to display lowercase letters and 0 (false) to display uppercase letters. On the next line, use a loop to display the shifted alphabet in the proper case. To perform the shift, add the shift value to the character (the int parameter). Example with a shift value of 3: A + 3 is D. The shift value can also be negative, shifting the other direction. Example with a shift value of -3: A - 3 is X. Be careful with characters at the beginning or end of the alphabet that wrap around. Note: you will need to support numbers larger than 25 (and smaller than -25). Calling printcipher(3, 1) produces the output (shift 3, lower case): defghijklmnopqrstuvwxyzabc Calling printcipher(5, 0) produces the output (shift 5, upper case): ABCDEFGHIJKLMNOPQRSTUVWXYZ FGHIJKLMNOPQRSTUVWXYZABCDE Calling printcipher(29, 1) produces the output (shift 29, lower case): defghijklmnopqrstuvwxyzabc d) Create a function called readshift() with a return type of int which takes no parameters. Your function declaration should look like: int readshift(void); In this function, take user input until a valid positive OR negative integer is entered (0 is neither positive nor negative). Within the body of readshift(), create a variable called shift of type int. For example: int shift; /* Store user input */ Begin by prompting the user to enter a positive or negative integer by displaying the message Enter a positive or negative number to shift: to the console using printf(). Take user input using the scanf() Standard C Library function and store the result in shift. Display the error message ERROR: Enter a valid positive or negative

number! if zero is input. A loop will repeat the prompt until a valid input is entered. Return the shift value. Note: a value of 26 is valid, even though the result of shifting by 26 does not cause a shift of letters. e) Create a function called readcase() with a return type of _Bool which takes no parameters. Your function declaration should look like: _Bool readcase(void); In this function, you will take user input until the user enters U / u or L / l character. Within the body of readcase() create two variables called character and lowercase. For example: char character; /* Stores user input */ _Bool lowercase; /* Stores result of the input */ Begin by prompting the user to enter a U, u, L, l by display the message Enter U for uppercase cipher or L for lowercase cipher [U/L]: to the consoles using printf(). Take user input using the getchar() Standard C Library function. Store the character entered by the user in the character variable. Check to make sure the user entered valid input. Display the error message ERROR: Enter U for uppercase cipher or L for lowercase cipher! if the character entered is not valid. Prompt for another character to be input. A loop will repeat the prompt until a valid input is entered. Once the user has entered valid input, save the result in lowercase. Return the lowercase value. f) Create a function called encrypttext() with a return type of void which takes an int and a _Bool as parameters. Your function declaration should look like: void encrypttext(int, _Bool); Like printcipher() the int parameter holds the shift value and the _Bool parameter holds the lowercase flag. Ask the user to enter a word to encrypt by displaying the message Enter a word to encrypt: to the console using printf(). In a loop, take user input using the getchar() or the scanf() function. After reading each character, display the shifted value of the character to the console in the selected case from the _Bool parameter. You may need to convert an uppercase letter to lowercase or vice versa. Loop until the user enters a newline character \n. Note: any character that is not a letter should not be shifted and displayed as is (spaces, numbers, punctuation, etc.). Keep in mind that the loop doesn t run until the user hits the enter key, so nothing needs to be stored except for a single character that is read in, shifted, and then printed. Calling encrypttext(3, 1) could produce the following output: Enter a word to encrypt: this is a test! wklv lv d whvw! Calling encrypttext(5, 0) could produce the following output: Enter a word to encrypt: this is a test! YMNX NX F YJXY!

HINT: Solve the problem in small steps. Here s a suggestion. 1) Enter the code for step a), b), c). Test 2) Write the code for step d). Ignore error checking for now. Test. 3) Write the code for step e). Ignore range checking for now. Test. 4) Write the code for step f). Test. 5) Complete the code for step d) and e) above to range check your input. Test 6) Test. Test. Done! 7) Work on the extra credit!! Extra Credit Prior to working on the extra credit, create a backup of your file just in case something goes wrong. I recommend storing it on Google Drive or Dropbox. Extra Credit Part 1 (+1 points) g) Create a function called convertchar() with a return type of char which takes an int, _Bool, and char as parameters. Your function declaration should look like: char convertchar(int, _Bool, char); This function takes the char parameter and shifts it by the int parameter and changes the case to match the _Bool parameter, just as in printcipher(). If the character in the char parameter is not a letter, return the value as is (unshifted, no case change). Otherwise, return converted character (shifted, case changed). Modify encrypttext()to use convertchar() to shift the value and change the case prior to printing the value to the console. Make sure to remove the outdated code (or comment it out). After modifying encrypttext(), the program should work the same way, just using the convertchar() function instead of having the shifting code inside the encrypttext() function. Extra Credit Part 2 (+1 points) h) For this part, you will need to create two functions. The first function, encryptline(), is used to call the second function, encrypt(), from the menu. Together, both functions work exactly like encrypttext() from part f. First, create a function called encryptline() with a return type of void which takes an int and a _Bool as parameters. Your function declaration should look like: void encryptline(int, _Bool); Like encrypttext() the int parameter holds the shift value and the _Bool parameter holds the lowercase flag. Ask the user to enter a word to encrypt by displaying the message Enter a word to encrypt: to the console using printf(). Next, call the encrypt() function. Create a function called encrypt() with a return type of void which takes an int and a _Bool as parameters. Your function declaration should look like: void encrypt(int, _Bool);

This function is a recursive function, which is a function that calls itself. Every recursive function needs an end case, or it will act like an infinite loop until all the program s available stack space is used up and the program crashes. The end case for this function is the newline character \n. Each time the function is called, it should read a character from stdout using getchar() or scanf(). Then it should call the convertchar() function and print out the shifted value to the console. Finally, if the newline character hasn t been read in, the function should call itself. Example Execution of the Program (user input is in bold): Menu: 1) Print Cipher 2) Encrypt Text 3) Change Shift 4) Change Case 5) Extra Credit: Encrypt Line 0) Exit Enter a number (0-5) to choose an option from the menu above: 1 defghijklmnopqrstuvwxyzabc Enter a number (0-5) to choose an option from the menu above: 3 Enter a positive or negative number to shift: 5 Enter a number (0-5) to choose an option from the menu above: 1 fghijklmnopqrstuvwxyzabcde Enter a number (0-5) to choose an option from the menu above: 4 Enter U for uppercase cipher or L for lowercase cipher [U/L]: k ERROR: Enter U for uppercase cipher or L for lowercase cipher! Enter U for uppercase cipher or L for lowercase cipher [U/L]: u Enter a number (0-5) to choose an option from the menu above: 3 Enter a positive or negative number to shift: 0 ERROR: Enter a valid positive or negative number! Enter a positive or negative number to shift: -5 Enter a number (0-5) to choose an option from the menu above: 1 ABCDEFGHIJKLMNOPQRSTUVWXYZ VWXYZABCDEFGHIJKLMNOPQRSTU Enter a number (0-5) to choose an option from the menu above: 2 Enter a word to encrypt: This is a test! OCDN DN V OZNO!

Enter a number (0-5) to choose an option from the menu above: 5 Enter a word to encrypt: This is a test! OCDN DN V OZNO! Enter a number (0-5) to choose an option from the menu above: 9 Enter a number (0-5) to choose an option from the menu above: 0 Exiting... Press ENTER to exit! Final Notes Make sure you have your name and cs5af account number in the header comment of hw4.c. Note: replace XX in the Login: cs5afxx with your unique login name. /* * Name: Jane-Joe LastName * Login: cs5afxx * Date: Month Day, Year * File: hw4.c * Sources of Help: * General description of the program... */ #pragma warning(disable:4996) #include <stdio.h> //Disable security warnings Make sure to include the #pragma warning line after the program header or your program will not compile in Visual Studio. Comment your program like the previous programs (and the Program Grading guide). Make sure to use meaningful comments. You can download an example executable for Homework 4 from the following link: http://www.gregmiranda.com/cse5a-hw4-sample Verify you saved your work in the cs5af HOME directory (H:\HW4\hw4.c or H:\HW4\HW4\hw4.c). For homework 4, we will also be turning them in using gradescope.com, which is an LMS (learning management system). Grade Scope is an online tool that will make it easier for us to collect homework assignments, grade them, and return them to you. Keep an eye out for an email from Grade Scope as well as a Piazza post with instructions on how to turn in your homework 4. However, make sure to keep a copy of your homework 4 in the HOME directory like previous homework assignments just in case there is an issue.