Algorithm for the below program #include #include #include //structure typedef struct { char Username [20]; int cents; } value; int *coin Change (int change); //main function int main () { FILE *f; //open the file f = f open ("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; }
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//structure
typedef struct
{
char Username [20];
int cents;
}
value;
int *coin Change (int change);
//main function
int main ()
{
FILE *f;
//open the file
f = f open ("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;
}
Step by step
Solved in 2 steps