Write a program to ask two people for their account balance. Then, based on who
ID: 3723709 • Letter: W
Question
Write a program to ask two people for their account balance. Then, based on who has the most money, all subsequent purchases will be charged to his or her account.
Example
User input is underlined:
Note that there is a tab before "Dinner," "Movie," and "Ice cream." There are spaces after the colons.
Challenge
As a challenge, try to write a function to reduce the value of the debit card according to the cost of the date:
This function will contain the three prompts (Dinner, Movie, and Ice Cream) and reduce the value of pAccount by that amount.
Write the program to store the two account balances. Create a pointer to the account that has the largest balance. Then, for the three item costs on the date, reduce the balance of the appropriate account using the pointer.
Explanation / Answer
#include <stdio.h>
void date(float *pAccount);
float sam_bal, sue_bal,dinner,movie,ice_cream;
int main()
{
float *pAccount;
int input;
do
{
//Get the input as required from the user
printf("Whats Sam's balance?");
scanf("%f",&sam_bal);
printf("Whats Sue's balance?");
scanf("%f",&sue_bal);
printf("Cost of the date; ");
printf(" Dinner:");
scanf(" %f",&dinner);
printf(" Movie:");
scanf(" %f",&movie);
printf(" Ice Cream:");
scanf(" %f",&ice_cream);
//Check for the account which has the largest value using if statement
if (sam_bal>sue_bal)
{
pAccount=&sam_bal;//if sam has the largest balance
date(pAccount); //deduct the amount from the card
sam_bal=*pAccount; //update the account balance
}
else
{
pAccount=&sue_bal;//if sue has the largest balance
date(pAccount);//deduct the amount from the card
sue_bal=*pAccount; //update the account balance
}
//Print the updated values
printf("Sam's balance:%f",sam_bal);
printf("Sue's balance:%f",sue_bal);
//Perform the calculation for another date
printf("Do you want to continue? 1.Yes 2.No:");
scanf("%d",&input);
}while(input==1); //continue based on user input
return 0;
}
//Method to compute the debit card value according to the cost of the date
//pAccount is a pointer to the account which has the largest value
void date(float *pAccount)
{
*pAccount=*pAccount-(dinner+movie+ice_cream);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.