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
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
Music Player – Songs in music player are linked to previous and next song. You can play songs from either starting or ending of the list. Write an algorithm for the above problem and analyse the efficiency of the algorithm.
Program Specification For this assignment you will write a program to help people record the events of their day by supplying prompts and then saving their responses along with the question and the date to a file. Functional Requirements This program must contain the following features: Write a new entry - Show the user a random prompt (from a list that you create), and save their response, the prompt, and the date as an Entry. Display the journal - Iterate through all entries in the journal and display them to the screen. Save the journal to a file - Prompt the user for a filename and then save the current journal (the complete list of entries) to that file location. Load the journal from a file - Prompt the user for a filename and then load the journal (a complete list of entries) from that file. This should replace any entries currently stored the journal. Provide a menu that allows the user choose these options Your list of prompts must contain at least five different prompts.…
0
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
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT