Starting from:
$30

$24

Lab 0302 Solution




1. Problem Statement




Create a program that will open a given file and print the contained values. If there is not a file or the file is empty then display an error.




2. Requirements




2.1 Assumptions




Written in C++



Compiled exclusively in Visual Studio



File “​in.dat​” will be provided for testing
File will contain integers



2.2 Specifications




Array Size is 20



Two External Functions



Reading File to Array



Printing Array to Console



Assertion when array is empty



Decomposition Diagram



Program



Input



Read in file [​in.dat​]
Process



Fill array with integers from file



Output



Print filled array in order



Test Strategy



Valid Data



Invalid Data



Test Plan Version 1
Test Strategy
#
Description
Input
Expected Output
Actual Output
Pass/Fail














Valid Data
1
File Exists






















Valid Data
2
File contains 20












good integers












readIntFile​()












printFileValues​()








Valid Data
3
File contains < 20












good integers












readIntFile​()












printFileValues​()








Invalid Data
1
File Does Not Exist































Initial Algorithm



Include Compiler Directives



“assert.h”



<iostream



<fstream



<string



Create External Functions



Create Function



Name: ​readIntFile​()
Parameters: ​ifstream​by reference, integer array ​intArray​[], int ​size​, int length​by reference



Return: void



Method:



Declare counter variable ​i​= 0
Until end of file



Fill array at index ​i
Iterate ​i



Pass back ​length ​as ​i



Create Function



Name: ​printFileValues​()
Parameters: integer array ​intArray​[], integer ​length​by reference



Return: Void



Method:



Assert that array has any values



Print array length



Loop until length reached
a. Print each array index formatted

Main Method ​main​()



Prompt user for file name [​in.dat​]



Open file with given file name



If file is open



Call ​readIntFile​()
Call ​printFileValues​()



Else



Alert user that file does not exist



Test Plan Version 2






Test Strategy
#
Description
Input


Expected Output
Actual Output
Pass/Fail
















Valid Data
1
File Exists
“​in.dat​”


Array Filled




Valid Data
2
File contains 20
“​in.dat​”


Array Filled








good integers
7


intArray​[0] = 7








readIntFile​()
8


intArray​[1] = 8








printFileValues​()
91011-1-30
intArray​[2] = 9














intArray​[3] = 10














intArray​[4] = 11










15 88
99
intArray​[5] = -1














intArray​[6] = -3










200 15 99


intArray​[7] = 0














intArray​[8] = 15










8888 -2 0 -99
intArray​[9] = 88














intArray​[10] = 99














intArray​[11] = 200














intArray​[12] = 15










-300


intArray​[13] = 99














intArray​[14] = 8888














intArray​[15] = -2














intArray​[16] = 0














intArray​[18] = -99














intArray​[19] = -300




Valid Data
3
File contains < 20
“​in2.dat​”


intArray​[0] = 404








good integers
404


intArray​[1] = 6502








readIntFile​()
6502


intArray​[2] = 666








printFileValues​()
666


intArray​[3] = 8088










8088


intArray​[4] = 7










7


intArray​[5] = 42










42



























Invalid Data 1 File Does Not Exist “​in.txt​’ FILE DNE



More products