MyProgrammingLab - For Gaddis: Starting Out with C++ From Control Structures through Objects
MyProgrammingLab - For Gaddis: Starting Out with C++ From Control Structures through Objects
15th Edition
ISBN: 9780133780611
Author: Pearson
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 17, Problem 13PC
Program Plan Intro

Rainfall Statistics Modification #2

Program Plan:

“IntList.h”:

  • Include the required specifications into the program.
  • Define a class template named “IntList”.
    • Declare the member variables “value” and “*next” in structure named “ListNode”.
    • Declare the constructor, copy constructor, destructor, and member functions in the class.
  • Declare a class template and define a function named “appendNode()” to insert the node at end of the list.
    • Declare the structure pointer variables “newNode” and “dataPtr” for the structure named “ListNode”.
    • Assign the value “num” to the variable “newNode” and assign null to the variable “newNode”.
    • Using “if…else” condition check whether the list to be empty or not, if the “head” is empty and make a new node into “head” pointer. Otherwise, make a loop find last node in the loop.
    • Assign the value of “dataPtr” into the variable “newNode”.
  • Declare a class template and define a function named “print()” to print the values in the list.
    • Declare the structure pointer “dataPtr” for the structure named “ListNode”.
    • Initialize the variable “dataPtr” with the “head” pointer.
    • Make a loop “while” to display the values of the list.
  • Declare a class template and define a function named “insertNode()” used to insert a value into the list.
    • Declare the structure pointer variables “newNode”, “dataPtr”, and “prev” for the structure named “ListNode”.
    • Make a “newNode” value into the received variable value “num”.
    • Using “if…else” condition to check whether the list is empty or not.
      • If the list is empty then initialize “head” pointer with the value of “newNode” variable.
      • Otherwise, make a “while” loop to test whether the “num” value is less than the list values or not.
      • Use “if…else” condition to initialize the value into list.
  • Declare a class template and define a function named “deleteNode()”to delete a value from the list.
    • Declare the pointer variables “dataPtr”, and “prev” for the structure named “ListNode”.
    • Using “if…else” condition to check whether the “head” value is equal to “num” or not.
      • Initialize the variable “dataPtr” with the value of the variable “head”.
      • Remove the value using “delete” operator and reassign the “head” value into the “dataPtr”.
      • If the “num” value not equal to the “head” value, then define the “while” loop to assign the “dataPtr” into “prev”.
      • Use “if” condition to delete the “prev” pointer.
  • Declare a class template and define a function named “getTotal()” to calculate total value in a list.
    • Define a variable named “total” and initialize it to “0” in type of template.
    • Define a pointer variable “nodePtr” for the structure “ListNode” and initialize it to be “NULL”.
    • Assign the value of “head” pointer into “nodePtr”.
    • Define a “while” loop to calculate “total” value of the list.
    • Return a value of “total” to the called function.
  • Declare a class template and define a function named “numNodes()” to find the number of values that are presented in the list.
    • Declare a variable named “count” in type of “integer”.
    • Define a pointer variable “nodePtr” and initialize it to be “NULL”.
    • Assign a pointer variable “head” to the “nodePtr”.
    • Define a “while” loop to traverse and count the number of elements in the list.
  • Declare a class template and define a function named “getAverage()”to find an average value of elements that are presented in list.
  • Declare a class template and define a function named “getLargest()”to find largest element in the list.
    • Declare a template variable “largest” and pointer variable “nodePtr” for the structure.
    • Using “if” condition, assign the value of “head” into “largest” variable.
    • Using “while” loop, traverse the list until list will be empty.
      • Using “if” condition, check whether the value of “nodePtr” is greater than the value of “largest” or not.
      • Assign address of “nodePtr” into “nodePtr”.
    • Return a value of “largest” variable to the called function.
  • Declare a class template and define a function named “getSmallest()” to find smallest element in the list.
    • Declare a template variable “smallest” and pointer variable “nodePtr” for the structure.
    • Using “if” condition, assign the value of “head” into “smallest” variable.
    • Using “while” loop, traverse the list until list will be empty.
      • Using “if” condition, check the value of “nodePtr” is smaller than the value of “smallest”.
      • Assign address of “nodePtr” into “nodePtr”.
    • Return a value of “smallest” variable to the called function.
  • Declare a class template and define a function named “getSmallestPosition()” to find the position of smallest value in the list.
    • Declare a template variable “smallest” and pointer variable “nodePtr” for the structure.
    • Using “while” loop traverses the list until the list will be empty.
      • Using “if” condition, find the position of “smallest” value in the list.
    • Return the value of “position” to the called function.
  • Declare a class template and define a function named “getLargestPosition()” to find the position of largest value in the list.
    • Declare a template variable “largest” and pointer variable “nodePtr” for the structure.
    • Using “while” loop traverses the list until the list will be empty.
      • Using “if” condition, find the position of “largest” value in the list.
    • Return the value of “position” to the called function.
  • Define the destructor to destroy the values in the list.
    • Declare the structure pointer variables “dataPtr”, and “nextNode” for the structure named “ListNode”.
    • Initialize the “head” value into the “dataPtr”.
    • Define a “while” loop to make the links of node into “nextNode” and remove the node using “delete” operator.
  • Declare a class template and define a function named “storeToFile()” to save the list values into file.
    • Declare a pointer variable “nodePtr” for a structure “ListNode” and assign it’s to be null.
    • Make a call to create a file object “outFile()” and initialize the value of variable “head” pointer into “nodePtr”.
    • Using “while” loop save the values of list into file and assign the next node into “nodePtr”.
    • Close the file using “close()” method.
  • Declare a class template and define a function named “getFromFile()” to get the data from file into list.
    • Declare a pointer variable “nodePtr” for a structure “ListNode” and assign it’s to be null.
    • Declare a template variable “newValue” and create an object for file.
    • Using “while” loop, get the values from file into list.
    • Close the file using “close()” method.

Program #1 “Main.cpp”:

  • Include the required header files into the program.
  • Declare a variable “n” in type of integer.
  • Read the value of “n” from user and using “while” loop to validate the data entered by user.
  • Declare an object named “rainfall” for the class “IntList”.
  • Using “for” loop, read an input for every month from user.
    • Append the value entered from user into the list.
  • Make a call to the function “storeToFile()” to write the list values into file.

Program #2 “Main.cpp”:

  • Include the required header files into the program.
  • Declare an object named “rainfall” for the class “IntList”.
  • Make a call to the function “getFromFile()” to read a file into list using “rainfall” object.
  • Make a call to “getTotal()”, “getAverage()”, “getLargest()”, “getSmallest()”, “getLargestPosition()”, and “getSmallestPosition()” function and display the values on the screen.

Blurred answer
Students have asked these similar questions
Computer Science In c++ (There should be three different files, "main.cpp", "sourcefile.cpp" and "header.cpp" your solution must have those three different files): Write a function that checks if the nodes in a linked list is sorted in ascending order. The function should return true if the values are sorted, otherwise false. Take user input. Call the funciton in the main function to show it is working.
Topic: Singly Linked ListImplement the following functions in C++ program. Read the question carefully.  (See attached photo for reference)    void isEmpty() This method will return true if the linked list is empty, otherwise return false. void clear() This method will empty your linked list. Effectively, this should and already has been called in your destructor (i.e., the ~LinkedList() method) so that it will deallocate the nodes created first before deallocating the linked list itself.
Functions that look at a linked list but do not modify it are referred to as______________
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