1A. Write a function called insertIntoSortedArray .
- It should take three arguments
- myArray[ ] : sorted array that should be able to hold at most 100 integers.
- numEntries : the number of elements inserted so far.
- newValue : the incoming value to be inserted into the sorted array (i.e.
myArray[]).
- The insertIntoSortedArray function 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);
31, August 2019
1 B. Write a complete program to do the following :
- Reading the file: Your program should take a single command line argument i.e. the
name of the file where the integers are present.
- This file needs to be stored in the same directory as your program.
- The file should store up to 100 integers on separate lines. You can use the file named txt on Moodle, or create your own if you prefer.
- In the main function:
- Create an array of integers to store at most 100 integers.
- 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 getline function to read the integers one by one.
- Store these integers in a sorted array by passing them to the insertIntoSortedArray function (you can 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
1, 6
1, 2, 6
1, 2, 6, 12
1, 2, 5, 6, 12
31, August 2019
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.
- Write the lines where the area of the park is greater than the minimum value into the output .csv
- 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:
- The addPark function has the following signature:
// 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 the parkname, state, area values in it.
- Add the struct to the parks
2B. Write a function named printList:
- The printList function has the following signature:
// length: Number of items in the array
void printList(const Park parks[], int length);
- Loop through the parks
- Print out each element of the parks array 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 a complete program which includes the following:
- The park struct and the addPark, printList functions coded above.
- A main() function defined as below:
- Your main() should handle three command line arguments: the name of the input .csv file, the name of the output .csv file and a minimum area respectively.
- Input and output files need to be stored in the same directory as your program.
- Read from the input file, csv :
- Each line of the file can be read using getline
- 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 addPark function to update the parks
- Call the printList function after the array has been filled with data.
- Write into output .csv file:
- Write the <parkname>, <state>, <area> of the parks, whose
<area> is more than the minimum_area (read from command line) into the output .csv file.
- Make sure you close the file when you are done.
Check next page for sample input and output.
Assignment 1
31, August 2019
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
Reviews
There are no reviews yet.