;;Lab 10: File Access;This lab accompanies Chapter 10 of Starting Out with Programming Logic & Design.;Name;Lab 10.1 ? File Access and Pseudocode;Critical Review;When a program needs to save data for later use, it writes the data in a file. The data can be read from the file at a later time.;Three things must happen in order to work with a file. 1) Open a file. 2) Process the file. 3) Close the file.;An internal file must be created for an output file or input file, such as;Declare OutputFile myFile //to write out;Declare InputFile myFile //to read in;A data file must also be created to store the output, such as;Open myFile ?thedata.txt?;New keywords and syntax include the following;Open [InternalName] [FileName];Write [InternalName] [String or Data];Read [InternalName] [Data];Close [InternalName];AppendMode //used with Open when need to append;Loops are used to process the data in a file. For example;For counter = 1 to 5;Display ?Enter a number:?;Input number;Write myFile number;End For;When reading information from a file and it is unknown how many items there are, use the eof function. For example;While NOT eof(myFile);Read myFile number;Display number;End While;This lab examines how to work with a file by writing pseudocode. Read the following programming problem prior to completing the lab. The following program from Lab 9.1 will be used, with some modifications.;The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and written to a file. Write a loop around the program to run multiple times. The data should be appended to the file to keep track of multiple days. If the user wants to print data from the file, read it in and then display it. Store the pints per hour and the average pints donated in a file called blood.txt.;Step 1: Note that the getPints, getTotal, and getAverage functions do not change. Also note that the references to displayInfo, getHigh, and getLow functions are removed to meet the new requirements. In the pseudocode below, add the following;In the Main Module;A variable named option of the data type Integer;Input option;Write an if statement that will determine which option to run;Call a module called writeToFile that passes pints and averagePints;Call a module called readFromFile that passes pints and averagePints;In the writeToFile Module;Declare an output file called outFile in AppendMode, with the name bloodFile. (Reference: Appending Data to an Existing File, Page 370).;Open the internal file (bloodFile) and a text file named blood.txt. (Reference: Creating a File and Writing Data to it, Page 362.);Write the string ?Pints Each Hour? to the file. (Reference: Writing Data to a File, Page 363).;In the while loop, write each element of the pints array to the bloodFile. (Reference: Using Loops to Process Files, Page 371).;Write the string ?Average Pints? to the file.;Write the value of averagePints to the file.;Close the bloodFile. (Reference: Closing an Output File, Page 363).;In the readFromFile Module;Declare an input file called inFile, with the name bloodFile. (Reference: Reading Data from a File, Page 366).;Open the internal file (bloodFile) and a text file named blood.txt.;Read the string ?Pints Each Hour? in from your file and store into a variable str1. This should be done such as Read bloodFile str1. The string will be stored in the variable str1.;Display str1 to the screen.;Read pints in from the bloodFile and store in the pints array.;Display pints to the screen.;Read the string ?Average Pints? in from your file and store into a variable str2.;Display str2 to the screen.;Read averagePints in from the bloodFile.;Display averagePints to the screen;Close the file. (Reference: Closing an Input File, Page 367).;Module main();//Declare local variables;Declare String again = ?no?;Declare Real pints;Declare Real totalPints;Declare Real averagePints;a.;While again == ?no?;//module calls below;Display ?Enter 1 to enter in new data and store to file?;Display ?Enter 2 to display data from the file?;Input b.;c. If _____________ == _________________ Then;Call getPints(pints);Call getTotal(pints, totalPints);Call getAverage(totalPints, averagePints);d. Call _____________(__________,________);Else;e. Call _____________(__________,________);End If;Display ?Do you want to run again: yes or no?;Input again;End While;End Module;Module getPints(Real pints);Declare Integer counter = 0;For counter = 0 to 6;Display ?Enter pints collected:?;Input pints[counter];End For;End Module;Function getTotal(Real pints, Real totalPints);Declare Integer counter = 0;Set totalPints = 0;For counter = 0 to 6;Set totalPints = totalPints + pints[counter] End For;Return totalPints;Function getAverage(Real totalPints, Real averagePints);averagePints = totalPints / 7;Return averagePints;Module writeToFile(Real pints, Real averagePints);f. Declare;g. Open;h. Write _______________ ?__________________?;Declare Integer counter = 0;i. While counter < 7;Write __________ _________[_________];Set counter = counter + 1;End While;j. Write ____________ ?_____________?;k. Write;l. Close;End Module;Module readFromFile(Real pints, Real averagePints);m. Declare;n. Open __________ ?__________?;o. Read;p. Display;q. Read;r. Display;s. Read;t. Display;u. Read;v. Display;w. Close;End Module;Lab 10.2 ? File Access and Flowcharts;Critical Review;Outputting to a File using Raptor;The Output symbol is used to output data to a text file. When an Output symbol is reached during Raptor program execution, the system determines whether or not output has been redirected. If output has been redirected, meaning an output file has been specified, the output is written to the specified file. If output has not been redirected, it goes to the Master Console.;One version of redirecting output to a file is by creating a call symbol and adding the following;Redirect_Output(?file.txt");Note: If the file specified already exists, it will be overwritten with no warning! All of the file's previous contents will be lost!;The second version of Redirect_Output redirects output with a simple yes or true argument;Redirect_Output(True);This delays the selection of the output file to run time. When the Call symbol containing Redirect_Output is executed, a file selection dialog box will open, and the user can specify which file is to be used for output.;After a successful call to Redirect_Output, the program writes its output to the specified file. To reset Raptor so that subsequent Output symbols write their output to the Master Console, another call to Redirect_Output is used, this time with a False (No) argument;Redirect_Output(False);After this call is executed, the output file is closed, and subsequent outputs will again go to the Master Console.;There is no Append option in Raptor.;Input to a File using Raptor;This is done the same way, except Redirect_Input() is called.;To pull something in from a file, the input symbols are used.;This lab requires you to create a flowchart for the blood drive program in Lab 10.1. Use an application such as Raptor or Visio.;Step 1: Start Raptor and open your Lab 9-3. Save this file as Lab 10-3. The.rap file extension will be added automatically.;Step 2: Remove the reference no longer need. This is the highPints and lowPints variables, and the getHigh, getLow, and displayInfo modules. With the modules, first delete the function calls, and the right click on the tabs and select Delete Subchart.;Step 3: In main after the module call to getAverage, add a call to writeToFile.;Step 4: Go to that module and add a call symbol.
Paper#67774 | Written in 18-Jul-2015Price : $67