Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a two parter question that revolves aroudn the following stuct in C lang

ID: 3597104 • Letter: T

Question

This is a two parter question that revolves aroudn the following stuct in C language. Thank you for your time. Only require the function.

We use the following struct to represent a bank account:
struct account {
  char name[15]; /* name of account holder */
  int dollars; /* money in the account */
};
1.Write a function named lessThan that takes two read-only pointers to accounts, and returns 1 if the first account has less money than the second, and 0 otherwise.
Standard library string functions may be used, if needed. NOTE: Why do you suppose that we want to pass struct pointers, rather than structs? Why do we want the pointers to refer to read-only (const) data?

2. Write a function named lessThan that takes two read-only pointers to accounts, and returns 1 if the first account is less than the second, and 0 otherwise. If the dollar amount of the first is less, then the account is less. If the dollar amount is equal, then "less than" is determined by the account name.
Standard library string functions may be used, if needed.

Explanation / Answer

Please find my implementation.


struct account {
char name[15]; /* name of account holder */
int dollars; /* money in the account */
};

1)

int lessThan(const strct account *acc1, const strct account *acc2) {

if(acc1->dollars < acc2->dollars)
return 1;
else
return 0;
}

we want the pointers to refer to read-only (const) data otherwise we can change original content of struct in lessThan function.
So, toprevent any such kind of modification we pass const pointer.

2)

int lessThan(const strct account *acc1, const strct account *acc2) {

if(acc1->dollars < acc2->dollars)
return 1;
else if( strcmp(acc1->name, acc2->name) < 0)
return 1;
else
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote