Structure chart with parameter passing for the following program #include #include #include //structure typedef struct { char Username[20]; int cents; }value; 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; } value arr[10]; int i, j, n, option, totalCoins; char name[20]; int *coins; // = //read the file for(i=0; !feof(f) ; i++) { fscanf(f, "%s", arr[i].Username); 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", &option); //switch statement switch(option) { case 1: //read the name scanf("%s", name); totalCoins = 0; //search in the list for(i=0; i 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; }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 8PP: (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays...
icon
Related questions
Question

Structure chart with parameter passing for the following program

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

//structure
typedef struct
{
char Username[20];
int cents;
}value;

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;
}

value arr[10];
int i, j, n, option, totalCoins;
char name[20];
int *coins; // =

//read the file
for(i=0; !feof(f) ; i++)
{
fscanf(f, "%s", arr[i].Username);
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", &option);

//switch statement
switch(option)
{
case 1:
//read the name
scanf("%s", name);
totalCoins = 0;
//search in the list
for(i=0; i<n; i++)
{
if(!strcmp(name, arr[i].Username))
{
totalCoins = totalCoins + arr[i].cents;
}
}
//check if not found
if(totalCoins==0)
printf("Not found\n\n");
else
{
//get the change
coins = coinChange(totalCoins);
//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++)
{
totalCoins = arr[i].cents;
strcpy(name, arr[i].Username);
for(j=i+1; j<n; j++)
{
if(!strcmp(name, arr[j].Username))
{
totalCoins = totalCoins + arr[j].cents;
arr[j].cents = 0;
}
}
//check if found
if(totalCoins!=0)
{
//get change
coins = coinChange(totalCoins);
//print to the csv file
fprintf(f, "%s,%d,%d,%d,%d,%d\n", arr[i].Username, totalCoins, 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;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr