COMP10002 Foundations of Algorithms 2020 SM1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Foundations of Algorithms (COMP10002_2020_SM1)
Question 1 |
8 pts |
A credit card transaction record is represented by a struct type named trans_t defined as follows: #define TANS_ID_LEN 12 #define CARD_ID_LEN 8
typedef struct { int year, month, day; /* the year, month, and day of a transaction */ } date_t;
typedef struct { char id[TANS_ID_LEN+1]; /* transaction ID: 12 alphanumeric characters; no uppercase letters */ char card_id[CARD_ID_LEN+1]; /* card ID: 8 alphanumeric characters; no uppercase letters */ date_t date; /* transaction date */ int amount; /* the amount spent in a transaction: a positive integer */ } trans_t;
Write a function int trans_comp(void *trans1, void *trans2); that compares two transaction records pointed to by trans1 and trans2 using their transaction IDs in alphabetical order: |
Quiz: Foundations of lgorithms (COMP10002_2020_SM1)
If the id of the transaction record pointed to by trans1 is smaller than the id of
the transaction record pointed to by trans2, the function should return -1;
If the id of the transaction record pointed to by trans1 is larger than the id of
the transaction record pointed to by trans2, the function should return 1;
If either trans1 or trans2 is NULL, the function should return 0;
You may assume that the ids of the two transaction records will not be the
same.
You may NOT use any library function in your answer to this question.
For example:
If trans1 points to {"mlgtqk8oo74e", "ceww0p66", {2020, 5, 15}, 90} and
trans2 points to {"u7s604f0u6bz", "xnwxw8tl", {2020, 6, 22}, 50}, the function should return -1;
If trans1 points to {"mlgtqk8oo74e", "ceww0p66", {2020, 5, 15}, 90} and
trans2 points to {"6hjqaydtmrq5", "vb3dtxp0", {2020, 5, 15}, 20}, the function should return 1.
Instructions on how to enter code in the answer box (also applies to the other coding questions):
The following instructions intend to make entering code easier. You may ignore these and simply enter code in the answer box. Code formatting and indentation will not be marked.
Click the "Paragraph" button below and choose the "Preformatted" option. Then, continue with any of the following options:
Option A: Type your answer in the answer box directly, and use whitespaces
for indentation.
Option B: Write code in Grok/jEdit/any other code editor. Paste your answer
back to the answer box when you are done.
The indentation may get lost. No need to worry about it. If you want to fix
the indentation, select all code that you just pasted in the answer box, click the "Paragraph" button and choose "Preformatted" again. If this does not fix it, ignore the indentation and move on to the other questions.
Option C [This option is highly discouraged if you have not tried it out with
our sample quiz. Uploading a photo may cause delays. This will not serve as grounds for special considerations]: Write code on paper, take a photo of your answer, and upload the photo by:
Click the "More options" button below (the rightmost button, with three
vertical dots).
In the expanded menu, click the Images icon.
Quiz: Foundations of Algorithms (COMP10002_2020_SM1)
Choose the "Upload Image" option to upload your answer photo. If the upload is successful, you should be able to see the uploaded photo in the answer box. |
||||
Edit View Insert Format Tools Table 12pt Paragraph |
||||
|
Question 2 |
8 pts |
Consider the same trans_t type definition as in Question 1. Write a function int compute_card_total_amount(trans_t trans[], int n, char *card_id) that takes an array of n (n > 0) trans_t records and a credit card id card_id as the input. The function returns the total amount spent over the credit card with id card_id. Note: If card_id is NULL or cannot be found in trans, the function should return 0. You may NOT use any library function in your answer to this question. For example, if trans has the following 10 records and card_id = "xnwxw8tl", the function should return 10 + 60 + 50 = 120. |
Quiz: Foundations of Algorithms (COMP10002_2020_SM1)
|
|
|||||||||
|
|
2022-05-31