Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Give a function definition corresponding to the following function declaration. (The type ShoeType is given in Self-Test Exercise \(2 .)\) ShoeType discount(ShoeType old_record); //Returns a structure that is the same as its argument, //but with the price reduced by \(10 \%\).

Short Answer

Expert verified
Question: Write a function called "discount" that takes a parameter of the type ShoeType and returns a new ShoeType structure with the price reduced by 10%. Answer: To create a "discount" function, you would first define the function with the parameter of the type ShoeType called old_record. Create a new variable inside the function called new_record of type ShoeType and initialize it with the values of old_record. Then, reduce the 'price' member of new_record by 10% and return the modified new_record structure. The resulting function definition would be: ShoeType discount(ShoeType old_record) { ShoeType new_record = old_record; new_record.price *= 0.9; // Reduce price by 10% return new_record; }

Step by step solution

01

Recalling the ShoeType structure

Recall the structure definition for ShoeType from Self-Test Exercise 2. It should include a member variable representing the price (let's assume it is called 'price') so that we can modify it in the discount function.
02

Defining the function

First, write the function definition for `discount`. The function must take a parameter of the type`ShoeType` called `old_record`. ShoeType discount(ShoeType old_record) { }
03

Create a copy of the input

Inside the function, create a new variable called `new_record` of type `ShoeType` and initialize it with the values of `old_record`. This will provide us with a new structure that we can modify without affecting the original input. ShoeType discount(ShoeType old_record) { ShoeType new_record = old_record; }
04

Apply the discount

Calculate the discount by reducing the 'price' member of `new_record` by 10%. ShoeType discount(ShoeType old_record) { ShoeType new_record = old_record; new_record.price *= 0.9; // Reduce price by 10% }
05

Return the modified structure

Return the modified `new_record` structure. ShoeType discount(ShoeType old_record) { ShoeType new_record = old_record; new_record.price *= 0.9; // Reduce price by 10% return new_record; }

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

C++ Programming
C++ is a high-level programming language that has been a foundational tool for creating complex software systems. It extends the capabilities of the C programming language with features such as object-oriented programming, exceptions, and templates. In learning C++, understanding how to define and use functions is essential. A function in C++ is a self-contained block of code that encapsulates a specific task or related group of tasks. Typically, a function has a unique name, a return type, parameters, and a body where the actual code resides.

When you define a function, you specify what it will do with its input (if it takes any) and what it will return upon completion. Functions help in breaking down complex problems into smaller, more manageable parts, promoting code reusability and improving the structure of the program.
Structures in C++
In C++, structures (structs) are user-defined data types that allow the grouping of variables of different types under a single name. This makes it easy to handle complex data that has multiple attributes. For instance, a structure named ShoeType could represent a shoe with attributes for size, color, and price. Structures are defined using the struct keyword followed by a block of code that specifies the member variables.

Example Structure Definition:

struct ShoeType {    double price;    std::string color;    int size;};
Structures can be passed to functions, allowing operations to be performed on all the contained data at once. This is beneficial when working with records or complex data entities, as it keeps the data organized and manageable.
Function Parameters
Function parameters are the variables used by a function to receive data from the calling part of the program. When defining a function, you list the parameters that it will take as inputs in the parentheses following the function name. These parameters can be of any data type, including basic types like int and double, as well as complex types like structures and classes.

Parameters can be passed to functions by value, which creates a copy of the argument and preserves the original, or by reference/pointer, which allows the function to modify the original data. The discount example provided demonstrates a by-value parameter, where ShoeType old_record is passed to the function. This ensures the function works on a copy, thus leaving the original object unchanged.
Data Structures
Data structures in programming are a way of organizing and managing data efficiently. They include arrays, lists, stacks, queues, trees, and graphs. In C++, data structures can be custom-built using arrays, pointers, and structures, or they can be used from the Standard Template Library (STL) which provides ready-made classes for frequently used data structures like std::vector, std::map, and std::set.

A good grasp of data structures can significantly improve the efficiency of your programs, both in terms of speed and memory usage. In the context of our function example, structures are a fundamental data structure allowing you to keep related data bundled together and manipulate it as a single entity. Understanding how they work and interact with functions is critical in optimizing data handling in C++.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Define a function called send_line that takes one argument that is an output stream. When called, send_line reads one line of input from the keyboard and outputs the line to the output stream given as its argument. You should be able to call your function using either cout or an outputfile stream as the argument to your function send_line. (If the argument is an output-file stream, then the stream is connected to a file before the function is called, so send_line will not open or close any files.) For example, the first of the following calls to send_line copies a line from the keyboard to the file morestuf.dat, and the second copies a line from the keyboard to the screen: ofstream fout; fout.open("morestuf.dat") cout \(<<\) "Enter 2 lines of input: \(\backslash n "\) send_line(fout); send_line(cout)

Suppose your program contains the following class definition (along with definitions of the member functions): class Yourclass \\{ public: YourClass(int new_info, char more_new_info); Yourclass(); void do_stuff( \()\) private: int information; char more_information; \\} Which of the following are legal? YourClass an_object(42, 'A'); YourClass another_object; YourClass yet_another_object() \\[ \begin{array}{l} a n_{-} \text {object }=\text { Yourclass }\left(99, \quad^{\prime} B^{\prime}\right) ; \\ a n_{-} \text {object }=\text { Yourclass }() ; \\ \text { an_object }=\text { YourClass } \end{array} \\]

Give a definition for the function with the following function declaration. The class BankAccount is defined in Display 10.5 BankAccount new_account(BankAccount old_account); //Precondition: old_account has previously been given a value // (that is, its member variables have been given values). //Returns the value for a new account that has a balance of zero / / and the same interest rate as the old_account. For example, after this function is defined, a program could contain the following: BankAccount account3, account4; account3.set(999, 99, 5.5) ; account4 \(=\) new_account(account 3) account4.output(cout); This would produce the following output:

Here is an initialization of a structure type. Tell what happens with each initialization. Note any problems with these initializations. struct Date \\{ int month; int day; int year; \\}; a. Date due_date \(=\\{12,21\\}\) b. Date due_date \(=\\{12,21,2022\\}\) c. Date due_date \(=\\{12,21,20,22\\}\) d. Date due_date \(=\\{12,21,22\\}\)

What is the type of the stream cin? What is the type of the stream cout?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free