Turn the code below to a fully modular C code
a main.c file and a functions file and a header file to compile
it
coins.txt file:
john 30
John 45
Alan 30
Terry 15
John 10
Oliver 75
Naomi 60
Jessica 90
David 55
code below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure to store the input data
typedef struct Data
{
// Declare the necessary variables
char name[50];
int change_value;
}Data;
// Define the calculations() function
void calculations(int inputAmount, int change[])
{
for(int x = 0; x < 4; x++)
{
/*Re-Initialize the change[]*/
change[x] = 0;
}
// Loop till the inputAmount is not equal to zero
while(inputAmount != 0)
{
// Check if the amount can be split to 50 cents
if(inputAmount/50 > 0)
{
// Increment the count for 50 cents
change[0]++;
// Assign the remainder to the variable
inputAmount = inputAmount % 50;
}
// Check if the amount can be split to 20 cents
if(inputAmount/20 > 0)
{
// Increment the count for 20 cents
change[1]++;
// Assign the remainder to the variable
inputAmount = inputAmount % 20;
}
// Check if the amount can be split to 10 cents
if(inputAmount/10 > 0)
{
//printf("10 cent %d\n", inputAmount);
// Increment the count for 10 cents
change[2]++;
// Assign the remainder to the variable
inputAmount = inputAmount % 10;
}
// Check if the amount can be split to 5 cents
if(inputAmount/5 > 0)
{
// printf("10 cent %d\n", inputAmount);
// Increment the count for 20 cents
change[3]++;
// Assign the remainder to the variable
inputAmount = inputAmount % 5;
}
}
}
// Define getDataFromFile() function
int getDataFromFile(Data data[])
{
// Declare the necessary variables
char n[50];
int value;
// Initialize a counter variable to zero
int num_entries = 0;
// Declare a FILE pointer
FILE *infile;
// Open the input file
infile = fopen("coins.txt", "r");
// Check if the input file does not exists
if(infile == NULL)
{
// Display error message
printf("ERROR: input file does not exist.");
exit(0);
}
// Loop till the end of the file
while(fscanf(infile, "%s%d", n, &value) != EOF)
{
// Check if the change amount is a multiple of 5
// and is in the range [5, 95]
if((value >= 5 && value <= 95) && (value %
5 == 0))
{
// Copy the data to the array
strcpy(data[num_entries].name, n);
data[num_entries].change_value = value;
// Increment the counter variable by 1
num_entries++;
}
}
// Close the input file
fclose(infile);
// Return the value
return num_entries;
}
// Define the displayCalculatedChange() function
void displayCalculatedChange(char name[], int total_amount, int
found)
{
// Declare an int variable to each cent value
int change[4];
// Check if the name exists
if(found != 0)
{
// Display the name
printf("\nCustomer:\n%s %d cents\n", name, total_amount);
// Call the calculations() function and assign the value
// to the pointer
calculations(total_amount, change);
// Display the change
printf("\nChange:\n");
// Check if the total_amount has a 50 cent change
if(change[0] != 0)
{
// Display the count
printf("50 cents: %d\n", change[0]);
}
// Check if the total_amount has a 20 cent change
if(change[1] != 0)
{
// Display the count
printf("20 cents: %d\n", change[1]);
}
// Check if the total_amount has a 10 cent change
if(change[2] != 0)
{
// Display the count
printf("10 cents: %d\n", change[2]);
}
// Check if the total_amount has a 5 cent change
if(change[3] != 0)
{
// Display the count
printf("5 cents: %d\n", change[3]);
}
}
else
{
// Display name whose details are not present
printf("\nName: %s\n", name);
printf("Not Found\n");
}
// Print blank line
printf("\n");
}
// Define search_name() function
void search_name(char new_name[], Data data[], int size)
{
// Initialize a flage variable to 0
int found = 0;
// Initialize an int variable to 0
int total_amount = 0;
for(int x = 0; x < size; x++)
{
// Check if the name is present in the array
if(strcmp(data[x].name, new_name) == 0)
{
// Calculate the total amount
total_amount += data[x].change_value;
// Assign 1 to the variable
found = 1;
}
}
// Call the displayCalculatedChange() function
displayCalculatedChange(new_name, total_amount, found);
}
void write_data(Data data[], int size)
{
// Declare a FILE pointer
FILE *o_file = fopen("change.csv", "w");
// Declare an int variable to each cent value
int change[4];
fopen("change.csv", "w");
// Check if the input file does not exists
if(o_file == NULL)
{
// Display error message
printf("ERROR");
exit(0);
}
for(int x = 0; x < size; x++)
{
// Call the calculations() function for each entry
calculations(data[x].change_value, change);
// Write the information to the file
fprintf(o_file, "%s, %d, %d, %d, %d, %d\n", data[x].name,
data[x].change_value, change[0], change[1], change[2],
change[3]);
}
// Close the output file
fclose(o_file);
}
// Define the main function
int main()
{
// Create an array to store maximum of 10 input
// values that are present in the file
Data data[10];
// Call the getDataFromFile() function and assign the
// count of input values to the variable
int num_input = getDataFromFile(data);
// Declare an int variable to store the selection from
// the user
int ch;
// Declare an int variable to each cent value
int change[4];
// Declare a charater array to store the name entered
// by the user
char new_name[50];
while(1)
{
// Display the menu options
printf("1. Enter name\n");
printf("2. Exit\n");
// Prompt user ot enter the selection
printf("Selection: ");
// Read user input
scanf("%d", &ch);
switch(ch)
{
case 1:
// Prompt user ot enter name
printf("\nEnter name: ");
scanf("%s", new_name);
// Call the search_name() function
search_name(new_name, data, num_input);
break;
case 2:
// Write the data to a CSV file
write_data(data, num_input);
// Display message
printf("CSV file successfully updated...\n");
return 0;
break;
default:
// Display error message
printf("Invalid choice! Select another option.\n");
break;
}
}
}
Turn the code below to a fully modular C code a main.c file and a functions file and a header file to compile it coins.t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am