Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education