Objectives
- Standard I/O of C program, command line arguments, formatted input and output
- Primary data types, variables, functions
- Compiling, executing, and testing
- Assignment submission
Preparations;
- It is recommended to use working directory
C:/cp264/assignments/a1
to store programs of a1. Check the submission requirement at the end of this document for required submission program files. - If it is not done yet, follow instructions in the example page/course software to Install and configure utitlity programs and C compilers. We will use VS Code as default IDE. However, you can use any programming IDE or text editor, e.g. Eclipse, notepad++, to write assignment programs.
Marking
This assignment has a total of 30 points. Question points are shown in line of question tiles. Question will be marked by the number of successful tests, point grade will be calculated by successful-tests/question-tests*quesiton-points.
Char type and operations [10 points]
Write C programs mychar.h
and mychar.c
, containing the headers and implementations of the following functions. Do not use other library functions and macros.
/**
* Determine the type of a char character.
*
* @param c - char type
* @return - 0 if c is a digit
1 if c is an arithmetic operator
2 if c is the left parenthsis (
3 if c is the right parenthsis )
4 if c is an English letter;
otherwise -1.
*/
int mytype(char c);
/**
* Flip the case of an English character.
*
* @param c - char type
* @return - c's upper/lower case letter if c is a lower/upper case English letter.
*/
char case_flip(char c)
/**
* Convert digit character to the corresponding integer value.
*
* @param c - char type value
* @return - its corresponding integer value if c is a digit character;
* otherwise -1.
*/
int digit_to_int(char c)
Test your programs using provided public test main program mychar_ptest.c and terminal commands shown in the following public test. The screen output should be like as shown after the test run command.
Public test
compile command: gcc mychar.c mychar_ptest.c -o q1 test run command: q1 ------------------ Test: mychar Char ASCII MyType 2 50 0 digit_char_to_int: 2 Char ASCII MyType 8 56 0 digit_char_to_int: 8 Char ASCII MyType A 65 4 caseflip: a Char ASCII MyType a 97 4 caseflip: A Char ASCII MyType z 122 4 caseflip: Z Char ASCII MyType Z 90 4 caseflip: z Char ASCII MyType + 43 1 operator Char ASCII MyType - 45 1 operator Char ASCII MyType ( 40 2 left parenthesis: ( Char ASCII MyType ) 41 3 right parenthesis: ) Char ASCII MyType $ 36 -1 Not typed
Simple power sum [10 points]
Write C programs powersum.h
and powersum.c
, containing the header and implementation of the following functions. Where for positive integers b and n, int mypower(int b, int n) computes and returns bn, int power_overflow(int b, int n) checks if bn is overflow, and int powersum(int b, int n) computes and returns the sum of power series b0+b1+…+bn, and return 0 if overflow happens. Do not use other library functions and macros.
/**
* Depect if overflow in power computing of b to power of n
*
* @param b - the base
* @param n - the exponent
* @return - 1 if overflow happens, 0 otherwise
*/
int power_overflow(int b, int n);
/**
* Compute and return b to power of n.
*
* @param b - the base
* @param n - the exponent
* @return - b to the power of n if no overflow happens, 0 otherwise
*/
int mypower(int b, int n);
/**
* Compute and return the sum of powers.
*
* @param b - the base
* @param n - the exponent
* @return - the sum of powers if no overflow happens, 0 otherwise
*/
int powersum(int b, int n);
Test your programs using provided public test main program powersum_ptest.c and terminal commands shown in the following public test. The screen output should be like as shown after the test run command.
Public test
compile command: gcc powersum.c powersum_ptest.c -o q2 test run command: q2 ------------------ Test: power_overflow(base,exponent) power_overflow(2 10): 0 power_overflow(2 30): 0 power_overflow(2 32): 1 power_overflow(3 10): 0 power_overflow(3 30): 1 power_overflow(3 32): 1 ------------------ Test: mypower(base,exponent) mypower(2,2): 4 mypower(2,4): 16 mypower(2,6): 64 mypower(2,8): 256 mypower(2,10): 1024 mypower(2,30): 1073741824 mypower(2,32): 0 mypower(3,2): 9 mypower(3,4): 81 mypower(3,6): 729 mypower(3,8): 6561 mypower(3,10): 59049 mypower(3,30): 0 mypower(3,32): 0 ------------------ Test: powersum(base,n) powersum(2,1): 3 powersum(2,2): 7 powersum(2,3): 15 powersum(2,4): 31 powersum(2,5): 63 powersum(2,6): 127 powersum(2,7): 255 powersum(2,8): 511 powersum(2,9): 1023 powersum(2,10): 2047 powersum(2,20): 2097151 powersum(2,30): 2147483647 powersum(2,31): 0 powersum(2,32): 0 powersum(3,1): 4 powersum(3,2): 13 powersum(3,3): 40 powersum(3,4): 121 powersum(3,5): 364 powersum(3,6): 1093 powersum(3,7): 3280 powersum(3,8): 9841 powersum(3,9): 29524 powersum(3,10): 88573 powersum(3,20): 0 powersum(3,30): 0 powersum(3,31): 0 powersum(3,32): 0
Simple mortgage calcuation [10 points]
Write a C program mymortgage.h
and mymortgage.c
, containing the headers and implementations of the following functions.
/**
* Compute the monthly payment of given mortgage princile, annual interest rate (%), and mortgage years.
*
* @param principal_amount - float type.
* @param annual_interest_rate - value of parcentage rate, e.g. 5 means 5%, float type.
* @param years - number of mortgage year, int type.
* @return - monthly payment, float type.
*/
float monthly_payment(float principal_amount, float annual_interest_rate, int years)
/**
* Determine the total payment of loan given mortgage princile, annual interest rate (%), and mortgage years.
*
* @param principal_amount - float type.
* @param annual_interest_rate - value of parcentage rate, e.g. 5 means 5%, float type.
* @param years - number of mortgage year, int type.
* @return - the total payment of the loan, float type.
*/
float total_payment(float principal_amount, float annual_interest_rate, int years)
/**
* Determine the total interested payed for the loan given mortgage princile, annual interest rate (%), and mortgage years.
*
* @param principal_amount - float type.
* @param annual_interest_rate - value of parcentage rate, e.g. 5 means 5%, float type.
* @param years - number of mortgage year, int type.
* @return - the total interest payed by the end of paying off the loan, float type.
*/
float total_interest(float principal_amount, float annual_interest_rate, int years)
Test your programs using provided public test main program mymortgage_ptest.c and terminal commands shown in the following public test. The screen output should be like as shown after the test run command.
Public test
compile command: gcc mymortgage.c mymortgage_ptest.c -o q3 test run command: q3 ------------------ Test: monthly_payment(principle,rate,years) monthly_payment(1000.00,1.00,1): 83.78 monthly_payment(10000.00,3.00,10): 96.56 monthly_payment(200000.00,5.00,20): 1319.90 ------------------ Test: total_payment(principle,rate,years) total_payment(1000.00,1.00,1): 1005.35 total_payment(10000.00,3.00,10): 11587.06 total_payment(200000.00,5.00,20): 316776.31 ------------------ Test: total_interest(principle,rate,years) total_interest(1000.00,1.00,1): 5.35 total_interest(10000.00,3.00,10): 1587.06 total_interest(200000.00,5.00,20): 116776.31
- Select and zip required files in the a1 folder to create a zip file, named
a1.zip
. The zip file should only contain the following files.mychar.h mychar.c powersum.h powersum.c mymortgage.h mymortgage.c
- Create submission package by command line operations: open a cmd console or terminal, cd to the a1 directory. On Windows machine, use the following command.
7z a -tzip a1.zip mychar.h mychar.c powersum.h powersum.c mymortgage.h mymortgage.c
On Mac machine, use the following command.
zip a1.zip mychar.h mychar.c powersum.h powersum.c mymortgage.h mymortgage.c
- Submit
a1.zip
to a1 dropbox on MyLS.
End