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

Suppose that you have the following definitions: struct timeType struct tourType { { int hr; string cityName; double min; int distance; int sec; timeType travelTime; }; }; a. Declare the variable destination of type tourType. b. Write C++ statements to store the following data in destination: cityName—Chicago, distance—550 miles, travelTime—9 hours and 30 minutes. c. Write the definition of a function to output the data stored in a variable of type tourType. d. Write the definition of a value-returning function that inputs data into a variable of type tourType. e. Write the definition of void function with a reference parameter of type tourType to input data in a variable of type tourType.

Short Answer

Expert verified
Declare `tourType destination;`. Use assignments to store data. Define functions for output, input, and data entry by reference.

Step by step solution

01

Declare Variable

To declare a variable named `destination` of type `tourType`, write the following statement: ```cpp tourType destination; ```
02

Store Data in 'destination'

To store the specified data in `destination`, assign values to its members: ```cpp destination.cityName = "Chicago"; destination.distance = 550; destination.travelTime.hr = 9; destination.travelTime.min = 30; destination.travelTime.sec = 0; ```
03

Define Output Function

To define a function that outputs the data in a `tourType` variable, create the following function: ```cpp void displayTourInfo(const tourType& tour) { std::cout << "City Name: " << tour.cityName << std::endl; std::cout << "Distance: " << tour.distance << " miles" << std::endl; std::cout << "Travel Time: " << tour.travelTime.hr << " hours, "; std::cout << tour.travelTime.min << " minutes, "; std::cout << tour.travelTime.sec << " seconds" << std::endl; } ```
04

Define Value-Returning Input Function

To define a value-returning function that inputs data into a `tourType` variable, use this function: ```cpp tourType inputTourData() { tourType newTour; std::cout << "Enter city name: "; std::cin >> newTour.cityName; std::cout << "Enter distance: "; std::cin >> newTour.distance; std::cout << "Enter travel time (hours minutes seconds): "; std::cin >> newTour.travelTime.hr >> newTour.travelTime.min >> newTour.travelTime.sec; return newTour; } ```
05

Define Void Function with Reference Parameter

Finally, define a void function that inputs data using a reference parameter for a `tourType` variable: ```cpp void getTourData(tourType& tour) { std::cout << "Enter city name: "; std::cin >> tour.cityName; std::cout << "Enter distance: "; std::cin >> tour.distance; std::cout << "Enter travel time (hours minutes seconds): "; std::cin >> tour.travelTime.hr >> tour.travelTime.min >> tour.travelTime.sec; } ```

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.

Struct Definition
In C++, a `struct` is a user-defined data type that allows us to group variables of different types together. This is similar to a class, but with some differences, such as its default public access. A `struct` definition starts with the keyword `struct` followed by the name of the structure. Inside the curly braces `{}`, you declare the member variables of the structure. For example: - **timeType**: - Contains three member variables: `int hr`, `double min`, and `int sec`. - **tourType**: - Has a string `cityName`, an integer `distance`, and a `timeType` struct called `travelTime` as its members. Structs help in organizing and managing data efficiently. By grouping related data, they make handling different but related pieces of information easier.
Function Definition
Defining a function in C++ involves writing a statement that specifies the function's behavior. A function usually consists of: - **Return Type**: Specifies what type of value the function returns, such as `void` for no return value or any data type like `int`, `double`, etc. - **Function Name**: Used to call the function later in program. - **Parameters**: The data that you may pass into the function. - **Function Body**: The code block between `{}` that executes when the function is called. For instance, `displayTourInfo` is a void function that takes a constant reference to a `tourType` object. Another example is the value-returning function `inputTourData` which returns a `tourType` object after prompting the user for data. Function definitions organize code and help in reusing code blocks.
Variable Declaration
Variable declaration in C++ is the process of telling the compiler about the variable's name and the type of data it will store. This reserves a space in the memory. To declare a variable, you specify the type followed by the variable name. For example: ```cpp tourType destination; ``` This declares a variable named `destination` of the type `tourType`. No specific value is assigned initially; it only determines the kind of data it can hold. Understanding variable declaration is crucial as it is the first step in variable management for any program. It helps in organizing your data storage and manipulation effectively.
Value-Returning Function
In C++, a value-returning function is designed to perform some operation and return a value to the point where it was called. The primary components of such functions include: - **Return Type**: This indicates the type of value that will be returned, such as `int`, `double`, or custom types like `tourType`. - **Return Statement**: Uses the `return` keyword to send back the value. For example, the function `inputTourData` returns an object of type `tourType`. Inside the function, user input is acquired and then encapsulated within a `tourType` object, which is then returned. Value-returning functions are vital for modularizing your code. They allow the reuse of logic, enable the collection of data for further processes, and help reduce redundancy.

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

Consider the following statements: struct nameType struct dateType struct personalInfoType { { { string first; int month; nameType name; string last; int day; int pID; }; int year; dateType dob; }; }; personalInfoType person; personalInfoType classList[100]; nameType student; Mark the following statements as valid or invalid. If a statement is invalid, explain why. a. person.name.first = "William"; b. cout << person.name << endl; c. classList[1] = person; d. classList[20].pID = 000011100; e. person = classList[20]; f. student = person.name; g. cin >> student; h. for (int j = 0; j < 100; j++) classList[j].pID = 00000000; i. classList.dob.day = 1; j. student = name;

Define a struct, checkingAccount, to store the following data about a checking account: account holder's name (string), account number (int), balance (double), and the interest rate (double).

Assume that you have the following definition of a struct. struct partsType { string partName; int partNum; double price; int quantitiesInStock; }; Declare an array, inventory, of 100 components of type partsType.

Mark the following statements as true or false. a. All members of a struct must be of different types. b. \(A\) function cannot return a value of type struct. c. A member of a struct can be another struct. d. The only allowable operations on a struct are assignment and member selection. e. An array can be a member of a struct. f. In \(C++,\) some aggregate operations are allowed on a struct. g. Because a struct has a finite number of components, relational operations are allowed on a struct.

Define a struct, movieType, to store the following data about a movie: movie name (string), movie director (string), producer (string), the year movie was released (int), and number of copies in stock.

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