Description
OBJECTIVES
-
Read-in command line arguments
-
Read a file
-
Loop through an array
-
Split a string
-
Create an array of struct types
-
Read and Write to a file
Instructions
Write code to complete the Problems 1 and 2. Implement each of the problems as separate programs. To receive credit for your code, you will need to paste your solution into Moodle’s autograder (CodeRunner). Your code needs to compile and pass the given test cases in order to receive points. Note that the autograder submissions are divided into separate parts, so that you will be required to test the individual functions prior to testing the entire program.
Problem 1
Overview: You will write a program that reads up to 100 numbers from a file. As you read the numbers, insert them into an array in descending order.
Specifications:
1A.Write a function called insertIntoSortedArray.
i. It should take three arguments –
-
-
myArray[ ] : sortedarray that should be able to hold at most 100 floats.
-
numEntries : the number of elements inserted so far.
-
-
-
newValue : the incoming value to be inserted into the sorted array (i.e. myArray[]).
-
-
The insertIntoSortedArrayfunction should return a count of the elements inserted so far (i.e. the current size of the array)
The function header will appear as follows:
int insertIntoSortedArray(float myArray[], int numEntries, float newValue);
CSCI 2270 – Data Structures Spring 2020
Instructors: Zagrodzki, Ashraf, Trivedi
1B. Write a complete programto do the following:
i. Reading the file: Your program should take a single command line argument i.e. the name of the file where the numbers are present.
-
-
This file needs to be stored in the same directory as your program.
-
-
-
The file should store up to 100 numbers on separate lines. For testing you can use the file named“numbers.txt”on Moodle, or create your own if you prefer.
-
ii. In the main function:
-
-
Create an array of floats to store at most 100 floats.
-
-
-
Open the file that was passed via the command line
-
-
-
-
If there is any error in opening the file then print the below statement std::cout << “Failed to open the file.” << std::endl;
-
-
Use the getlinefunction to read the integers one by one.
-
Store these integers in a sorted array by passing them to the insertIntoSortedArrayfunction (you should use the code from part 1A).
-
Each time a new number is read, print out the entire array after insertion.
-
The Input and Output formats are shown below:
Testcase 1:
FileContents: arr.txt
1
6
2
12
5
Your Output:
1
6, 1
6,2,1
12, 6, 2, 1
12, 6, 5, 2, 1
CSCI 2270 – Data Structures Spring 2020
Instructors: Zagrodzki, Ashraf, Trivedi
Problem 2
Overview:In this question, you will write a program that:
-
Reads a “.csv” file with up to 100 lines and columns containing information on national parks.
-
Stores the information in an array of structs.
-
Writes the lines into the output .csv file where the area of the park is within a range specified by the lower bound and upper bound.
-
Prints the content of the entire array.
Specifics:
Create an array that holds the Park struct objects. Use the following struct declaration:
struct Park{
string parkname;
string state;
int area;
};
2A. Write a function named addPark:
i. The addParkfunction has the following declaration:
// length: Number of items currently stored in the array
void addPark(Park parks[], string parkname, string state, int area, int length);
-
Instantiate a struct and store theparkname, state, and area values in it.
-
Add the struct to the parks array.
2B. Write a function named printList:
i. TheprintListfunction has the following signature:
// length: Number of items in the array
void printList(const Park parks[], int length);
-
Loop through the parksarray.
-
Print out each element of the parksarray in the following format. “<PARKNAME> [<STATE>] area: <AREA>” using the below cout statement
std::cout << park.parkname <<” [” << park.state << “] area: “<< park.area << std::endl;
Example:
“Acadia National Park [ME] area: 47390”
CSCI 2270 – Data Structures Spring 2020
Instructors: Zagrodzki, Ashraf, Trivedi
2C. Write a complete programwhich includes the following:
-
The parkstruct and the addPark, printList functions coded above.
-
A main()function defined as below:
-
Your main() should handle four command line arguments: the name of the input “.csv” file, the name of the output “.csv” file, a lower bound of area and an upper bound of area respectively.
-
-
-
Input and output files need to be stored in the same directory as your program.
-
-
-
Read from the input file, “park.csv” :
-
Each line of the file can be read using getline function.
-
-
-
-
-
Parse each line using stringstream and convert each entry into its appropriate data type. parkname should be a string, state should be a string, and area should be an integer. (Hint: Use stoi, stof functions to convert from strings to numbers)
-
-
-
-
-
Call addParkfunction to update the parksarray.
-
-
Call the printList function after the array has been filled with data.
-
-
-
Writing out to external files in C++ can be done very similarly as reading in. Instead of using an object of the ifstream class, as is done with input streams, use an object of the ofstream class. A “csv” stands for comma separated values. Write into output“.csv”file:
-
-
-
-
Write the <parkname>, <state>, <area> of the parks, whose
-
-
<area> >= lower_bound and <area> <= upper_bound (read from command line) into the output “.csv”file.
-
-
-
You should not sort them while writing to output file. The relative order will be same as input file. Only those park with area within the bound should be written in the output file.
-
-
-
-
Make sure your program closes the output file when it is finished writing.
-
Check next page for sample input and output.
CSCI 2270 – Data Structures Spring 2020
Instructors: Zagrodzki, Ashraf, Trivedi
Sample Input and Output:
Testcase 1:
File Contents: data.csv
Acadia National Park, ME, 47390
Arches National Park, UT, 76519
Badlands National Park, SD, 242756
Big Bend National Park, TX, 801163
Biscayne National Park, FL, 172924
Your print Output:
Acadia National Park [ME] area: 47390
Arches National Park [UT] area: 76519
Badlands National Park [SD] area: 242756
Big Bend National Park [TX] area: 801163
Biscayne National Park [FL] area: 172924
Your output.csv file with area between 200000 and 900000 should contain the following:
Badlands National Park, SD, 242756
Big B