Question

Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed....

Question2 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 use that requires a bit of changing from question 1:

#include

coinChange(int change);
display();

int main(){

   int num = display();

   coinChange(num);

  
   return(0);
}

int coinChange(int change){
   while(change > 0){
       if(change > 0 && change <=95 && change% 5 == 0){
           if(change >= 50){
               change -= 50;
               printf("50 cents ");
           }else if(change >= 20){
               change -= 20;
               printf("20 cents ");
           }else if(change>=10){
              
               change -= 10;
               printf("10 cents ");

           }else if(change>=5){
      
               change -= 5;
               printf("5 cents ");
           }
          
       }
   }
   return change;

}


  


int display(){
   int x = 0;
   printf("Please input your coin amount: ");
   scanf("%d%*c", &x);
   return x;
}

PLEASE be mindful that everything should be in modules(methods,functions) and no global variables if they are not constants including printing out the change

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//structure
typedef struct
{
char name[20];
int cents;
}record;

int *coinChange(int change);

//main function
int main(){

FILE *f;

//open the file
f = fopen("coins.txt", "r");

//check if file not opened
if(f==NULL)
{
printf("File not opened!");
return 1;
}

record arr[10];
int i, j, n, op, total;
char name[20];
int *coins; // =

//read the file
for(i=0; !feof(f) ; i++)
{
fscanf(f, "%s", arr[i].name);
fscanf(f, "%d", &arr[i].cents);
}

n = i;

//close the file
fclose(f);

//loop
while(1)
{
//display menu
printf("1. Enter name\n");
printf("2. Exit\n\n");

//prompt and read option
printf("Enter option (1 or 2): ");
scanf("%d", &op);

//switch statement
switch(op)
{
case 1:
//read the name
scanf("%s", name);
total = 0;
//search in the list
for(i=0; i<n; i++)
{
if(!strcmp(name, arr[i].name))
{
total = total + arr[i].cents;
}
}
//check if nor found
if(total==0)
printf("Not found\n\n");
else
{
//get the change
coins = coinChange(total);
//print the change
if(coins[0])
printf("50 cents: %d\n", coins[0]);
if(coins[1])
printf("20 cents: %d\n", coins[1]);
if(coins[2])
printf("10 cents: %d\n", coins[2]);
if(coins[3])
printf("5 cents: %d\n", coins[3]);
printf("\n\n");
}

break;

case 2:

//create csv file
f = fopen("change.csv", "w");

//search the name in the list
for(i=0; i<n-1; i++)
{
total = arr[i].cents;
strcpy(name, arr[i].name);
for(j=i+1; j<n; j++)
{
if(!strcmp(name, arr[j].name))
{
total = total + arr[j].cents;
arr[j].cents = 0;
}
}
//check if found
if(total!=0)
{
//get change
coins = coinChange(total);
//print to the csv file
fprintf(f, "%s,%d,%d,%d,%d,%d\n", arr[i].name, total, coins[0],
coins[1],coins[2],coins[3]);
}
}

//close the file
fclose(f);
return 0;

default:
printf("Wrong option. Try again\n\n");
}
}

return(0);
}

//function to calculate the change
int* coinChange(int change){
int *coins = (int*)calloc(4, sizeof(int));

while(change > 0){
if(change > 0 && change <=95 && change% 5 == 0){
if(change >= 50){
change -= 50;
coins[0]++;
}else if(change >= 20){
change -= 20;
coins[1]++;
}else if(change>=10){
change -= 10;
coins[2]++;
}else if(change>=5){

change -= 5;
coins[3]++;
}

}
}
return coins;
}

coins.txt

Jane 20
Donald 40
Jane 10
Donald 35

Output:

1. Enter name
2. Exit

Enter option (1 or 2): 1
Jane
20 cents: 1
10 cents: 1


1. Enter name
2. Exit

Enter option (1 or 2): 2

change.csv

Add a comment
Know the answer?
Add Answer to:
Question2 uses structured design implemented in C. Array of records (structs) with file I/O is needed....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • Write a program called CountCoins.java that prompts the user for the input file name (you can...

    Write a program called CountCoins.java that prompts the user for the input file name (you can copy the getInputScanner() method given in the ProcessFile assignment) then reads the file. The file contains a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be “pennies” (1 cent each), “nickels” (5 cents each), “dimes” (10 cents each), or “quarters” (25 cents each), case-insensitively. Add up the cash values of...

  • [C++] Outline: The movie data is read from a file into an array of structs and...

    [C++] Outline: The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.   Requirements: Your...

  • Write a program that tells what coins to give out for as change from 1 cent...

    Write a program that tells what coins to give out for as change from 1 cent to 99 cents. Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies) only. Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program. Solution Demo Hello I am the coin machine! I will give you the least number of coins for your change....

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Requesting help with the following C Program: DESIGN and IMPLEMENT a menu driven program that uses...

    Requesting help with the following C Program: DESIGN and IMPLEMENT a menu driven program that uses the following menu and can perform all menu items: Enter a payroll record for one person Display all paycheck stubs Display total gross payroll from all pay records. Quit program The program will reuse the DATE struct from the previous assignment.  You should also copy in the functions relating to a DATE. The program will create a PAYRECORD struct with the following fields: typedef struct...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Design and implement a C version of the color match program. As a starting point, use...

    Design and implement a C version of the color match program. As a starting point, use the file HW2-1-shell.c. The program should employ a reasonable algorithm that compares all pairings of colors in the palette exactly once. A color should not be compared to itself. Nor should two colors be compared to each other more than once. Your C program should print out the total component color difference for the closest pair using the print string provided in the shell...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT