C++ FUNDAMENTALS

$24.99 $18.99

​OBJECTIVES Read-in command line arguments Read a file Loop through an array Split a string Create an array of struct types Pass by reference Write code to complete the ​Problems 1 and 2​. Implement each of the problems separately. Problem 1 Overview: You will write a program that reads up to 100 numbers from a…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

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. Pass by reference

Write code to complete theProblems 1 and 2. Implement each of the problems separately.

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 ascending order.

Specifics:

1A.Write a function calledinsertIntoSortedArray.

i. It should take three arguments

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

    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. TheinsertIntoSortedArrayfunction should return a count of the elements inserted so far (i.e. the current size of the array)

This function is defined as follows:

int​ ​insertIntoSortedArray(int myArray[],int numEntries,int newValue);

1 B.Write acomplete 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 integers are present.

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

    1. The file should store up to 100 integers on separate lines. 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 integers to store at most 100 integers.

    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 thegetlinefunction to read the integers one by one.

    2. Store these integers in a sorted array by passing them to the insertIntoSortedArrayfunction (you can 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

1, 6

1,2,6

1, 2, 6, 12

1, 2, 5, 6, 12

CSCI 2270 – Data Structures

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. Write the lines where thearea of the parkis greater than the minimum value into the output​​.csvfile.

  1. Prints the content of the entire array.

Specifics:

Create an array that holds thePark struct objects. Use the following struct declaration:

struct​ ​Park{

string parkname;

string state;

int area;

};

2A. Write a function namedaddPark:

  1. TheaddParkfunction has the following signature:

    • length: Number of items currently stored in the array

void addPark(Parkparks[], stringparkname, stringstate, intarea, int length);

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

  2. Add the struct to theparksarray.

2B. Write a function namedprintList:

  1. TheprintListfunction has the following signature:

    • length: Number of items in the array

void​ ​printList(const​ ​Parkparks[], intlength);

  1. Loop through theparksarray.

  1. Print out each element of theparksarray 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”

2C.Write acomplete programwhich includes the following:

  1. Theparkstruct and theaddPark, printListfunctions coded above.

  1. Amain()function defined as below:

    1. Yourmain()should handle three command line arguments: the name of the input.csv” file, the name of theoutput.csv” file and aminimum 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 usinggetlinefunction.

      1. Parse each line usingstringstream and convert each entry into its appropriate data type.parkname should be a string,stateshould be a string, andareashould be an integer.(Hint: Usestoi, stof functions to convert from strings to numbers)

      1. CalladdParkfunction to update theparksarray.

    1. Call theprintListfunction after the array has been filled with data.

    1. Write intooutput​​“.csv”file:

      1. Write the <parkname>, <state>, <area> of the parks, whose

<area> is more than theminimum_area (read from command line) into theoutput “.csv”file.

    1. Make sure you close the file when you are done.

Check next page for sample input and output.

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 minimum area 200000 should contain the following:

Badlands National Park, SD, 242756

Big Bend National Park, TX, 801163

C++ FUNDAMENTALS
$24.99 $18.99