Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
Question
Book Icon
Chapter 18, 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
Task 3: Statistics using arrays: by java programming With the spread of COVID 19, the HR department in a company has decided to conduct some statistics among the employees in order to determine the number of infections according to some conditions. For each employee, they have to record the code, name, age, whether he/she was infected or no and the remaining days of leaves for him/her. You are requested to write the program that maintains the lists of details for the employees as mentioned above using the concept of arrays. The program repeats the display of a menu of services until the user decides to exit. 1. Start by initializing the employee details by reading them from the keyboard. 2. Repeat the display of a menu of 4 services, perform the required task according to the user’s choice and asks the user whether he/she wants to repeat or no. You need to choose one service from each category (‘A’,’B’,’C’,’D’) a. A. Display the total number of employees that were infected b. B.…
Median Function – In statistics, the median of a set of values is the value that lies in the middle when the values are arranged in sorted order.   If the set has an even number of values, the median is the average of the two middle values.  Your program should start with two arrays of integers containing the following values: Even numbered array:  17 32 45 68 99 101 67 89 22 27 Odd numbered array:   17 32 45 68 99 101 67 89 22 Using a sort function of your choice, first sort the arrays.  NOTE:  you may use the Standard Template Library sort function or your own sort function. Then, write a function that determines the median of a sorted array.   The function should take an array of numbers and an integer indicating the size of the array and return the median of the values in the array.   The same function should be called twice – once for the even array and once for the odd array. Your program should also have a printArray function that can be used to print the sorted array. (It…
An array's index type may be any form of data. Do you think this is true or not?
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,