Question

In: Computer Science

#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<conio.h>
struct Bank_Account_Holder
{
int account_no;
char name[80];
int balance;
};
int n;
void accept(struct Bank_Account_Holder[], int);
void display(struct Bank_Account_Holder[], int);
void save(struct Bank_Account_Holder[], int);
void load(struct Bank_Account_Holder[], int);
int search(struct Bank_Account_Holder[], int, int);
void deposit(struct Bank_Account_Holder[], int, int, int);
void withdraw(struct Bank_Account_Holder[], int, int, int);
int lowBalenquiry(int,int);

void main(void)
{
clrscr();
struct Bank_Account_Holder data[20];
int choice, account_no, amount, index;

printf("NHU Banking System\n\n");
printf("Enter the count of records: ");
scanf("%d", &n);
accept(data, n);
do
{

   printf("\nNHU Banking System Menu :\n");
   printf("Press 1 to display all records.\n");
   printf("Press 2 to search a record.\n");
   printf("Press 3 to deposit amount.\n");
   printf("Press 4 to withdraw amount.\n");
   printf("Press 5 to save all records to file.\n");
   printf("Press 6 to load Records from file.\n");
   printf("Press 0 to exit\n");
   printf("\nEnter choice(0-4) : ");
   scanf("%d", &choice);
   switch (choice)
   {
   case 1:
       display(data, n);
       break;
   case 2:
       printf("Enter account number to search : ");
       scanf("%d", &account_no);
       index = search(data, n, account_no);
       if (index == - 1)
       {
       printf("Record not found : ");
       }
       else
       {
       printf("A/c Number: %d\nName: %s\nBalance: %d\n",
           data[index].account_no, data[index].name,
           data[index].balance);
       }
       break;
   case 3:
       printf("Enter account number : ");
       scanf("%d", &account_no);
       printf("Enter amount to deposit : ");
       scanf("%d", &amount);
       deposit(data, n, account_no, amount);
       break;
   case 4:
       printf("Enter account number : ");
       scanf("%d", &account_no);
       printf("Enter amount to withdraw : ");
       scanf("%d", &amount);
       withdraw(data, n, account_no, amount);
       break;
   case 5:
       save(data, n);
       break;
   case 6:
   load(data, n);
   break;
   default:
   printf("\nWrong choice");
   case 0:
   exit(1);
}
}
while (choice != 0);

getche();
}

void accept(struct Bank_Account_Holder array[80], int s)
{
int i;
for (i = 0; i < s; i++)
{
printf("\nEnter data for Record #%d", i + 1);

printf("\n Enter account_no : ");
scanf("%d", &array[i].account_no);
fflush(stdin);
printf("Enter name of account Holder : ");
gets(array[i].name);
array[i].balance = 0;
}
}

void load(struct Bank_Account_Holder array[80], int s)
{
  
int i;
char fname[20];
printf("\nEnter File with extention for loading::");
scanf("%s",&fname);
FILE * fp;
char dataToBeRead[50];
fp = fopen(fname, "r");
if ( fp == NULL )
{
printf( "\n File Cannot Find" ) ;
}
else
{
  
printf("\n The file is now opened.\n") ;
  
// Read the dataToBeRead from the file
// using fgets() method
while( fgets ( dataToBeRead, 50, fp ) != NULL )
{
char *ptr = strtok(dataToBeRead, ",");
  

array[n].account_no=atoi(ptr);
ptr = strtok(NULL, ",");
//printf("'%s'\n", ptr);
strcpy(array[n].name,ptr);
ptr = strtok(NULL, ",");
//printf("'%s'\n", ptr);
array[n].balance=atoi(ptr);
ptr = strtok(NULL, ",");
n=n+1;

}
  
// Closing the file using fclose()
fclose(fp) ;
  
printf("Data successfully read from file\n");
printf("The file is now closed.") ;
}

}

void display(struct Bank_Account_Holder array[80], int s)
{
int i;

printf("\n\nA/c No\tName\tBalance\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\t%d\n", array[i].account_no, array[i].name,
array[i].balance);
}
}
// save records to file
void save(struct Bank_Account_Holder array[80], int s)
{
int i;
FILE *fp ;
fp = fopen("save.txt", "w");

for (i = 0; i < s; i++)
{
//each line represent single record and field seperated by comma
fprintf(fp, "%d,%s,%d\n", array[i].account_no, array[i].name,
array[i].balance);
}
fclose(fp);
printf("\nSaved Sucessfully to file");
}

int search(struct Bank_Account_Holder array[80], int s, int number)
{
int i;

for (i = 0; i < s; i++)
{
if (array[i].account_no == number)
{
return i;
}
}
return - 1;
}

void deposit(struct Bank_Account_Holder array[], int s, int number, int amt)
{
int i = search(array, s, number);
if (i == - 1)
{
printf("Record not found");
}
else
{
array[i].balance += amt;
}
}

void withdraw(struct Bank_Account_Holder array[], int s, int number, int amt)
{
int i = search(array, s, number);
if (i == - 1)
{
printf("Record not found\n");
}
else if (lowBalenquiry(array[i].balance,amt))
{
printf("Insufficient balance\n");
}
else
{
array[i].balance -= amt;
}
}
int lowBalenquiry(int bal,int amt){
if(bal < amt)
return 1;
return 0;
}

Create a report of this program
and Explain each section of code ( I need this for semester project report file )

Solutions

Expert Solution

Analysis: This program needs to be corrected first because this program code contains multiple errors. And without rectifying the errors this program cannot be executed successfully.

Test Result:
main.c: In function ‘main’:
main.c:100:1: warning: implicit declaration of function ‘getche’ [-Wimplicit-function-declaration]
getche();

^~~~~~
main.c: In function ‘accept’:
main.c:114:1: warning: ‘gets’ is deprecated [-Wdeprecated-declarations]
gets(array[i].name);

^~~~
In file included from main.c:9:0:
/usr/include/stdio.h:638:14: note: declared here
extern char *gets (char *__s) __wur __attribute_deprecated__;

^~~~
main.c: In function ‘load’:
main.c:125:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
scanf("%s",&fname);

^~~~
/tmp/cc1vv8XU.o: In function `accept':
main.c:(.text+0x40f): warning: the `gets' function is dangerous and should not be used.
/tmp/cc1vv8XU.o: In function `main':
main.c:(.text+0xc): undefined reference to `clrscr'
main.c:(.text+0x347): undefined reference to `getche'
collect2: error: ld returned 1 exit status

Conclusion: This program code can be used only after rectifying the proposed corrections.


Related Solutions

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct node* insert(struct node* list,int d ); struct node* del(struct node* list,int d ); void print( struct node *list); void freeList(struct node* list); void copy ( struct node *q, struct node **s ); int main( ) {     int number = 0, choice = 0;     struct node *pList=NULL;     struct node *nList = NULL;         while(choice!= 4)     {                 printf("Do you want to (1)insert, (2)delete, (3)Copy (4)quit.\n");...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT