Write a C program Turn in the source code of the program that is the solution to
ID: 3567695 • Letter: W
Question
Write a C program Turn in the source code of the program that is the solution to Problem 6.
1. Write a program to simulate deposit/withdraw activities on a banking account: Initialize the
beginning balance to 1 million, withdraw 600 thousands, and then deposit 500 thousands.
Finally print out the ending balance. What is the output?
2. Write two functions, one for withdraw, the other for deposit. Declare the balance as a global
variable. Both withdraw and deposit functions have one parameter, which represent the
amount to withdraw or deposit. Then call the two functions from main().
3. Modify the withdraw and the deposit functions to make them deduct the balance and add to
the balance one dollar at a time, respectively. Therefore, to withdraw 600 thousands, for
example, the withdraw function has to execute a loop with 600 thousand iterations. The
deposit function works in the same way, in the reverse direction.
4. Create two Posix threads in main(), which call the withdraw and the deposit function
respectively. You can create these two threads in any order. However, before you create the
second thread, use pthread_join() to wait for the first thread to terminate. What is the ending
balance?
5. Move the calls to pthread_join() function after the creation of both pthreads. Run the program
several times. Do you see different result?
6. Use pthread-mutex_lock() and pthread-mutex_unlock() functions to ensure mutual exclusion
between the two pthreads. Is the ending balance right now?
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
struct transaction
{
char time[256];
int credit, debit, balance;
};
void calcTime(char *str)
{
time_t ts;
ts = time(NULL);
strcpy(str, ctime(&ts));
str[strlen(str) - 1] = '';
return;
}
int main()
{
struct transaction obj[100000];
int count = 0, amount, bal = 0, j, ch;
char account[100];
printf("Enter your Account Number:");
fgets(account, 100000, stdin);
account[strlen(account) - 1] = '';
if (strcmp(account, "110011") !=0) {
printf("%s does not exists!! ", account);
return 0;
}
memset(obj, 0, sizeof(obj));
while (1) {
printf("1. Deposit 2. Withdraw 3. Account Summary 4. Exit ");
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the amount you want to deposit:");
scanf("%d", &amount);
obj[count].credit = amount;
bal = bal + amount;
obj[count].balance = bal;
calcTime(obj[count].time);
count++;
break;
case 2:
printf("Enter the amount to withdraw:");
scanf("%d", &amount);
if ( bal - amount < 0) {
printf(" Can't withdraw ");
break;
}
obj[count].debit = amount;
bal = bal - amount;
obj[count].balance = bal;
calcTime(obj[count].time);
count++;
break;
case 3:
printf("Transaction Time Deposit "
"WithDraw Balance ");
for (j = 0; j < count; j++)
{
printf("%s %10d%10d%10d ", obj[j].time,
obj[j].credit, obj[j].debit, obj[j].balance);
}
break;
case 4:
exit(0);
default:
printf("You have entered wrong option!! ");
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.