51.2k views
1 vote
Lab assignment 7: Verifying sortedness of a sequence To be completed by Thursday March 16. Objectives:

Design and implement a function for validating sortedness when reading data from an input stream into an array. Learn the concept of designing loops with multiple exit conditions. IMPORTANT. Make sure you fully understand the video for Lecture 2 in Week 7 before you start on the lab For the tests, we have the following possibilities: file ends first, array fills first, data in file is not sorted. All these cases should be tested. Sometimes there is a need to check if the data in a file is sorted. In this lab we will write a function readSortedArray. When we call this function, an input filestream and an array of float values are specified. The function uses a loop that behaves in the following way: As the items are read, the function compares adjacent items to verify that they are in non- decreasing order. As long as items are in order, they are placed in successive locations in the array. If an adjacent pair of items is not in order, the function abandons the process and returns -1. If the end of the file or the end of the array is reached, the function returns the number of items that were read.

Question 1 (pre-lab). Consider the function readSortedArray. List the parameters that will specify the array and the input filestream. Write the header for the function readSortedArray.

Question 2 (pre-lab). Design a loop to read the data into the array. Note that the first item will be read outside the loop.

Question 3. Implement your function in C++, using your design from Question 2.

Question 4. Write a driver to test your function. The main program will declare two arrays: one of 20 float values, and another of 30 values, and open the input file and an output file. It then calls the function to store the items from the file into the first array. Then it displays the result: if the file was not sorted, a message is printed; otherwise it calls the display function from the previous lab, to print the values that were stored in the array. Next, the program closes the input file, clears the input stream, opens the file again and calls the function to store the items from the file into the second array and again displays the result. Test it on multiple files, including the empty file, a file with less than 20 items, a file with exactly 20 items, a file with more than 30 items and an unsorted file

User Redbox
by
7.8k points

1 Answer

3 votes

Final answer:

The readSortedArray function takes an input filestream object and an array of floats as parameters. It reads the first item outside the loop, then uses a loop to compare adjacent items and place them in successive locations in the array. If any adjacent pair is not in order, it returns -1. Once the end of the file or the end of the array is reached, it returns the number of items read.

Step-by-step explanation:

Question 1 (pre-lab):

The parameters that will specify the array and the input filestream for the function readSortedArray are:

  1. An input filestream object
  2. An array of float values

The header for the function readSortedArray would be:

int readSortedArray(ifstream& file, float arr[])

Question 2 (pre-lab):

The loop to read the data into the array can be designed as follows:

file >> arr[0]; // Read the first item outside the loop

int count = 1;
float currentItem;

while (file >> currentItem)
{
if (currentItem < arr[count - 1])
{
return -1; // Abandon the process and return -1 if items are not in order
}
arr[count] = currentItem; // Place the item in the next location in the array
count++;
if (count >= 20)
{
break; // Exit the loop if the end of the array is reached
}
}

Question 3:

Here is an implementation of the readSortedArray function in C++:

#include <iostream>
#include <fstream>

using namespace std;

double readSortedArray(ifstream& file, float arr[])
{
file >> arr[0]; // Read the first item outside the loop

int count = 1;
float currentItem;

while (file >> currentItem)
{
if (currentItem < arr[count - 1])
{
return -1; // Abandon the process and return -1 if items are not in order
}
arr[count] = currentItem; // Place the item in the next location in the array
count++;
if (count >= 20)
{
break; // Exit the loop if the end of the array is reached
}
}

return count; // Return the number of items that were read
}

Question 4:

Here is a driver program to test the readSortedArray function:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
float arr1[20];
float arr2[30];

ifstream inputFile("input.txt");
ofstream outputFile("output.txt");

double result1 = readSortedArray(inputFile, arr1);

if (result1 == -1)
{
cout << "File is not sorted." << endl;
}
else
{
display(arr1, result1); // Call the display function from the previous lab to print the values stored in the array
}

inputFile.close();
inputFile.clear();
inputFile.open("input.txt");

double result2 = readSortedArray(inputFile, arr2);

if (result2 == -1)
{
cout << "File is not sorted." << endl;
}
else
{
display(arr2, result2); // Call the display function from the previous lab to print the values stored in the array
}

inputFile.close();
outputFile.close();

return 0;
}
User EugeneZ
by
7.8k points