H26.CPP ---------------------------------------------------------------- #include #include #include using namespace std; #include "h26.h" // Add your code here PLEASE WRITE THE CODE HERE //////////////////////// STUDENT TESTING ////////////////////////// #include #include int run() { cout << "Add your own tests here" << endl; // istringstream in("8 9 Q 4 5"); // FlexArray a; // in >> a; // cout << "a->" << a << endl; return 0; } H26.H #ifndef H26_H_ #define H26_H_ #include #include const size_t INITIAL_CAPACITY = 2; struct FlexArray { size_t size_ = 0; std::unique_ptr data_; }; /** * Read integers from a stream into a FlexArray. * @param[in] in the stream to read from. * @param[out] the FlexArray to store the data in * @return a reference to the modified FlexArray * @post size_ will contain the number of elements * @post data_ will contain exactly size_ elements * @post in will be at end of file or a non-integer */ FlexArray& readData(std::istream& in, FlexArray& a); /** * Return a string representation of a FlexArray. * @param a the array to represent. * @return a comma separated, brace delimited contents. */ std::string toString(const FlexArray& a); inline std::ostream& operator<<(std::ostream& out, const FlexArray& a) { out << toString(a); return out; } inline std::istream& operator>>(std::istream& in, FlexArray& a) { readData(in, a); return in; } #endif

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

H26.CPP

----------------------------------------------------------------

#include <string>
#include <iostream>
#include <memory>
using namespace std;

#include "h26.h"
// Add your code here

PLEASE WRITE THE CODE HERE


//////////////////////// STUDENT TESTING //////////////////////////
#include <iostream>
#include <sstream>
int run()
{
cout << "Add your own tests here" << endl;
// istringstream in("8 9 Q 4 5");
// FlexArray a;
// in >> a;
// cout << "a->" << a << endl;
return 0;
}

 

H26.H

#ifndef H26_H_
#define H26_H_
#include <iostream>
#include <memory>
const size_t INITIAL_CAPACITY = 2;

struct FlexArray
{
size_t size_ = 0;
std::unique_ptr<int[]> data_;
};
/**
* Read integers from a stream into a FlexArray.
* @param[in] in the stream to read from.
* @param[out] the FlexArray to store the data in
* @return a reference to the modified FlexArray
* @post size_ will contain the number of elements
* @post data_ will contain exactly size_ elements
* @post in will be at end of file or a non-integer
*/
FlexArray& readData(std::istream& in, FlexArray& a);

/**
* Return a string representation of a FlexArray.
* @param a the array to represent.
* @return a comma separated, brace delimited contents.
*/
std::string toString(const FlexArray& a);

inline std::ostream& operator<<(std::ostream& out, const FlexArray& a)
{
out << toString(a);
return out;
}

inline std::istream& operator>>(std::istream& in, FlexArray& a)
{
readData(in, a);
return in;
}
#endif

Be sure to delete any intermediate arrays. If you have any memory leaks displayed, fix
them before submitting your assignment.
Remember: unique_ptr does not support ordinary copy or assignment, but you can
create a reference to a unique_ptr and, you can transfer ownership from one
unique_ptr to another by calling release() and/or reset():
release() returns the "raw" pointer stored in the unique_ptr and makes
that unique_ptr null.
reset() takes an optional raw pointer and repositions the unique_ptr to
point to the given pointer. If the unique_ptr is not null, then the object to
which the unique_ptr had pointed is deleted.
Calling release() breaks the connection between a unique_ptr and the object it
manages. The pointer returned by release() is often used to initialize another smart
pointer; responsibility for memory is transferred from one smart pointer to another.
| 2. The toString Function
For tostring(), the result should be:
• Delimited with braces "{" and "}".
• Individual elements should be separated with a comma and a space.
• There should be no space before the first element or after the last.
You'll recognize this as the fencepost algorithm. You can convert the integer elements
in the array by using the to_string() function in the <string> header.
When you add that and make test, all of the tests should pass.
Be sure to make submit to turn in your code for credit before the deadline. As always,
if you run into problems, bring your questions to Piazza or come to my office hour.
Transcribed Image Text:Be sure to delete any intermediate arrays. If you have any memory leaks displayed, fix them before submitting your assignment. Remember: unique_ptr does not support ordinary copy or assignment, but you can create a reference to a unique_ptr and, you can transfer ownership from one unique_ptr to another by calling release() and/or reset(): release() returns the "raw" pointer stored in the unique_ptr and makes that unique_ptr null. reset() takes an optional raw pointer and repositions the unique_ptr to point to the given pointer. If the unique_ptr is not null, then the object to which the unique_ptr had pointed is deleted. Calling release() breaks the connection between a unique_ptr and the object it manages. The pointer returned by release() is often used to initialize another smart pointer; responsibility for memory is transferred from one smart pointer to another. | 2. The toString Function For tostring(), the result should be: • Delimited with braces "{" and "}". • Individual elements should be separated with a comma and a space. • There should be no space before the first element or after the last. You'll recognize this as the fencepost algorithm. You can convert the integer elements in the array by using the to_string() function in the <string> header. When you add that and make test, all of the tests should pass. Be sure to make submit to turn in your code for credit before the deadline. As always, if you run into problems, bring your questions to Piazza or come to my office hour.
A Flex Array
he heap allows you to create dynamic arrays, and wait until runtime
to decide how many elements are needed, unlike the static arrays which are
built into the C++ language. In fact, the heap is what the vector and string classes
use to allow them to expand as the user adds new elements to the collection.
T
You'll find the definition for the FlexArray structure, as well as the prototypes for the
functions you are going to write in the header file. Do not make any changes to the
header file at all. You are going to write two functions. Here are the descriptions:
|1. The readData Function
The readData() function reads integers from an input stream (such as cin) until the
user terminates by either running out of data or by entering an invalid input such as Q.
• The function sets the size_data member to the number of numeric inputs.
• The member data_is a standard unique_ptr to a heap allocated array.
• At the end of the function, the array should have exactly size_elements.
• The function returns a reference to the modified FlexArray object.
At the outset, you won't know how many elements the user will enter. So, start with a
capacity of INITIAL_CAPACITY. Do not change this from its current value of 2.
Managing Memory in readData
In readData(), whenever your allocated array fills up, create a new array of double
the current capacity, copy the original elements to the new storage, free the original
array and assign the new array to the member data_.
At the end of your function, you'll follow a similar pattern to shrink the allocated
memory so that it is exactly the same size as the number of elements.
Transcribed Image Text:A Flex Array he heap allows you to create dynamic arrays, and wait until runtime to decide how many elements are needed, unlike the static arrays which are built into the C++ language. In fact, the heap is what the vector and string classes use to allow them to expand as the user adds new elements to the collection. T You'll find the definition for the FlexArray structure, as well as the prototypes for the functions you are going to write in the header file. Do not make any changes to the header file at all. You are going to write two functions. Here are the descriptions: |1. The readData Function The readData() function reads integers from an input stream (such as cin) until the user terminates by either running out of data or by entering an invalid input such as Q. • The function sets the size_data member to the number of numeric inputs. • The member data_is a standard unique_ptr to a heap allocated array. • At the end of the function, the array should have exactly size_elements. • The function returns a reference to the modified FlexArray object. At the outset, you won't know how many elements the user will enter. So, start with a capacity of INITIAL_CAPACITY. Do not change this from its current value of 2. Managing Memory in readData In readData(), whenever your allocated array fills up, create a new array of double the current capacity, copy the original elements to the new storage, free the original array and assign the new array to the member data_. At the end of your function, you'll follow a similar pattern to shrink the allocated memory so that it is exactly the same size as the number of elements.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY