Question #113231

Note answer in C language only

Create a struct called Invoice that a hardware store might use to represent an invoice

for an item sold at the store. An Invoice should include four data members—a part

number (type string), a part description (type string), a quantity of the item being

purchased (type int) and a price per item (type float). Your program should initialize the

four data members. In addition, it should calculate the invoice amount (i.e., multiplies the

quantity by the price per item), If the quantity is not positive, it should be set to 0. If the

price per item is not positive, it should be set to 0. Write a test program that demonstrates

struct Invoice’s capabilities.

Note: Perform this work using

1. pointer to 2d arrays and design function for calculating the results by passing array to function by value and by

reference.

Expert's answer

#include <stdio.h>

struct invoice {
    char partNumber[25];
    char partDescription[25];
    int itemQuantity;
    float itemPrice;
};

float invoice_amount(int quantity, float price) {
    if (quantity < 0) { quantity = 0; }
    return quantity * price;
}

int main() {
    struct invoice Invoice1 = {"ed34", "Screw Guage", 2, 30};
    struct invoice Invoice2 = {"e322", "Screws", 10, 3};
    printf("Invoice1's initial part number is: %s,"
           "\npart description is: %s, "
           "\nquantity per item is: %d, "
           "\nprice per item is: %f, "
           "\ninvoice amount: %f ",
           Invoice1.partNumber,
           Invoice1.partDescription,
           Invoice1.itemQuantity,
           Invoice1.itemPrice,
           invoice_amount(Invoice1.itemQuantity, Invoice1.itemPrice));

    printf("\n\nInvoice2's initial part number is: %s,"
           "\npart description is: %s, "
           "\nquantity per item is: %d, "
           "\nprice per item is: %f, "
           "\ninvoice amount: %f ",
           Invoice2.partNumber,
           Invoice2.partDescription,
           Invoice2.itemQuantity,
           Invoice2.itemPrice,
           invoice_amount(Invoice2.itemQuantity, Invoice2.itemPrice));
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS