Starting from:

$30

Lab 1: Using C for Calculations

APS105 Page 1 of 3
APS 105 — Computer Fundamentals

 

Lab 1: Using C for Calculations
In this laboratory, you will be writing some C programs to perform a few calculations with
variables and arithmetic operators. You must use scanf and printf for input and output.
Your solution will be marked by your TA during your scheduled lab period. Your TA will
mark your solution based on its style and your answers to a few questions.
Preparation
This lab has two parts. Read through this entire document carefully and do the work to
create the programs that are described in Part 1 and 2 below and try to make them work.
You can do this on your own home computer if you have succeeded in downloading and
getting Codelite to work, as described in Lab 0. You can also do this work in the ECF labs
image prior to your lab period.
In the sample output examples that follow, the text that would be entered by the program
user is displayed using a bold font. The text <enter> stands for the user pressing the
enter key on the keyboard. On some keyboards, the enter key may be labeled return.
When you execute your programs, the user’s text will not be displayed in bold and <enter>
will not be shown. Throughout this lab, there is a single space after the colon (:) in each
output line.
Part 1 — iBell, Mastercard, and Apple Pay
iBell, a fictional cellular service provider, is currently running a back to school promotion
advertised as “No-contract service for 3 months and get one month free”. The fine print
reads that one month within each 4-month period is free. This means that there is no
discount for a customer that uses the service for 1 to 3 months; however, a 4-month
service is charged for only 3 months. For service periods of more than 4 months, the same
rule applies to each 4-month period when the service is active. For example, if you use
iBell’s service for 11 months, you will be charged for 9 months. This is because it contains
two 4-month periods, plus an extra 3 months; each of these first two 4-month periods is
counted as 3 months, while there is no discount for the last 3-month period.
There are some further complications, unfortunately. Many customers choose to pay for
iBell’s cellular service within its newly launched iPhone app. The app uses the new “Apple Pay” payment solution for customers to pay for the service, which charges the user’s
Mastercard credit card. Mastercard will charge iBell a “swipe fee,” equivalent to a certain
percentage of the total iBell transaction, say, 3%. Apple will charge Mastercard a certain
percentage, say, 25%, of the swipe fee that Mastercard receives from iBell.
Write a C program that calculates the following:
APS105 Page 2 of 3
The iBell amount that a customer should pay after the entire duration of using iBell’s cellular
service. A 13% HST should be applied when calculating the total amount.
Your program reads in the monthly rate (in dollars), the service period (in months), the swipe
and Apple fee. It then prints:
1. the number of free months that the customer receives,
2. the iBell charge (tax inclusive) rounded to two decimal places.
3. The total amount of the swipe fee that Mastercard receives from iBell.
4. The total fee that Apple receives from Mastercard for using Apple Pay.
5. And finally, the total charges
Here are the result of 3 sample runs:
Enter the monthly rate: 34.99<enter>
Enter the service duration (in months): 10<enter>
Enter the swipe fee rate (in percentage): 3<enter>
Enter the Apple Pay rate (in percentage): 25<enter>
Your total free months(s) using iBell’s service is: 2
The iBill charge including taxes is: 316.31
The swipe fee paid to Mastercard is: 9.49
The fee paid to use Apple Pay is: 2.37
The total charge is: 328.17
__
Enter the monthly rate: 51.88
Enter the service duration (in months): 17
Enter the swipe fee rate (in percentage): 3
Enter the Apple Pay rate (in percentage): 25
Your total free months(s) using iBell's service is: 4
The iBill charge including taxes is: 762.12
The swipe fee paid to Mastercard is: 22.86
The fee paid to use Apple Pay is: 5.72
The total charge is: 790.70
__
Enter the monthly rate: 73.34
Enter the service duration (in months): 27
Enter the swipe fee rate (in percentage): 0
Enter the Apple Pay rate (in percentage): 25
Your total free months(s) using iBell's service is: 6
The iBill charge including taxes is: 1740.36
The swipe fee paid to Mastercard is: 0.00
The fee paid to use Apple Pay is: 0.00
The total charge is: 1740.36
Note: You can assume that the user enters valid numbers. You should represent the HST
tax rate using a constant.
Test your code for the cases that users do not use credit card and / or Apple Pay.
Your C program must go in a file named Lab1Part1.c.
APS105 Page 3 of 3
Part 2 — Decipher
Like many others, Mr. Baker has way too many credit cards, each with its own 3-digit code
on the back of the card. Since Mr. Baker is forgetful, he chooses to carefully record the 3-
digit codes in the Notes app on his iPhone.
Of course, he is smart enough not to write the real combination, and has devised a simple
coding scheme that translates the 3-digit code into an encrypted one, by swapping the first
and the third digit of the code, and replacing second digit by its 9’s complement. A code of the
form abc is therefore encoded as c(9-b)a. For example, if the real 3-digit code is 250, the
encrypted code will be 042. A nice feature in his coding scheme is that he can apply the same
scheme again on the encrypted code to calculate the real one.
In this part, you will need to write a C program to help Mr. Baker. The program asks the
user to enter an encrypted 3-digit code and then prints the real 3-digit code. Sample output
from an execution of the program appears below:
Enter an encrypted 3-digit code: 250<enter>
The real 3-digit code is: 042
Enter an encrypted 3-digit code: 042<enter>
The real 3-digit code is: 250
You may assume that the entered code is a valid 3-digit integer. When reading the 3-digit
code from user input, you should scan in a single integer using scanf.
Your C program must go in a file named Lab1Part2.c.
Good Luck!

More products