Page 1 of 1

This question extends assignment 1. There is meant to be code reuse from assignment 1 if your design and code from assig

Posted: Sat Nov 27, 2021 2:40 pm
by answerhappygod
This question extends assignment 1. There is meant to be
code reuse from assignment 1 if your design and code from
assignment 1 adheres to the principles of high cohesion and low
coupling.
You will know if these principles were adhered to if you are
able to reuse most of the modules from assignment 1.
Assignment 2 uses structured design implemented in C. Array of
records (structs) with file I/O is needed.
The program takes two inputs at a time. The name of a person,
and, the coin value as an integer in the range 5 to 95. Input coin
values should always be divisible by 5 (integer division). Names
are one word strings. An example input is:

Jane 30
This input line indicates that 30 cents change is to be given to
Jane.

Output change values need to be in multiples of 50, 20, 10 and 5
cents. The program should aim to give as much of the higher valued
coins as possible. A poor solution for an input of 30 cents is to
give six 5 cent coins. A better solution is to give a 20 cent coin
and a 10 cent coin.

Input to the program comes from a data file
called coins.txt. There can be 0 and up to 10 input
lines like the example above. It is also possible to have the same
name repeated in the data file but the coin values can be
different. When the name is the same, it would mean the same
individual. If the name is the same, your program would need to add
up the coin amounts to obtain a total amount for that individual
before computing the change to be given.

Once your program has read in the data
from coins.txt, your program will
close coins.txt first, and then show a console
screen menu as illustrated below. The program will continue to show
the menu and execute the menu options until "Exit" is selected by
entering the value 2 at the menu prompt.

1. Enter name
2. Exit
When the user enters the value 1 at the menu prompt, your
program will ask for a name. As an example, if the user enters the
name Jane, the program will output:

Customer:
Jane 30 cents

Change:
20 cents: 1
10 cents: 1

Change values of 0 are not shown.
If the user enters a non-existent name (e.g. Donald) at menu
option 1, and therefore would not be in the array of records, your
program will print:
Name: Donald
Not found
After the process output for menu option 1, the menu is
redisplayed.
If the user enters 2 to exit, your program will write the coin and
change data in CSV format to file called change.csv.
After writing the data to the file your program will exit.
In change.csv, the data line for Jane will look like
the following, with each value separated by a comma and the line is
terminated by newline:

Jane,30,0,1,1,0

Each data line in change.csv will be in the
format:
name of person,total coin
value,number of 50 cent
coins, number of 20 cent
coins,number of 10 cent coins,number of 5 cent coins
newline.

So in the example output, Jane has 30 cents in one 20 cent coin and
one 10 cent coin. There are no 50 or 5 cent coins.

The output data file change.csv cannot have
repeated names.

You need to provide a test plan to fully test your algorithm and
program, and provide an input data file, coins.txt,
that contains test data in the specified format for testing your
program.

Your solution (program and algorithm) should be modular in nature.
Use a high cohesion and low coupling design. Your solution (program
and algorithm) should be modular in nature.
This requires the submission of a structure chart and a
high-level algorithm and suitable decompositions of each step.
Note that for this problem, the principle of code reuse is
particularly important and a significant number of marks are
allocated to this. You should attempt to design your solution such
that it consists of a relatively small number of functions that are
as general in design as possible and you should have
functions/subroutines that can be reused (called repeatedly) in
order to solve the majority of the problem. If you find that you
have developed a large number of functions (code
modules/subroutines) where each perform a similar task (or have a
lot of code that is repeated in the functions) then attempt to
analyse your design to generalise the logic so that you have just
one general version of the function (module).
Be mindful of the cohesion exhibited by the function (module).
So if you have a function (module) that is doing more than one
task, then cohesion is low, and, you will need to redesign to have
high cohesion.
Code to reuse:
#include <stdio.h>
#include <stdlib.h>
void calculateChange(int amount){
if(amount/50 > 0){
printf("%d 50 cent
coin.\n",(amount/50));
amount = amount % 50;
}
if(amount/20 > 0){
printf("%d 20 cent
coin.\n",(amount/20));
amount = amount % 20;
}
if(amount/10 > 0){
printf("%d 10 cent
coin.\n",(amount/10));
amount = amount % 10;
}
if(amount/5 > 0){
printf("%d 5 cent
coin.\n",(amount/5));
}
}
int main(){
while (1){
int amt;
printf("Enter amount (5-95) :
");
scanf("%d", &amt);
if (!(amt >=5 && amt <=
95 ) || (amt % 5 != 0)){
printf("\nInvalid
amount must be a multiple of 5 and in range 5-95.");
}
else{
calculateChange(amt);
}
char ch;
printf("\nDo you want to
continue?(Press N/n to exit or Press Y to continue) : ");
scanf("\n%c",&ch);
if(ch=='n' || ch=='N'){
exit(0);
}
}
}