Assignment 1 C++ FUNDAMENTALS Solution

$30.00 $24.00

​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…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

OBJECTIVES

  1. Read-in command line arguments

  1. Read a file

  1. Loop through an array

  1. Split a string

  1. Create an array of struct types

  1. 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

    1. myArray[ ] :​ ​sortedarray that should be able to hold at most 100 floats.

    2. numEntries : the number of elements inserted so far.

    1. newValue : the incoming value to be inserted into the sorted array (i.e. myArray[]).

  1. 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.

    1. This file needs to be stored in the same directory as your program.

    1. The file should store up to 100 numbers on separate lines. For testing you can use the file namednumbers.txton Moodle, or create your own if you prefer.

ii. In the main function:

    1. Create an array of floats to store at most 100 floats.

    1. Open the file that was passed via the command line

      1. If there is any error in opening the file then print the below statement std::cout << “Failed to open the file.” << std::endl;

    1. Use the getlinefunction to read the integers one by one.

    2. Store these integers in a sorted array by passing them to the insertIntoSortedArrayfunction (you should use the code from part 1A).

    3. 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:

  1. Reads a “.csv” file with up to 100 lines and columns containing information on national parks.

  1. Stores the information in an array of structs.

  1. 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.

  1. 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);

  1. Instantiate a struct and store theparkname, state, and area values in it.

  2. 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);

  1. Loop through the parksarray.

  1. 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:

  1. The parkstruct and the addPark, printList functions coded above.

  1. A main()function defined as below:

    1. 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.

    1. Input and output files need to be stored in the same directory as your program.

    1. Read from the input file, “park.csv” :

      1. Each line of the file can be read using getline function.

      1. 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)

      1. Call addParkfunction to update the parksarray.

    1. Call the printList function after the array has been filled with data.

    1. 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:

      1. 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.

      1. 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.

    1. 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

Assignment 1 C++ FUNDAMENTALS Solution
$30.00 $24.00