Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
Question
Book Icon
Chapter 17, Problem 9PC
Program Plan Intro

Rainfall Statistics Modification

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 is empty or not, if the “head” is empty then make a new node into “head” pointer. Otherwise, make a loop to find last node in the loop.
      • Assign the value of “newNode” into the variable “dataPtr”.
  • 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.
    • Check whether the list is empty or not; if the list is empty then 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 largest 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.

main.cpp:

  • Include the required header files into the program.
  • Declare a variable “months” in type of integer.
  • Read the value of “months” 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 “getTotal()”, “getAverage()”, “getLargest()”, “getSmallest()”, “getLargestPosition()”, and “getSmallestPosition()” function and display the values on the screen.

Blurred answer
Students have asked these similar questions
ignore the 4a is actually the previous I done
An array's index type may be any form of data. Do you think this is true or not?
An array definition reserves space for the array. true or false
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,