Data structures and algorithms in C++
Data structures and algorithms in C++
2nd Edition
ISBN: 9780470460443
Author: Goodrich
Publisher: WILEY
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 1, Problem 16R

Explanation of Solution

Program code:

main.cpp

//include the requird header files

#include <vector>

#include "CreditCard.h"

//use the namespace

using namespace std;

//define the function textCard()

void testCard()

{

    // vector of CC pointers

    vector <CreditCard*> wallet(10);

    //create the credit card details

    wallet[0] = new CreditCard(5391037593875309,"JoBlo",2500);

    wallet[1] = new CreditCard(3485039933951954,"JoBlo",3500);

    wallet[2] = new CreditCard(6011490232942994,"JoBlo",5000);

    //iterate a for loop

    for (int i = 0; i < 3; i++)

    {

        //iterate a for loop

        for (int j = 1; j <= 16; j++)

        {

            // explicit cast

            wallet[0] -> chargeIt(double(i));

            // implicit cast

            wallet[1] -> chargeIt(2.0 * i);

            wallet[1] -> chargeIt(2.0 * i);

        }

        //print the card details

           cout << *wallet[i];

        //iterate a while loop to get the balance

        while (wallet[i]->getBalance() > 100.0)

        {

            //call the method makePayment

            wallet[i]->makePayment(100.0);

            //print the new balance

            cout << "New balance = " <<

            wallet[i]->getBalance() << "\n";

        }

        //deleting a walllet

        delete wallet[i];

    }

}

//define the main() method

int main()

{

    //First test case

    CreditCard CC = CreditCard(1234, "Bill gates", 2500);

    CC.chargeIt(1000);

    CC.chargeIt(-100);

    CC.chargeIt(500);

    CC.makePayment(-100);

    CC.makePayment(300);

    cout << CC << endl << endl;

    //given test case

    testCard();

    return EXIT_SUCCESS;

}

Explanation:

In the code,

  • Include the required header files.
  • Use the namespace.
  • Define the “testCard()” method.
    • Create vector pointers
    • Create array “wallet[]” to store credit card details.
    • Iterate a “for” loop.
      • Iterate a “for” loop.
        • Explicit cast.
        • Implicit cast.
      • Print the card details.
      • Iterate a “while” loop to get the balance.
        • Call the method “makepayment()”.
        • Print the new balance.
      • Delete a wallet.
  • Define the “main()” function.
    • Enter the credit card details.
    • Call the function “chargeIt()” and “makePayment()” using “CC”.
    • Call the function “testCard()”.
    • Return “EXIT_SUCCESS”.

CreditCard.cpp

//Code for CreditCard.cpp

#include "CreditCard.h"

using namespace std;

//define the constructor CreditCard

CreditCard::CreditCard(long long int no, const string& nm, int lim, double bal)

{

    //declare the required variables

    number = no;

    name = nm;

    limit = lim;

    balance = bal;

}

//define a function chargeIt()

bool CreditCard::chargeIt(double price)

{

    if(price <= 0)

    {

        //print error message when price is negative

        cout << "Error: Input argument must be positive.\n";

        //return false

        return false;

    }

    //if the condition is true

    if (price + balance > double(limit))

    {

        //return false

        return false;

    }

    //add price to balance

    balance += price;

    //update fraction

    fraction = double(balance)*100.0/limit;

    //if the condition is true

    if(fraction > FRACTION_LIMIT)

    {

//print warning message when fraction is more than 50%

printf("Warning: the current balance on credit card %lld is %.2f%c of your limit.\n", number, fraction, '%');

    }

    // the charge goes through

    return true;

}

//define the function makePayment

void CreditCard::makePayment(double amount)

{

    //create a variable

    const double interestRate = 0.10;

    //if amount less than or equal to 0

    if(amount <= 0)

    {

        //return

        return;

    }

    //set the value of balance

    balance -= amount * (1 + interestRate);

}

ostream& operator<<(ostream& out, const CreditCard& c)

{

    //print method

    out << "Number = " << c.getNumber() << "\n"

    << "Name = " << c.getName() << "\n"

    << "Balance = " << c.getBalance() << "\n"

    << "Limit = " << c.getLimit() << "\n";

    return out;

}

Explanation:

In the code,

  • Include the required header files.
  • Use the namespace.
  • Define the constructor “creditCard()”.
    • Declare the required variables.
  • Define the function “chargeIt()”.
    • If the value of the “price” is less than or equal to 0.
      • Print an error message that the price must be positive.
      • Return a value “false”.
        • If the condition is true,
          • Return false.
        • Add the value of “price” to “balance”.
        • Update the value of “fraction”.
        • If the condition “fraction > FRACTION_LIMIT” is true.
          • Print a warning message.
        • Return true.
  • Define the function “makePayment()”.
    • Create a variable “interestrate”.
    • If the value of the “amount” is less than or equal to 0.
      • Return.
        • Set the value of “balance”.
  • Print the details.
    • Enter the credit card details.
    • Call the function “getNumber()” and “getName()”,“getBalance()” ,and “getLimit()”.
    • Call the function “testCard()”.
    • Return “out”.

CreditCard.h

//Code for CreditCard.h:

#ifndef CreditCard_H

#define CreditCard_H

#include<iostream>

//interest rate

#define INTEREST 5

//fraction limit

#define FRACTION_LIMIT 50.00

//define a class

class CreditCard

{

    //public access specifier

    public:

        //class member functions

        CreditCard(long long int no, const std::string& nm,

        int lim, double bal = 0);

        long long int getNumber() const

        {

            return number;

        }

        std::string getName() const

        {

            return name;

        }

        double getBalance() const

        {

            return balance;

        }

        int getLimit() const

        {

        return limit;

}

// make a charge

bool chargeIt(double price);

// make a payment

void makePayment(double amount);

//private access specifier

private:

//class members

// credit card number

long long int number;

// card owner's name

std::string name;

// the current balance

double balance;

// the credit limit

int limit;

//maintains the fraction

double fraction;

};

// print card information

std::ostream& operator<<(std::ostream& out, const CreditCard& c);

#endif

Explanation:

In the code,

  • Include the required header files...

Blurred answer
Students have asked these similar questions
here is a diagram code : graph LR subgraph Inputs [Inputs] A[Input C (Complete Data)] --> TeacherModel B[Input M (Missing Data)] --> StudentA A --> StudentB end subgraph TeacherModel [Teacher Model (Pretrained)] C[Transformer Encoder T] --> D{Teacher Prediction y_t} C --> E[Internal Features f_t] end subgraph StudentA [Student Model A (Trainable - Handles Missing Input)] F[Transformer Encoder S_A] --> G{Student A Prediction y_s^A} B --> F end subgraph StudentB [Student Model B (Trainable - Handles Missing Labels)] H[Transformer Encoder S_B] --> I{Student B Prediction y_s^B} A --> H end subgraph GroundTruth [Ground Truth RUL (Partial Labels)] J[RUL Labels] end subgraph KnowledgeDistillationA [Knowledge Distillation Block for Student A] K[Prediction Distillation Loss (y_s^A vs y_t)] L[Feature Alignment Loss (f_s^A vs f_t)] D -- Prediction Guidance --> K E -- Feature Guidance --> L G --> K F --> L J -- Supervised Guidance (if available) --> G K…
details explanation and background   We solve this using a Teacher–Student knowledge distillation framework: We train a Teacher model on a clean and complete dataset where both inputs and labels are available. We then use that Teacher to teach two separate Student models:  Student A learns from incomplete input (some sensor values missing). Student B learns from incomplete labels (RUL labels missing for some samples). We use knowledge distillation to guide both students, even when labels are missing. Why We Use Two Students Student A handles Missing Input Features: It receives input with some features masked out. Since it cannot see the full input, we help it by transferring internal features (feature distillation) and predictions from the teacher. Student B handles Missing RUL Labels: It receives full input but does not always have a ground-truth RUL label. We guide it using the predictions of the teacher model (prediction distillation). Using two students allows each to specialize in…
We are doing a custom JSTL custom tag to make display page to access a tag handler.   Write two custom tags: 1) A single tag which prints a number (from 0-99) as words. Ex:    <abc:numAsWords val="32"/>   --> produces: thirty-two   2) A paired tag which puts the body in a DIV with our team colors. Ex:    <abc:teamColors school="gophers" reverse="true">     <p>Big game today</p>     <p>Bring your lucky hat</p>      <-- these will be green text on blue background   </abc:teamColors> Details: The attribute for numAsWords will be just val, from 0 to 99   - spelling, etc... isn't important here. Print "twenty-six" or "Twenty six" ... .  Attributes for teamColors are: school, a "required" string, and reversed, a non-required boolean.   - pick any four schools. I picked gophers, cyclones, hawkeyes and cornhuskers   - each school has two colors. Pick whatever seems best. For oine I picked "cyclones" and       red text on a gold body   - if…

Chapter 1 Solutions

Data structures and algorithms in C++

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY