Starting from:

$30

ENSF337-Lab 8 Solved

Assume an engineering firm that works on hydropower plants has asked you to design and write a program in C++ to help them to study the variation of annual water flow in a river of their interest. There should be only one value of flow for each year (no duplications). Here is an example of the format of the records to be used:

Year     Flow (in billions of cubic meters) 
 1961  322.7 

1963   321.5 
our program should be able to calculate the average flow in the list, and also to be able to add new data to the list, or remove data from the list.  An example of the sequence of operations that your program must perform is explained in the rest of this section:

The program starts with displaying a title and brief information about the program. For example:

Program: Flow Studies – Fall 2020 

Version: 1.0 

Lab section: B?? 

Produced by: Your name(s)  

<<< Press Enter to Continue>>>> 

Then it should read its input from a text file called flow.txt (posted on the D2L), and create a linked list of the data (year, flow). You are not allowed to use arrays to store these data.  The program should be written in several modules (a collection of .cpp and .h file, is called a module module)  

First Module: 
The program should have a module that contains a header file called list.h and list.cpp. This module will contain two structures and one class as follows:

1.           Structure called ListItem that maintains an annual flow record (year and flow): 

 struct  ListItem { 

   int year;    double flow; 

2.   Structure called Node that contains the data (an instance of ListItem) and a pointer to Node: struct Node { 

                  ListItem item; 

                  Node *next; 

3.  A class called FlowList that holds and manages a set of nodes that contains ListItems.  This class can be similar to class OLList, in the previous exercise, that keeps the data in each node in order of years. You may need to add more member functions as needed. For this purpose you can also add another data member of type Node*, to be able to set it to different nodes when traversing through the list and have access to the item in the node (this is just a suggestion and you can come up with different solutions as you like).  

Second Module: 
Your program should have a second module that contains two files (hydro.cpp, and hydro.h). Some of the required functions to be defined in this module include:  

•     main that creates and uses the objects of FlowList.  

•     displayHeader - that display the introduction screen:  
Program: Flow Studies – Fall 2020 

Version: 1.0 

Lab section: B?? 

Produced by: Your name  

<<< Press Enter to Continue>>>> 
•     Function readData - that reads records years and flows from input file (flow.txt), inserts them into the list, and returns the number of records in the file.

•     Function menu - that displays a menu and returns the user’s choice (an integer 1 to 5).

•     Function display - that displays years and flows, and shows the average of the flows in the list (calling function averge).

•     Function addData - that prompts the user to enter new data,  inserts the data into the linked list, and updates the number of records.

•     Function removeData – that prompts the user to indicate what year to be removed, removes a single record from the list, and updates the number of records.

•     Function average – that returns the flow average in the given list

•     Function saveData - that opens the flow.txt file for writing and writes the contents of the linked list (annual flow records) into the file.

•     Function pressEnter - that displays <<<Press Enter to Continue>>>, and waits for the user to press the <Return Key> . You can implement this function by printing the message followed by a call to function cin.get() that works similar to functions fgetc() or getc()in C. Here is the statements needed:

 cout << "\n<<< Press Enter to Continue>>>>\n"; cin.get(); 
 Note: These functions are all global functions like main (), not a class member function of a class.

Samples run of the program: 

  To give you a better idea that how the program is supposed to work, here are some screenshots of the sample run of the program. The program starts with displaying a title and brief information about the program. For example:

 Program: Flow Studies, Fall 2020 Version: 1.0 

Lab section: Your lab section 

Produced by: Your name    

<<< Press Enter to Continue>>>> 

  When user presses <Enter>, the program opens an input file called flow.txt. The program should give an error message and terminate, if for some reason it cannot open the file.  

If the file exits it reads the data (years and flows) and insert the data into the linked list in ascending order, based on the flow years.  Then, closes the file, and displays the following menu:  

Please select on the following operations 1. Display flow list, and the average. 

2.  Add data. 

3.  Save data into the file 

4.  Remove data 

5.  Quit 

Enter your choice (1, 2, 3, 4, of 5):   

If user enters 1: the program displays the content of the list on the screen in the ascending order of years. For example:

                Year       Flow (in billions of cubic meters) 

1963           321.5 

1964           222.7 

2002            100.0 

2003            99.5 

The annual average of the flow is: ... billions of cubic meter 
<<< Press Enter to Continue>>>> 
When user presses <Enter> the menu will be displayed again:
 Please select on the following operations 1. Display flow list, and the average 

2.  Add data. 

3.  Save data into the file 

4.  Remove data 

5.  Quit 

Enter your choice (1, 2, 3, 4, of 5):  

 

When user selects 2, the program prompts the user to enter a year and then the flow. For example user can enter the following values (in bold):

 Please enter a year: 1995 

Please enter the flow: 102.99 

 

If data for the same year doesn’t exit, the program should insert the record in proper order (ascending order, based on year) in the list, and display the following message:

New record inserted successfully.  

<<< Press Enter to Continue>>>> 
If the year already exists in the list, the program displays the following message:

Error: duplicate data. 

<<< Press Enter to Continue>>>> 
If user selects 3 from the menu options: the program opens flow.txt again, but this time for writing into the file, by creating an ofstream object. Then, it writes the data (years and flow) into the file, closes the file, and displays the following messages on the screen.


Data are saved into the file. 

       <<< Press Enter to Continue>>>> 
When user selects 4, the program prompts the user to enter the year that should be removed. Here is an example:

Please enter the year that you want to remove: 1995 
  If data exist, the program should remove the record from the list, then displays the following message:

Record was successfully removed.  

<<< Press Enter to Continue>>>> 

(Note: The program at this point doesn’t rewrite the records into the data file) 

If the requested year doesn’t exist in the list, the program displays the following message:

Error: No such a data. 

<<< Press Enter to Continue>>>> 
If user selects 5: the program terminates, showing the following message:

                Program terminated successfully. 
Your main function should test and shows all the functionalities of your program.  As illstrated in the following example a good option would be to use a C++ switch statement.  
int main(void) {   FlowList x; 

  while(1) 

  {     

    switch(menu()){            case 1:  

                     // call display function; 

                       // call pressEnter; 

                     break;                 case 2:  

                     // call addData function 

                     // call pressEnter; 

                     break;                 case 3:  

                     // call saveData function; 

                       // call pressEnter;                        break;                case 4:  

                     // call removeData 

                     // call presenter; 

                     break;                 case 5:  

                     cout << "\nProgram terminated!\n\n";                        quit = 1;                    break;                 default:   

                     cout <<  "\nNot a valid input.\n"; 

                     // pressEnter(); 

  if(quit == 1) break;                                           

More products