comp206_f2023_assign3

pdf

School

McGill University *

*We aren’t endorsed by this school

Course

206

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

4

Uploaded by AdmiralTitaniumKoala36

Report
School of Computer Science, McGill University COMP-206 Introduction to Software Systems, Fall 2023 Assignment 3: C Programming Due Date Nov 1, 17:59 EST TA Office Hours will start on Oct 26: See MyCourses announcement for list This is an individual assignment. You need to solve these questions on your own. Use mimi.cs.mcgill.ca to create the solution to this assignment, and write all the code using Vim. An important objective of the course is to make students practice working completely on a remote system. Therefore, you must not use your Mac command-line, Windows command-line, nor a Linux distro installed locally on your laptop. You can access mimi.cs.mcgill.ca from your personal computer using ssh or putty as seen in class and in Lab A. If we find evidence that you have been instead using your laptop without using ssh, etc., to do some parts of your assignment work, you might lose all of the assignment points. All of your solutions should be executable in mimi.cs.mcgill.ca . For this assignment, you will have to turn in two C programs and two Bash testers. Instructors/TAs upon their discretion may ask you to demonstrate/explain your solution. No points are awarded for programs that do not execute at all. (Programs that execute, but provide incorrect behavior/output will be given partial marks.) TAs WILL NOT modify your programs in any ways to make them work. Please read through the entire assignment before you start working on it. You can lose up to 3 points for not following the instructions in addition to the points lost per questions. Total Points: 20 Some background information: The way to get arguments from the command line when the program is called is to have the following signature for the main function: int main(int argc, char *argv[]) . argc is then the number of arguments that were inputted (including the filename), while argv is an array of strings. You will get a deeper understanding of why strings have type char * next week (as of the date this assignment was posted), but for now to complete the assignment the important is this: Let’s say that you call a program as ./program hello world argc will be equal to 3 and argv will be this array: ["./program", "hello", "world"] argv[0] will be ”./program”, argv[1] will be ”hello”, argv[2] will be ”world” Since strings themselves are actually sequences of characters, you can do argv[1][0] to get ’h’, argv[1][1] to get ’e’, argv[1][2] to get ’l’, etc. C knows when a string ends because they end in a null character ( \ 0). You can get the length of a string (i.e. before the null character) with the function strlen() from <string.h> . For example, strlen("foo") would give you 3, and in the example above strlen(argv[1]) would give you 5. Note that if you ever read a string from standard input, the presence of the newline character that the user input when pressing enter depends on the function. For example, fgets reads the newline from buffer and includes it before the null character, while scanf keeps the newline in the buffer. If you need to copy a string, you need to use a function like strcpy() from <string.h> . Ex. 1 — Divisible (6 points) Before doing 1.1 below, make sure to read 1.2 too as it involves making a tester for your program which might be useful while coding it. 1
1. (5 points) Write a C program divisible.c (compiled as the program divisible ) that prompts the user for 3 integer numbers and then verifies if the numbers are all divisible by the first number without a remainder AND that the three number are in increasing order. The program must do the following: 1. Return an error code of 0 if the numbers are all divisible by the first number and are in increasing order. Must also print Divisible & Increasing to screen. 2. Return an error code of 1 if the numbers are not all divisible by the first number and are in increasing order. Must also print Not divisible & Increasing to screen. 3. Return an error code of 2 if the numbers are all divisible by the first number and are not increasing. Must also print Divisible & Not increasing to screen. 4. Return an error code of 3 if the numbers are not all divisible by the first number and are not increasing. Must also print Not divisible & Not increasing to screen. The program terminates after doing this one time. Example execution (after compiling the file to an executable named divisible ): $ ./divisible Please input three numbers: 10 5 7 Not divisible & Not increasing $ ./divisible Please input three numbers: 3 6 9 Divisible & Increasing $ ./divisible Please input three numbers: 5 20 10 Divisible & Not increasing Submit divisible.c . Do not submit the executable. We will compile the file using gcc. 2. (1 point) Write a bash tester divtester.bash for your program. Please include the shebang. This tester should run your program with a couple of test cases. It should be run from the same directory as your program (i.e. to refer to your program the tester will use ./divisible ). To get the point for this, your tester should include the following tests: 1. The first test should first display Test 1: not divisible & not increasing , then run the divisible program with numbers “10 5 7” (you will need to use redirection to replace the keyboard input). Following the output from the command itself you should display Expected output message: Not divisible & Not increasing 2. The second test should first display Test 2: divisible & not increasing , then run the divisible program with numbers “5 20 10”. Following the output from the command itself, depending on the error code you should display either Exited with expected return code: 2 or Exited with unexpected return code: X (Expected 2) (where X is the error code from your program). You are allowed to put as many more additional tests as you want as it might be helpful to test your program. Example execution (Note that this might be different depending on if division.bash is correct for these tests or not. Also, the numbers don’t appear next to “ Please input three numbers: since they are being redirected): $ ./divtester.bash Test 1: not divisible & not increasing Please input three numbers: Not divisible & Not increasing Expected output message: Not divisible & Not increasing Test 2: divisible & not increasing Please input three numbers: Divisible & Increasing Exited with unexpected return code: 0 (Expected 2) Submit divtester.bash Ex. 2 — Anagram (14 points) Before doing 2.1 below, make sure to read 2.2 too as it involves making a tester for your program which might be useful while coding it. 2
1. (13 points) Create a C program called anagram.c (compiled as the program anagram ) that implements an anagram check on 2 words entered from the command line. For this assignment, an anagram will be a sequence of characters formed by rearranging the letters of a different sequence of characters using all the original characters exactly once. For example, the word anagram can be rearranged into nagaram , or the word binary into brainy , or the word 123 abc into 3 acb21 . woo is not considered an anagram of ow . Note that this is case sensitive. Specifically, the user will run the program by typing the following at the command line prompt: ./anagram WORD1 WORD2 . Note: you are not checking if the anagram is a proper word. The program requires the user to input 2 words then returns the error code 0 if the words are anagrams and prints to the screen “Anagram”, or the error code 1 otherwise and prints to the screen “Not an anagram”. The program does this once and then terminates. Example execution (after compiling the file to an executable named anagram ): $ ./anagram mary ramy Anagram $ ./anagram bob lary Not an anagram $ ./anagram bob boo Not an anagram Submit anagram.c . Do not submit the executable. We will compile the file using gcc. 2. (1 point) Write a bash tester anagramtester.bash for your program. Please include the shebang. This tester should run your program with a couple of test cases. It should be run from the same directory as your program (i.e. to run your program the tester will do ./anagram WORD1 WORD2 ). To get the point for this, your tester should include the following tests: 1. The first test should first display Test 1: mary and ramy , then run the anagram program with words “mary” and “ramy”. Following the output from the command itself you should display Expected output message: Anagram 2. The second test should first display Test 2: bob and lary , then run the anagram program with words “bob” and “lary”. Following the output from the command itself, depending on the error code you should display either Exited with expected return code: 1 or Exited with unexpected return code: X (Expected 1) (where X is the error code from your program). You are allowed to put as many more additional tests as you want as it might be helpful to test your program. Example execution (Note that this might be different depending on if anagram.bash is correct for these tests or not): $ ./anagramtester.bash Test 1: mary and ramy Anagram Expected output message: Anagram Test 2: bob and lary Not an anagram Exited with expected return code: 1 Submit anagramtester.bash WHAT TO HAND IN Upload your two programs and two testers, divisible.c , anagram.c , divtester.bash and anagramtester.bash to MyCourses under the Assignment 3 folder. You do not have to zip all of the files together, but you might need to if MyCourses does not accept the file format. Re-submissions are allowed, but please try to upload all of the files again (and not just the modified ones) so that TAs do not have to go over multiple submissions to find correct files. You are responsible to ensure that you have uploaded the correct files to the correct assignment/course. No emailing of submissions . If it is not in MyCourses in the correct submission folder, it does not get graded. Late penalty is -10% per day . Even if you are late only by a few minutes it will be rounded up to a day. Maximum of 2 late days are allowed. Even if you only (re)submit a part of the assignment (e.g., one script) late, late penalty is applicable to the entire assignment. 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
QUESTIONS? If you have questions, post them on the MyCourses discussion board, under the Assignment 3 forum, but do not post major parts of the assignment code. Though small parts of code are acceptable, we do not want you sharing your solutions (or large parts of them) on the discussion board. If your question cannot be answered without sharing significant amounts of code, please use TA/Instructors office hours. Also check the MyCourses FAQ under “Assignment 3” before you post a new question. If it is already discussed there, it will not get a response. Emailing TAs and Instructors for assignment clarifications, etc., is not allowed. You can email your TA only if you need clarification on the assignment grade feedback that you received. 4