Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Assignment Objectives Implement a linear collection of dynamically allocated objects. Be able to implement a linked list using a template class. Understand the Application

Lab Assignment Objectives

  1. Implement a linear collection of dynamically allocated objects.
  2. Be able to implement a linked list using a template class.

Understand the Application

In lab 2 you wrote an interactive program that managed a collection of rainfall statistical information using an array. This week you will modify your lab 2 rainfall statics program to use dynamic memory.

a2.cpp

#include "rain.h" #include #include

using namespace std;

int main() { float arr[12];

//getting the input from user for (int i = 0; i < 12; i++) { cout << "Enter the rainfall(in inches) for month #" << i + 1 << ": "; cin >> arr[i]; // if the input is innvalid then entering again while (arr[i] < 0) { cout << "Rainfall must be 0 or more Please re-enter: "; cin >> arr[i]; } }

cout << "The total rainfall for the year is " << setprecision(2) << fixed << total(arr, 12) << " inches "; cout << "The average for the year is " << setprecision(2) << fixed << avg(arr, 12) << " inches "; cout << "The largest amount of rainfall was " << setprecision(2) << fixed << maximum(arr, 12) << " inches in month " << maxIndex(arr, 12) << endl; cout << "The smallest amount of rainfall was " << setprecision(2) << fixed << minimum(arr, 12) << " inches in month " << minIndex(arr, 12) << endl;

cout << " Here are rainfall amounts, sorted in ascending order: "; cout << "---------------------------------------- "; sortRainfall(arr, 12); for (int i = 0; i < 12; i++) { cout << arr[i] << endl; } cout << "----------------------------------------- ";

return 0; }

********* rain.cpp*************

#include "rain.h" #include #include

using namespace std;

float minimum(float arr[], int n) { float min = INT_MAX;

// getting the minimum value for (int i = 0; i < n; i++) { if (arr[i] < min) min = arr[i]; }

return min; }

float maximum(float arr[], int n) { float max = -1;

// getting the maximum value for (int i = 0; i < n; i++) { if (arr[i] > max) max = arr[i]; }

return max; }

float total(float arr[], int n) { float total = 0;

// adding all the values in the array for (int i = 0; i < n; i++) { total += arr[i]; }

return total; }

float avg(float arr[], int n) { // getting total from total function float ttl = total(arr, n); return ttl / n; }

void sortRainfall(float arr[], int n) { int i, j, min_idx; float temp;

// One by one move boundary of unsorted subarray for (i = 0; i < n - 1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j;

// Swap the found minimum element with the first element temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } }

int maxIndex(float arr[], int n) { float max = -1, index = 0;

for (int i = 0; i < n; i++) { if (arr[i] > max) { max = arr[i]; index = i; } }

return index + 1; }

int minIndex(float arr[], int n) { float min = INT_MAX, index = 0;

for (int i = 0; i < n; i++) { if (arr[i] < min) { min = arr[i]; index = i; } }

return index + 1; }

Your task is to write the implementation methods for the LinkedList template class interface shown below:

template class LinkedList { private: // Declare a structure for the list struct ListNode { T value; struct ListNode *next; }; ListNode *head; // List head pointer public: LinkedList() // Constructor { head = nullptr; } ~LinkedList(); // Destructor void appendNode(T); void insertNode(T); void deleteNode(T); void displayList(); int search(T); // search function T getTotal(); int numNodes(); T getAverage(); T getLargest(); int getLargestPosition(); T getSmallest(); int getSmallestPosition(); }; *********rain.cpp********** #include float avg(float arr[], int n); float minimum(float arr[], int n); float maximum(float arr[], int n); float total(float arr[], int n); void sortRainfall(float arr[], int n); int maxIndex(float arr[], int n); int minIndex(float arr[], int n); Hint:  To compile, use -std=C++11 Example: $ g++ -std=c++11 a5.cpp -o a5 

The Program Specification

First obtain user input for the number of months of data to be entered. Use a linked list instead of an array to hold the monthly data.

Based on the user input data generate the following statistical report values over the runtime period specified by the user: total amount of rainfall, average amount of rainfall, the largest recorded monthly rainfall, the smallest recorded monthly rainfall.

Generate a report summary to display your monthly rainfall statistical findings.

Testing Specification

Input Errors

  • The number of months needs to be > 0.
  • Negative input figures are not to be accepted for rainfall figures.

Pigeonhole the user to obtain valid input data.

Test Run Requirements

Provide a commented out copy of your program test run validation.

What to Turn In

Hand in 2 files: No zip files.

  • LinkedList.h : template class definition
  • a5cpp : test driver file

Example Test run

/* How many months will you enter? -1 Enter at least 1 for the number of months: 0 Enter at least 1 for the number of months: 18 Enter the rainfall (in inches) for month #1: 1 Enter the rainfall (in inches) for month #2: 2 Enter the rainfall (in inches) for month #3: 2 Enter the rainfall (in inches) for month #4: 1 Enter the rainfall (in inches) for month #5: 3 Enter the rainfall (in inches) for month #6: 2 Enter the rainfall (in inches) for month #7: 1 Enter the rainfall (in inches) for month #8: 4 Enter the rainfall (in inches) for month #9: 3 Enter the rainfall (in inches) for month #10: 2 Enter the rainfall (in inches) for month #11: 1 Enter the rainfall (in inches) for month #12: 1 Enter the rainfall (in inches) for month #13: 0 Enter the rainfall (in inches) for month #14: 3 Enter the rainfall (in inches) for month #15: 2 Enter the rainfall (in inches) for month #16: 1 Enter the rainfall (in inches) for month #17: 2 Enter the rainfall (in inches) for month #18: 1 The total rainfall for the period is 32.00 inches. The average rainfall for the period is 1.78 inches. The largest amount of rainfall was 4.00 inches in month 8. The smallest amount of rainfall was 0.00 inches in month 13. */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago