Assignment 2
Assignment 2
Assignment 2
Individual 15% of Coursework 2 weeks after the distribution 10 marks deducted per day late Plagiarism will be penalized according to college regulation
B. Grading Criteria:
Criteria Program correctness Appropriate program style, structure, efficiency and comments User interface Documentation: 1. Pseudocode of requirement 5 2. Derivation of pseudocode of user-defined functions Marks 40% 20% 10% 10% 20%
C. Programming Requirements:
This is an individual assignment which requires you to design and implement a C program exactly according to the following requirements. Requirement 1: Print Programmer Information Your C program should first print your name, student number and the C compiler used. Requirement 2: Extract Integers Extract the 5th to 6th characters of your student number to produce an integer. Do the same with the 7th to 8th characters. Ignore the leading zeros. Examples: Student No. 11025739A 10240906A 11230003A Integers 57, 39 9, 6 0, 3
Requirement 3: Check Whether the Integers are Even or Not For each of the two integers, check whether it is even or not. Print out the integer. If the integer is even, say that it is an even number, otherwise say that it is an odd number. CC2005 (11/12) Assignment 2 Page 1 of 7
CC2005 Applied Computing Requirement 4: Calculate the Sum Interval of Two Integers Calculate and print out the sum interval of the two integers. The sum interval is the sum of all the integers between the two integers inclusively. For example, the sum interval of 9 and 6 is 30 (i.e. 9 + 8 + 7 + 6 = 30), and the sum interval of 0 and 3 is 6 (i.e. 0 + 1 + 2 + 3 = 6). Requirement 5: Ask for Users Input Ask the user whether he/she wants to input the numbers himself/herself. If the answer of the user is affirmative, prompt the user to enter two positive integers with value less than 1000. If a user enter an integer that is less than zero or greater than 1000, it should be considered as invalid and he/she should be informed of the error and be prompted to reenter an integer. Given the two valid integers as input, perform the process as described by requirements 3 and 4 above. After this, your program should ask the user whether he/she wants to repeat the process again. If the answer of the user is affirmative, prompt the user to enter two valid positive integers again and this process can go on and on. There should be no preset number of times that a user may repeat the process. The program terminates when it receives appropriate input from the user indicating that he/she wants to stop, and the program informs the user that the program completed. Design Requirement Write three functions with the names sumInterval, isEven, and enterPositiveInteger as part of the program where: sumInterval is a function that calculates the sum interval of two valid integers. This function returns the sum interval as an integer. As an example, note that the following two invocations of the same function should produce the same output value 30. sumInterval( 6, 9 ); sumInterval( 9, 6 ); isEven is a function that checks whether a number is even or not. It will return 1 (or true) when the number is even, and return 0 (or false) when it is odd. enterPositiveInteger is a function that prompts the user to enter a positive integer from the keyboard. If the user enter an invalid integer (e.g. -30, 1050), the function should inform the user that the input is invalid. The function continues to prompt the user to re-enter a value until the user enters a valid integer. The function then returns the integer. sumInterval, isEven, and enterPositiveInteger must be user-defined. You are not to use the corresponding library functions of the C language.
Page 2 of 7
CC2005 Applied Computing Pseudocode 1. Write the pseudocode to illustrate the algorithm for requirement 5 above (Ask for Users Input). As an advice, this part should be done before you even start to write your C source code. (Constraint: use the while pseudocode statement) Reference: appendix A for some examples of pseudocode. Constraint: use the while pseudocode statement. Derive Pseudocode using Top-down, Stepwise Approach 2. Derive the pseudocode of the sumInterval function in a top-down stepwise approach. (Constraint: use the while pseudocode statement) 3. Derive the pseudocode of the enterPositiveInteger function in a top-down stepwise approach. (Constraint: use the while pseudocode statement) Reference: appendix A for some examples of pseudocode. Reference: appendix B for the format of submission for this part. Constraint: use the while pseudocode statement.
} }
E. Sample Output
Figure 2 is a sample program execution output. It is well that you follow the output format.
Page 4 of 7
F. Equipments:
Personal computer with appropriate operating system C program development tool gcc compiler from Cygwin or other C compliers (You must state the compiler used during program execution) C program debugger (Optional)
G. Submission Requirements:
Soft Copies Compress the following three files into one single file using the ZIP format, and upload this file to the Moodle E-Learning system. The name of the ZIP file should conform to the file naming format: A2_xxxxxxxxx.ZIP, where xxxxxxxxx is your student number. 1. 2. 3. The C source code file (file naming format: A2_xxxxxxxxx.c) The compiled executable file (file naming format: A2_xxxxxxxxx.exe) The file in MS Word format (file naming format: A2_xxxxxxxxx.doc) that contains the following sections: i. The name of the compiler you used for this assignment e.g. GCC, Visual C++. ii. The pseudocode that illustrates the algorithm for requirement 5 above (Ask for Users Input). iii. The derivation of the pseudocodes for the functions sumInterval and enterPositiveInteger using the top-down, stepwise approach. Document each derivation on a separate of paper. iv. Screen capture of a test run using the user input value of 80 and 3 for your program, and some invalid user input such as -2 and 9999.
(Hint: In MS Windows, screen capture can be performed by pressing the Ctrl-PrtSc or Ctrl-PrintScreen key on the keyboard. The image can then be pasted into your MS Word document) Printed Hard Copies Print a hard copy (stapled in ONE piece) with the following sections, and submit to the assignment box of the subject lecturer. Please use A4 size paper only. i. Cover sheet with your name, student number, and tutorial group. ii. C source code. iii. MS Word document of part 3 above. All submitted copies (both softcopies and hardcopies) should contain: Your name Student ID Class and tutorial group
Page 5 of 7
printf(%d\n, result); if ( paid == 1 ) if ( fat == 0 ) if else printf(Enter an integer: ); scanf(\n%d, &number); ask whether the user wants to continue printf(Want to continue? (y/n) ); of not? scanf(\n%c, &again); display want to continue? (y/n). printf(Want to continue? (y/n) ); wait for user input. scanf(\n%c, &again); wait for user input. scanf(); integer sum_up: integer x, integer y User-defined function declaration: int sum_up(int x, int y) { } real getRealNumber: User-defined function declaration: float getRealNumber() { } get a real number. User-defined function invocation: getRealNumber(); character getSomething: User-defined function declaration: char getSomething() { } sum up 3 and 4, and assign the result to User-defined function invocation: a. a = sum_up(3, 4); check the schedule. User-defined function invocation: check_the_schedule(); if d is a valid date User-defined function invocation: if (isValidDate(d)) check whether d is a valid date or not, v = isValidDate(d); and assign the result to v. step the index i from 1 to 10 with an for ( i = 1; i <10; i++ ); increment of 1. step the index i from 0 to 8 with an for ( i = 0; i < 9; i = i + 2) printf(%d\n, i) increment of 2. At each step, display i. integer main: int main () if n is a prime number If (isPrime(n)) display n is a prime number printf("%d is a prime number\n", n); otherwise else display n is not a prime number. printf("%d is not a prime number\n", n); increment m by 1. m = m + 1; or m++; increment n by 2. n = n + 2; increment p by q. p = p + q; calculate the after-deduction take home take_home_pay = gross_salary pay. medical_deductions retirement_deduction; CC2005 (11/12) Assignment 2 Page 6 of 7
CC2005 Applied Computing convert the ascii string s to integer, and assign the result to n. return result. report that the operation is successful. let i be the next integer. let i be the first number. n = atoi(s); return result; return 0; i = i + 1; i = first_number;
At this point, all the pseudocodes can be translated to C code directly. The text between parenthesis are pseudocode comments.