A1 cp264 spring 2020 PDF

Title A1 cp264 spring 2020
Author Ralph Suyat
Course Data structures I
Institution Wilfrid Laurier University
Pages 5
File Size 179.7 KB
File Type PDF
Total Downloads 53
Total Views 140

Summary

Download A1 cp264 spring 2020 PDF


Description

CP264 Spring 2020

A1

A1: C Language Basics General Instructions: 1- Create a C project in eclipse called A1 2- Download A1.zip files and extract it to a folder in your machine. 3- Drag and drop all files (other than this PDF) to the A1 project. 4- Rename: “A1_template.txt” to “A1.c”. 5- Add your credentials on top of the file. 6- Use the file “A1_test.c” to test your solution. Compare your results to those shown in A1_output.txt. 7- Before making your submission, use the validator to verify that all your outputs match the desired output. 8- When you are done, you need to submit only your “A1.c” file. Do not export the project or upload a .zip file. 9- When writing your code, put all your variable declarations at the top of the function. Also, declare proper types for your variables. For example, do not declare a long type if the expected data can fit in short. 10- In your A1.c, you may only include any the following libraries, if needed: , , ,,,

Task 1: Calculate House Cost In order to make a drawing of a town house, an architecture engineer need to have the following dimensions: base_height, base_length, roof_height, door_height and door_length (see the drawing). All dimensions are in meters. Once the drawing is made, it is passed to a structural engineer to calculate the needed materials. For the house base, every m2 of concrete would cost 84.79 CAD, while the cost of every m2 for the roof shingles is 11.91 CAD and the cost of every m 2 of wood for the door is 8.73 CAD.

Qutaiba Albluwi © 2020

CP264 Spring 2020

A1

Implement the function: void calculate_cost(float,float,float,float,float)

Roof height

to calculate the construction cost for the house in CAD. The order of input parameters is the same as the order listed in the first paragraph.

door height

base height

Door length

base length

The function does not make any return values. Follow the output sample for how the output is expected to be formatted.

Task 2: Find Promotion Code: A fitness center decided to issue promotion tickets to its users, based on usage of the facility. Each ticket has a unique ticket number, and potentially a tag indicating VIP membership. Upon renewing the annual membership, a user will present the ticket to the administration office, which processes the request by mapping the ticket into one of six promotion categories: 1- No promotion: If ticket number is in the range [0:20) or (50-100]. 2- Level 1 Promotion: if ticket number is in the range [20-35] 3- Level 2 Promotion: if ticket number is in the range (35-50] Qutaiba Albluwi © 2020

CP264 Spring 2020

A1

4- Silver Promotion: if ticket number is in the range (100-200] without VIP membership. 5- Gold Promotion: if ticket number is in the range (100-200] with VIP membership 6- Special Promotion: if ticket number is in the range (200: -]. Implement the function void find_promotion(short, char). The first input parameter is an integer representing a ticket number, and the second a character to indicate VIP status. A value of ‘Y’ means VIP and ‘N’ means not VIP member. If a negative value is passed as a negative number, of if the VIP tag is any character other than ‘Y’ and ‘N’, the function should print an error message and the promotion code is set to -99.

Task 3: Simple Calculator Program The function int mini_calcuator(short, short, char) is a simulated simple calculator program. The program receives three input parameters. The first two are short integers representing the two operands, while the third parameter is a character representing the operator. The calculator supports the following six operators: • ‘+’ = Addition --> num1 + num2 • ‘-’ = Subtraction --> num1 - num2 • ‘*’ = multiplication --> num1 * num2 • ‘/’ = division --> num1/num2 • ‘%’ = modulus --> num1%num2 • ‘^’ = power --> num1num2 In order to make it easier to integrate this calculator in chips programmed through assembly language, the code should be written using simple conditional statements Qutaiba Albluwi © 2020

CP264 Spring 2020

A1

which are ONLY if and goto statements. Use of else, else if, ternary operator or switch statements is not allowed. The calculator has the following four limitations: 1- All operations are performed as integer operations. The return value is of type integer. 2- The calculator does not have any error message printing capability. Therefore, for the illegal operations (e.g. divide by zero) the calculator simply returns a special value which -99. The same applies if an illegal operator is passed to the function. 3- The calculator does not support the modulus operator on negative numbers. If either operands, or both, is a negative number, the calculator performs division instead of modulus. 4- The calculator supports the power operation only if the second operand is positive or equal to 0. If it is negative, then the function returns -99.

Task 4: Enhanced Calculator The calculator developed in Task 3 has several limitations. One of the issues is that there is no way to know if -99 was an actual result or an error code. Another issue is that the function does not produce decimal values, as all operations are integer operations. More serious, there is a chance that an overflow takes place without being detected. For instance, if you raise 500 to power 100, the function will give a garbage data, because the result can not fit in an integer. To fix the above three issues, implement the function: void

mini_calcuator2(short, short, char); Qutaiba Albluwi © 2020

CP264 Spring 2020

A1

Observe the following in your implementation: 1- The calculator supports the same operations in Task 3, i.e. addition, subtraction, multiplication, division, modulus and power. 2- Instead of returning the result, the function prints the result to the console, as shown in the output file. 3- If an illegal operation takes place, the function prints ERROR. This only happens when dividing or modulus over 0, or when an undefined operator is passed to the function. 4- If an overflow is detected the function prints OVERFLOW. An overflow takes place when the result can not fit in an integer. However, since platforms differ on its C implementation, make sure to use the sizeof operator along with the maximum and minimum values defined under the library. In other words, your function should detect an overflow in any machine, even though it might not have generated an overflow in your machine. The sample output provided was produced in a machine that implements a size of integer as 4 bytes. 5- The modulus operation over negative number is supported. 6- The power operation over non-positive number is supported. In case the second operand is negative then the output will be a float rounded to four decimal places. 7- In your implementation, you can use any branching command like if-else, switch-case, goto, and ternary operator. Indeed, you might find it easier to rewrite the entire function using if-else or switch-case.

Qutaiba Albluwi © 2020...


Similar Free PDFs