For any help regarding Electrical Engineering and Computer Science Assignment Help
Visit: https://www.programminghomeworkhelp.com/ ,
Email : support@programminghomeworkhelp.com or
call us at - +1 678 648 4277 programminghomeworkhelp.com
Problem 1: C++ Linked List Library (cpplist)
Your job is now to refactor your C code from the second assignment and produce a much more flexible library with similar functionality– and this
time in C++. Download the zipped folder provided in the file cpplist.zip as a basis of your program and take a look at the existing code.
• Write your implementation code in .cpp files in the src/ directory; feel free to add more header
files as needed in the include/ directory
.
• GRADER INFO.txt is a file for the grader (don’t edit!) containing PROG: cpplist, LANG: C++
• As before: you should submit a zipped folder using the same directory structure as the provided zip file. We’ve added a section in the Makefile for your
convenience: if you type make zip in the same folder as your project, a zip file containing all of your code and the required headers will be constructed
in the project directory and you can upload that to the grader.
Weare provided a header file describing the interface for the List data structure. Look in the file list.h
to find the functionality required of the other functions, which you will write.
II Forward declaration of applyIreduce types class ApplyFunction;
class ReduceFunction;
class List {
II ... put whatever private data members you need here II can also add any private member
functions you'd like
public: List();
-List();
size t length() const; int& value( size t pos );
int value( size t pos ) const; void append( int value ); void deleteAll(
int value );
void insertBefore( int value, int before ); void apply( const ApplyFunction &interface
);
int reduce( const ReduceFunction &interface ) const; void print() const;
};
II ..etc
Some questions to ask yourself for understanding:
• Why is there a function int& value( size t pos ); as well as a function
int value( size t pos ) const;?
• What is a forward declaration?
programminghomeworkhelp.com
Assignment 3, Problem 1
• Why don’t we include the headers apply.h and reduce.h here?
You’llfind thosetwoother header filescontaining definitions for your ApplyFunction and ReduceFunction
classes, shown here:
#include "list.h"
class ReduceFunction { protected:
virtual int function( int x, int y ) const = 0; public:
int reduce( const List &list ) const; virtual int identity()
const = 0; virtual -ReduceFunction() {}
};
II An example ReduceFunction
class SumReduce : public ReduceFunction { int function( int x, int
y ) const;
public: SumReduce() {}
-SumReduce() {}
int identity() const { return 0; }
};
and then in the source code file:
#include "list.h" #include "reduce.h"
II This works fine, but iterating over a list like this is II fairly slow. See if
you can speed it up!
int ReduceFunction::reduce( const List &list ) const { int result = identity();
for( size t p = 0; p < list.length(); ++p ) { result = function(
result, list.value( p ) );
}
return result;
}
int SumReduce::function( int x, int y ) const { return x + y;
programminghomeworkhelp.com
Assignment 3, Problem 1
Input/Output Format
Not applicable; your library will be compiled into a testing suite, your implemented functions will be called by the program, and the behavior checked for
correctness. For example, here is a potential test:
#include "list.h" int main() {
int N = 5;
II you'll need to write a copy constructor II to be able to do this (see Lecture 5)
auto list = List{};
for( int i = 0; i < N; ++i ) { list.append( i );
}
list.print(); return 0;
}
Upon calling this function, the code outputs
{ 0 -> 1 -> 2 -> 3 -> 4 }
or whatever your formatted output from list.print() is made to look like. Youare strongly encour aged to write your own tests in test.cpp so that you can
try out your implementation code before submitting it to the online grader.
Best Practices
The problem is only worth 500/1000 points when you submit; the rest of the grade will be based on how well your code follows C++ best practices and
object-oriented programming principles. Seea list of those here. The rubric for the other 500 points is as follows.
• +500 points: Code is eminently readable, follows best practices, highly efficient, well structured,
and extensible.
• +400 points: Code is easy to follow, only a few small violations of best practices, and extensible.
• +300 points: Adecent refactoring effort, no egregiously bad practices, might bedifficult to extend.
• +200 points: Some refactoring effort, lots of violations of best practices, not very extensible
• +100 points: Minor refactorings/improvements, little effort to follow best practices.
• +0 points: No effort to refactor or improve code (basically direct copy of HW#2)
programminghomeworkhelp.com
Look in list .h for a sense of the structure of the solution. The big idea to speed up the reduce/apply
functions while also giving users a nice way to iterate over the items in the list is to create an "iterator"
type within our class. Users will be able to write code similar to the STL:
II Print out every item in the list
for(List: :iterator it = list .begin (); it != list .end( ); ++it ){ std ::cout < < *it << "n";
}
To speed up our "append" function, the List class will also store a pointer to the very last element in the
current list. Directory structure:
GRADER_INFO.txt
include
apply.h o list.h
list_node.h o reduce.h
Makefile
src
apply.cpp o list.cpp
list_iterator.cpp o list_node.cpp
reduce.cpp o test.cpp
Here are the contents of apply.h:
#ifndef _65096_CPPLIST_APPLY_H
#define _65096_CPPLIST_APPLY_H
#include "list.h "
class ApplyFunction { protected :
virtual int function(int x )const public :
void apply(List &list )const; virtual -ApplyFunction (){}
}; programminghomeworkhelp.com
0J·
II An example ApplyFunction (see apply.cpp) class SquareApply : public ApplyFunction {
int function(int x )const;
};
#endif II _65096_CPPLIST_APPLY_H
Here are the contents of list.h:
#ifndef _65096_CPPLIST_H
#define _65096_CPPLIST_H
#include ccstddef>
#include cstdexcept>
class ApplyFunction;
r l """ RPrl11r PF11n rt"i nn :
programminghomeworkhelp.com
class List { size_t _length;
ListNode *_begin; ListNode *_back;
public :
II Can use outside as List ::iterator type class iterator {
II Making List a friend class means we'll be able to access II the private _node pointer data
within the scope of List. friend class List;
ListNode *_node; public :
iterator(ListNode *theNode ); iterator& operator++ ();
int& operator* ();
bool operator== (const iterator &rhs ); bool operator!=(const iterator &rhs );
};
II Can use outside as List: :const_iterator type class const_iterator {
II Again, this is basically the only situation you should
II be using the keyword 'friend'
friend class List; ListNode *_node;
public :
const_iterator (ListNode *theNode ); const_iterator& operator++ ();
const int& operator* ();
bool operator== (const const_iterator &rhs ); bool operator!=(const const_iterator &rhs );
};
List ();
List (const List &list );
List& operator= (const List &list );
-List();
size_t length{)const; int& value (size_t pos );
programminghomeworkhelp.com
int value (size_t pos )const; bool empty ()const;
iterator begin (); const_iterator begin ()const ; iterator back (); const_iterator
back ()const ; iterator end ();
const_iterator end{)const;
iterator find{ iterator s, iterator t, int needle ); void append{ int theValue );
void deleteAll{ int theValue );
void insertBefore (int theValue, int before ); void insert(iterator pos, int
theValue );
void apply(const ApplyFunction &interface );
int reduce(const ReduceFunction &interface )const; void print ()const;
void clear();
private :
ListNode* node{ iterator it ){return it ._node; } ListNode* node{
const_iterator it ){return it ._node; }
};
class ListOutOfBounds : public std ::range_error { public :
explicit ListOutOfBounds{) : std ::range_error("List index out of bounds" ){}
};
#endif II _6S096_CPPLIST_H programminghomeworkhelp.com
#ifndef _65096_CPPLIST_NODE_H
#define _65096_CPPLIST_NODE_H
class ListNode { int _value; ListNode *_next;
ListNode (const ListNode & )= delete;
ListNode& operator= (const ListNode & ) delete; public :
ListNode ();
ListNode (int theValue );
-ListNode (); int& value ();
int value ()const ; ListNode* next ();
void insertAfter (ListNode *before ); void setNext(ListNode *nextNode );
static void deleteNext (ListNode *before );
static void deleteSection ( ListNode *before, ListNode *after );
static ListNode* create(int theValue = 0 );
};
#endif II _65096_CPPLIST_NODE_H
Here are the contents of reduce.h:
#ifndef _65096_CPPLIST_REDUCE_H
#define _65096_CPPLIST_REDUCE_H
#include "list.h"
class ReduceFunction { protected :
virtual int function(int x, int y )const 0; public :
int reduce(const List &list const; virtual int identity ()const 0; virtual -ReduceFunction (){}
}; programminghomeworkhelp.com
II An example ReduceFunction
class SumReduce : public ReduceFunction { int function(int x, int y )const;
public : SumReduce (){}
-sumReduce (){}
int identity ()const {return 0; }
};
II Another ReduceFunction
class ProductReduce : public ReduceFunction { int function(int x, int y )const;
public :
ProductReduce (){}
-ProductReduce (){}
int identity ()const {return 1; }
};
#endif II _65096_CPPLIST_REDUCE_H
Here is the source code file apply.cpp:
#include "list.h"
#include "apply.h"
void ApplyFunction::apply (List &list )const {
for(auto it = list .begin (); it != list.end( ); ++it ){
*it = function (*it );
}
} programminghomeworkhelp.com
Here is the source code file list.cpp:
#include "list.h"
#include "list_node.h"
#include "apply.h"
#include "reduce.h"
#include <iostream>
List: :List() : _length {0}, _begin {nullptr }, _back{ nullptr }{}
List ::List(const List &list ) : _length {0}, _begin {nullptr }, _back {nullptr }{ for(auto it = list.begin ();
it != list.end( ); ++it ){
append( *it );
}
}
List& List ::operator= (const List &list ){ if(this != &list ){
clear();
for(auto it = list .begin (); it != list.end( ); ++it ){ append (*it );
}
}
return *this;
}
List: :-List(){clear (); }
size_t List: :length ()const {return _length; } int& List: :value(size_t pos ){
auto it = begin (); programminghomeworkhelp.com
for(size_t i = 0; i < pos && it != end( ); ++it, ++i ); if(it == end()){
throw ListOutOfBounds ();
}
return *it;
}
int List: :value(size_t pos )const { auto it = begin ();
for(size_t i = 0; i < pos && it != end( ); ++it, ++i ); if(it == end()){
throw ListOutOfBounds ();
}
return *it;
}
bool List: :empty()const { return _length 0;
}
List: :iterator List: :begin(){return iterator{_begin }; }
List: :const_iterator List: :begin()const {return const_iterator {_begin }; } List ::iterator List:
:back(){return iterator {_back }; }
List ::const_iterator List: :back()const {return const_iterator {_back }; } List: :iterator List:
:end(){return iterator{nullptr }; }
List: :const_iterator List: :end()const {return const_iterator {nullptr }; } programminghomeworkhelp.com
void List ::append (int theValue ){
auto *newNode ListNode: :create(theValue );
if ( empty ()){
newNode->setNext (_back );
_begin = newNode;
}else {
newNode->insertAfter (_back );
}
programminghomeworkhelp.com
}
void List: :deleteAll (int theValue ){ if( !empty () ){
II Delete from the front
while(_begin->value ()== theValue && _begin != _back ){ auto *newBegin = _begin->next ();
delete _begin;
_begin = newBegin;
--_length;
}
auto *p = _begin; if(_begin != _back ){
II Normal deletion from interior of list
for(; p->next () != _back; ){
if(p->next ()->value ()== theValue ){ ListNode: :deleteNext(p );
--_length;
}else {
p = p->next ();
}
}
II Deleting the last item
if(_back->value ()== theValue ){ ListNode: :deleteNext (p );
_back = p;
--_length;
}
}else if(_begin->value () programminghomeworkhelp.com
theValue ){
II Deal with the case where we deleted the whole list
_begin = _back = nullptr ;
_length = 0;
}
}
}
List: :iterator List: :find (iterator s, iterator t, int needle ){ for(auto it = s; it != t; ++it ){
if(*it == needle ){ return it;
}
}
return t;
}
void List: :insert (iterator pos, int theValue ){ auto *posPtr = node(pos );
auto *newNode = ListNode: :create(theValue ); newNode->insertAfter (posPtr );
++_length;
}
programminghomeworkhelp.com
void List: :insertBefore(int theValue, int before ){ if( !empty () ){
if(_begin->value ()== before ){
auto *newNode = ListNode: :create (theValue ); newNode->setNext (_begin );
_begin = newNode;
++_length;
}else {
auto *p = _begin;
for(; p != _back && p->next ()->value () != before; p p->next ()); if(p != _back && p->next ()-
>value ()== before ){
auto *newNode = ListNode: :create (theValue ); newNode->insertAfter (p );
++_length;
}
}
programminghomeworkhelp.com
void List: :apply( const ApplyFunction &interface ){ interface.apply(*this );
}
int List: :reduce(const ReduceFunction &interface )const { return interface.reduce(*this );
}
void List: :print()const { std ::cout << "{";
for(auto it = begin (); it != back (); ++it ){ std ::cout << *it << " -> ";
}
if( !empty () ){
std ::cout << *back()<< " ";
}
std ::cout << "}n";
}
void List: :clear (){
for(auto *p = _begin; p != nullptr; ){ auto *p_next = p->next ();
delete p;
p = p_next;
}
_length = 0;
_begin = nullptr;
_back = nullptr;
}
Here is the source code file list_iterator.cpp:
#include "list.h"
#include "list_node.h" programminghomeworkhelp.com
List ::iterator::iterator (ListNode *theNode ): _node{theNode }{} List: :iterator& List:
:iterator: :operator++ (){
_node = _node->next (); return *this;
}
int& List ::iterator: :operator* (){return _node->value (); }
bool List: :iterator: :operator== (const iterator &rhs ){return _node rhs._node;
} bool List: :iterator: :operator!= (const iterator &rhs ){return _node != rhs ._node; }
List ::const_iterator: :const_iterator(ListNode *theNode ): _node {theNode }{} List:
:const_iterator& List: :const_iterator: :operator++(){
_node = _node->next (); return *this;
}
const int& List ::const_iterator: :operator*(){return _node->value (); }
bool List ::const_iterator: :operator== (const const_iterator &rhs ){return _node
rhs._node; } bool List: :const_iterator: :operator!= (const const_iterator &rhs
){return _node != rhs._node; }
Here is the source code file list_node.cpp:
#include "list_node.h"
programminghomeworkhelp.com
ListNode: :ListNode () : _value {0}, _next {nullptr }{}
ListNode: :ListNode (int theValue ): _value{theValue }, _next {nullptr }{} ListNode:
:ListNode (){}
int& ListNode: :value(){return _value; }
int ListNode: :value(){const {return _value; } ListNode* ListNode: :next(){return
_next; }
void ListNode: :insertAfter(ListNode *before ){
_next = before->next (); before->_next = this;
}
void ListNode::setNext (ListNode *nextNode ){
_next = nextNode;
}
programminghomeworkhelp.com
delete before->next ();
before->_next = after;
}
void ListNode::deleteSection (ListNode *before, ListNode *after ){ auto *deleteFront
= before->next ();
while(deleteFront != after ){
auto *nextDelete = deleteFront->next (); delete deleteFront;
deleteFront = nextDelete;
}
}
ListNode* ListNode: :create(int theValue ){ return new ListNode{theValue };
}
Here is the source code file reduce.cpp:
#include "list.h"
#include "reduce.h"
int ReduceFunction ::reduce (const List &list )const { int result = identity ();
for(auto it = list.begin (); it != list.end( ); ++it ){ result = function(result, *it );
}
return result;
} programminghomeworkhelp.com
int SumReduce: :function (int x, int y )const { return x + y;
}
int ProductReduce: :function (int x, int y )const { return x * y;
}
Below is the output using the test data:
cpplist:
1: OK [0.004 seconds] OK
2: OK [0.005 seconds] OK
3: OK [0.005 seconds] OK
4: OK [0.009 seconds] OK
5: OK [0.006 seconds] OK
6: OK [0.308 seconds] OK
7: OK [0.053 seconds] OK
8: OK [0.007 seconds] OK
9: OK [0.005 seconds] OK
10: OK [0.742 seconds] OK.
programminghomeworkhelp.com

More Related Content

PPTX
Computer Science Assignment Help
PPTX
C Programming Homework Help
PPTX
Operating System Assignment Help
PPTX
C Assignment Help
PPTX
Operating System Engineering
PPTX
Programming Assignment Help
PPTX
Computational Assignment Help
PPTX
Python Homework Help
Computer Science Assignment Help
C Programming Homework Help
Operating System Assignment Help
C Assignment Help
Operating System Engineering
Programming Assignment Help
Computational Assignment Help
Python Homework Help

What's hot (19)

PPTX
Programming Homework Help
PPTX
Computer Science Assignment Help
PPTX
Computer Science Assignment Help
PPTX
Cpp Homework Help
PPTX
Software Construction Assignment Help
DOC
C - aptitude3
DOC
1183 c-interview-questions-and-answers
PDF
C++11 & C++14
DOCX
Type header file in c++ and its function
PPTX
PPT
Csc1100 lecture14 ch16_pt2
PDF
C programming & data structure [arrays & pointers]
PPTX
C++ 11 Features
PDF
C Programming Interview Questions
PDF
Oct.22nd.Presentation.Final
PPS
C programming session 02
PPTX
C++ io manipulation
Programming Homework Help
Computer Science Assignment Help
Computer Science Assignment Help
Cpp Homework Help
Software Construction Assignment Help
C - aptitude3
1183 c-interview-questions-and-answers
C++11 & C++14
Type header file in c++ and its function
Csc1100 lecture14 ch16_pt2
C programming & data structure [arrays & pointers]
C++ 11 Features
C Programming Interview Questions
Oct.22nd.Presentation.Final
C programming session 02
C++ io manipulation
Ad

Similar to Programming Assignment Help (20)

PPTX
CPP Assignment Help
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
PDF
Turn the linked list implementation into a circular list Ha.pdf
DOCX
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
PPTX
3.linked list
PDF
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
PPT
List
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
PDF
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
PPT
Chapter 5 ds
PDF
This assignment and the next (#5) involve design and development of a.pdf
PPT
linked-list.ppt
PDF
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
PPTX
Lecture 4 data structures and algorithms
PDF
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdf
PDF
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
PDF
Consider a double-linked linked list implementation with the followin.pdf
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
CPP Assignment Help
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
Turn the linked list implementation into a circular list Ha.pdf
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
3.linked list
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
List
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
Chapter 5 ds
This assignment and the next (#5) involve design and development of a.pdf
linked-list.ppt
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
Lecture 4 data structures and algorithms
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdf
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
Consider a double-linked linked list implementation with the followin.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
Ad

More from Programming Homework Help (20)

PPTX
Data Structures and Algorithm: Sample Problems with Solution
PPTX
Seasonal Decomposition of Time Series Data
PPTX
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
PPTX
Exploring Control Flow: Harnessing While Loops in Python
PPTX
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
PPTX
C Assignment Help
PPTX
Python Question - Python Assignment Help
PPTX
Best Algorithms Assignment Help
PPTX
Design and Analysis of Algorithms Assignment Help
PPTX
Algorithm Homework Help
PPTX
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
PPTX
Algorithm Homework Help
PPTX
Algorithms Design Assignment Help
PPTX
Algorithms Design Homework Help
PPTX
Algorithm Assignment Help
PPTX
Algorithm Homework Help
PPTX
C Homework Help
PPTX
C Homework Help
PPTX
Algorithm Assignment Help
PPTX
Algorithm Homework Help
Data Structures and Algorithm: Sample Problems with Solution
Seasonal Decomposition of Time Series Data
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
Exploring Control Flow: Harnessing While Loops in Python
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
C Assignment Help
Python Question - Python Assignment Help
Best Algorithms Assignment Help
Design and Analysis of Algorithms Assignment Help
Algorithm Homework Help
programminghomeworkhelp.com_Advanced Algorithms Homework Help.pptx
Algorithm Homework Help
Algorithms Design Assignment Help
Algorithms Design Homework Help
Algorithm Assignment Help
Algorithm Homework Help
C Homework Help
C Homework Help
Algorithm Assignment Help
Algorithm Homework Help

Recently uploaded (20)

PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
HVAC Specification 2024 according to central public works department
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Complications of Minimal Access-Surgery.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
semiconductor packaging in vlsi design fab
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Virtual and Augmented Reality in Current Scenario
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
HVAC Specification 2024 according to central public works department
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
Paper A Mock Exam 9_ Attempt review.pdf.
Complications of Minimal Access-Surgery.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Race Reva University – Shaping Future Leaders in Artificial Intelligence
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
semiconductor packaging in vlsi design fab
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Virtual and Augmented Reality in Current Scenario

Programming Assignment Help

  • 1. For any help regarding Electrical Engineering and Computer Science Assignment Help Visit: https://www.programminghomeworkhelp.com/ , Email : support@programminghomeworkhelp.com or call us at - +1 678 648 4277 programminghomeworkhelp.com
  • 2. Problem 1: C++ Linked List Library (cpplist) Your job is now to refactor your C code from the second assignment and produce a much more flexible library with similar functionality– and this time in C++. Download the zipped folder provided in the file cpplist.zip as a basis of your program and take a look at the existing code. • Write your implementation code in .cpp files in the src/ directory; feel free to add more header files as needed in the include/ directory . • GRADER INFO.txt is a file for the grader (don’t edit!) containing PROG: cpplist, LANG: C++ • As before: you should submit a zipped folder using the same directory structure as the provided zip file. We’ve added a section in the Makefile for your convenience: if you type make zip in the same folder as your project, a zip file containing all of your code and the required headers will be constructed in the project directory and you can upload that to the grader. Weare provided a header file describing the interface for the List data structure. Look in the file list.h to find the functionality required of the other functions, which you will write. II Forward declaration of applyIreduce types class ApplyFunction; class ReduceFunction; class List { II ... put whatever private data members you need here II can also add any private member functions you'd like public: List(); -List(); size t length() const; int& value( size t pos ); int value( size t pos ) const; void append( int value ); void deleteAll( int value ); void insertBefore( int value, int before ); void apply( const ApplyFunction &interface ); int reduce( const ReduceFunction &interface ) const; void print() const; }; II ..etc Some questions to ask yourself for understanding: • Why is there a function int& value( size t pos ); as well as a function int value( size t pos ) const;? • What is a forward declaration? programminghomeworkhelp.com
  • 3. Assignment 3, Problem 1 • Why don’t we include the headers apply.h and reduce.h here? You’llfind thosetwoother header filescontaining definitions for your ApplyFunction and ReduceFunction classes, shown here: #include "list.h" class ReduceFunction { protected: virtual int function( int x, int y ) const = 0; public: int reduce( const List &list ) const; virtual int identity() const = 0; virtual -ReduceFunction() {} }; II An example ReduceFunction class SumReduce : public ReduceFunction { int function( int x, int y ) const; public: SumReduce() {} -SumReduce() {} int identity() const { return 0; } }; and then in the source code file: #include "list.h" #include "reduce.h" II This works fine, but iterating over a list like this is II fairly slow. See if you can speed it up! int ReduceFunction::reduce( const List &list ) const { int result = identity(); for( size t p = 0; p < list.length(); ++p ) { result = function( result, list.value( p ) ); } return result; } int SumReduce::function( int x, int y ) const { return x + y; programminghomeworkhelp.com
  • 4. Assignment 3, Problem 1 Input/Output Format Not applicable; your library will be compiled into a testing suite, your implemented functions will be called by the program, and the behavior checked for correctness. For example, here is a potential test: #include "list.h" int main() { int N = 5; II you'll need to write a copy constructor II to be able to do this (see Lecture 5) auto list = List{}; for( int i = 0; i < N; ++i ) { list.append( i ); } list.print(); return 0; } Upon calling this function, the code outputs { 0 -> 1 -> 2 -> 3 -> 4 } or whatever your formatted output from list.print() is made to look like. Youare strongly encour aged to write your own tests in test.cpp so that you can try out your implementation code before submitting it to the online grader. Best Practices The problem is only worth 500/1000 points when you submit; the rest of the grade will be based on how well your code follows C++ best practices and object-oriented programming principles. Seea list of those here. The rubric for the other 500 points is as follows. • +500 points: Code is eminently readable, follows best practices, highly efficient, well structured, and extensible. • +400 points: Code is easy to follow, only a few small violations of best practices, and extensible. • +300 points: Adecent refactoring effort, no egregiously bad practices, might bedifficult to extend. • +200 points: Some refactoring effort, lots of violations of best practices, not very extensible • +100 points: Minor refactorings/improvements, little effort to follow best practices. • +0 points: No effort to refactor or improve code (basically direct copy of HW#2) programminghomeworkhelp.com
  • 5. Look in list .h for a sense of the structure of the solution. The big idea to speed up the reduce/apply functions while also giving users a nice way to iterate over the items in the list is to create an "iterator" type within our class. Users will be able to write code similar to the STL: II Print out every item in the list for(List: :iterator it = list .begin (); it != list .end( ); ++it ){ std ::cout < < *it << "n"; } To speed up our "append" function, the List class will also store a pointer to the very last element in the current list. Directory structure: GRADER_INFO.txt include apply.h o list.h list_node.h o reduce.h Makefile src apply.cpp o list.cpp list_iterator.cpp o list_node.cpp reduce.cpp o test.cpp Here are the contents of apply.h: #ifndef _65096_CPPLIST_APPLY_H #define _65096_CPPLIST_APPLY_H #include "list.h " class ApplyFunction { protected : virtual int function(int x )const public : void apply(List &list )const; virtual -ApplyFunction (){} }; programminghomeworkhelp.com
  • 6. 0J· II An example ApplyFunction (see apply.cpp) class SquareApply : public ApplyFunction { int function(int x )const; }; #endif II _65096_CPPLIST_APPLY_H Here are the contents of list.h: #ifndef _65096_CPPLIST_H #define _65096_CPPLIST_H #include ccstddef> #include cstdexcept> class ApplyFunction; r l """ RPrl11r PF11n rt"i nn : programminghomeworkhelp.com
  • 7. class List { size_t _length; ListNode *_begin; ListNode *_back; public : II Can use outside as List ::iterator type class iterator { II Making List a friend class means we'll be able to access II the private _node pointer data within the scope of List. friend class List; ListNode *_node; public : iterator(ListNode *theNode ); iterator& operator++ (); int& operator* (); bool operator== (const iterator &rhs ); bool operator!=(const iterator &rhs ); }; II Can use outside as List: :const_iterator type class const_iterator { II Again, this is basically the only situation you should II be using the keyword 'friend' friend class List; ListNode *_node; public : const_iterator (ListNode *theNode ); const_iterator& operator++ (); const int& operator* (); bool operator== (const const_iterator &rhs ); bool operator!=(const const_iterator &rhs ); }; List (); List (const List &list ); List& operator= (const List &list ); -List(); size_t length{)const; int& value (size_t pos ); programminghomeworkhelp.com
  • 8. int value (size_t pos )const; bool empty ()const; iterator begin (); const_iterator begin ()const ; iterator back (); const_iterator back ()const ; iterator end (); const_iterator end{)const; iterator find{ iterator s, iterator t, int needle ); void append{ int theValue ); void deleteAll{ int theValue ); void insertBefore (int theValue, int before ); void insert(iterator pos, int theValue ); void apply(const ApplyFunction &interface ); int reduce(const ReduceFunction &interface )const; void print ()const; void clear(); private : ListNode* node{ iterator it ){return it ._node; } ListNode* node{ const_iterator it ){return it ._node; } }; class ListOutOfBounds : public std ::range_error { public : explicit ListOutOfBounds{) : std ::range_error("List index out of bounds" ){} }; #endif II _6S096_CPPLIST_H programminghomeworkhelp.com
  • 9. #ifndef _65096_CPPLIST_NODE_H #define _65096_CPPLIST_NODE_H class ListNode { int _value; ListNode *_next; ListNode (const ListNode & )= delete; ListNode& operator= (const ListNode & ) delete; public : ListNode (); ListNode (int theValue ); -ListNode (); int& value (); int value ()const ; ListNode* next (); void insertAfter (ListNode *before ); void setNext(ListNode *nextNode ); static void deleteNext (ListNode *before ); static void deleteSection ( ListNode *before, ListNode *after ); static ListNode* create(int theValue = 0 ); }; #endif II _65096_CPPLIST_NODE_H Here are the contents of reduce.h: #ifndef _65096_CPPLIST_REDUCE_H #define _65096_CPPLIST_REDUCE_H #include "list.h" class ReduceFunction { protected : virtual int function(int x, int y )const 0; public : int reduce(const List &list const; virtual int identity ()const 0; virtual -ReduceFunction (){} }; programminghomeworkhelp.com
  • 10. II An example ReduceFunction class SumReduce : public ReduceFunction { int function(int x, int y )const; public : SumReduce (){} -sumReduce (){} int identity ()const {return 0; } }; II Another ReduceFunction class ProductReduce : public ReduceFunction { int function(int x, int y )const; public : ProductReduce (){} -ProductReduce (){} int identity ()const {return 1; } }; #endif II _65096_CPPLIST_REDUCE_H Here is the source code file apply.cpp: #include "list.h" #include "apply.h" void ApplyFunction::apply (List &list )const { for(auto it = list .begin (); it != list.end( ); ++it ){ *it = function (*it ); } } programminghomeworkhelp.com
  • 11. Here is the source code file list.cpp: #include "list.h" #include "list_node.h" #include "apply.h" #include "reduce.h" #include <iostream> List: :List() : _length {0}, _begin {nullptr }, _back{ nullptr }{} List ::List(const List &list ) : _length {0}, _begin {nullptr }, _back {nullptr }{ for(auto it = list.begin (); it != list.end( ); ++it ){ append( *it ); } } List& List ::operator= (const List &list ){ if(this != &list ){ clear(); for(auto it = list .begin (); it != list.end( ); ++it ){ append (*it ); } } return *this; } List: :-List(){clear (); } size_t List: :length ()const {return _length; } int& List: :value(size_t pos ){ auto it = begin (); programminghomeworkhelp.com
  • 12. for(size_t i = 0; i < pos && it != end( ); ++it, ++i ); if(it == end()){ throw ListOutOfBounds (); } return *it; } int List: :value(size_t pos )const { auto it = begin (); for(size_t i = 0; i < pos && it != end( ); ++it, ++i ); if(it == end()){ throw ListOutOfBounds (); } return *it; } bool List: :empty()const { return _length 0; } List: :iterator List: :begin(){return iterator{_begin }; } List: :const_iterator List: :begin()const {return const_iterator {_begin }; } List ::iterator List: :back(){return iterator {_back }; } List ::const_iterator List: :back()const {return const_iterator {_back }; } List: :iterator List: :end(){return iterator{nullptr }; } List: :const_iterator List: :end()const {return const_iterator {nullptr }; } programminghomeworkhelp.com
  • 13. void List ::append (int theValue ){ auto *newNode ListNode: :create(theValue ); if ( empty ()){ newNode->setNext (_back ); _begin = newNode; }else { newNode->insertAfter (_back ); } programminghomeworkhelp.com
  • 14. } void List: :deleteAll (int theValue ){ if( !empty () ){ II Delete from the front while(_begin->value ()== theValue && _begin != _back ){ auto *newBegin = _begin->next (); delete _begin; _begin = newBegin; --_length; } auto *p = _begin; if(_begin != _back ){ II Normal deletion from interior of list for(; p->next () != _back; ){ if(p->next ()->value ()== theValue ){ ListNode: :deleteNext(p ); --_length; }else { p = p->next (); } } II Deleting the last item if(_back->value ()== theValue ){ ListNode: :deleteNext (p ); _back = p; --_length; } }else if(_begin->value () programminghomeworkhelp.com
  • 15. theValue ){ II Deal with the case where we deleted the whole list _begin = _back = nullptr ; _length = 0; } } } List: :iterator List: :find (iterator s, iterator t, int needle ){ for(auto it = s; it != t; ++it ){ if(*it == needle ){ return it; } } return t; } void List: :insert (iterator pos, int theValue ){ auto *posPtr = node(pos ); auto *newNode = ListNode: :create(theValue ); newNode->insertAfter (posPtr ); ++_length; } programminghomeworkhelp.com
  • 16. void List: :insertBefore(int theValue, int before ){ if( !empty () ){ if(_begin->value ()== before ){ auto *newNode = ListNode: :create (theValue ); newNode->setNext (_begin ); _begin = newNode; ++_length; }else { auto *p = _begin; for(; p != _back && p->next ()->value () != before; p p->next ()); if(p != _back && p->next ()- >value ()== before ){ auto *newNode = ListNode: :create (theValue ); newNode->insertAfter (p ); ++_length; } } programminghomeworkhelp.com
  • 17. void List: :apply( const ApplyFunction &interface ){ interface.apply(*this ); } int List: :reduce(const ReduceFunction &interface )const { return interface.reduce(*this ); } void List: :print()const { std ::cout << "{"; for(auto it = begin (); it != back (); ++it ){ std ::cout << *it << " -> "; } if( !empty () ){ std ::cout << *back()<< " "; } std ::cout << "}n"; } void List: :clear (){ for(auto *p = _begin; p != nullptr; ){ auto *p_next = p->next (); delete p; p = p_next; } _length = 0; _begin = nullptr; _back = nullptr; } Here is the source code file list_iterator.cpp: #include "list.h" #include "list_node.h" programminghomeworkhelp.com
  • 18. List ::iterator::iterator (ListNode *theNode ): _node{theNode }{} List: :iterator& List: :iterator: :operator++ (){ _node = _node->next (); return *this; } int& List ::iterator: :operator* (){return _node->value (); } bool List: :iterator: :operator== (const iterator &rhs ){return _node rhs._node; } bool List: :iterator: :operator!= (const iterator &rhs ){return _node != rhs ._node; } List ::const_iterator: :const_iterator(ListNode *theNode ): _node {theNode }{} List: :const_iterator& List: :const_iterator: :operator++(){ _node = _node->next (); return *this; } const int& List ::const_iterator: :operator*(){return _node->value (); } bool List ::const_iterator: :operator== (const const_iterator &rhs ){return _node rhs._node; } bool List: :const_iterator: :operator!= (const const_iterator &rhs ){return _node != rhs._node; } Here is the source code file list_node.cpp: #include "list_node.h" programminghomeworkhelp.com
  • 19. ListNode: :ListNode () : _value {0}, _next {nullptr }{} ListNode: :ListNode (int theValue ): _value{theValue }, _next {nullptr }{} ListNode: :ListNode (){} int& ListNode: :value(){return _value; } int ListNode: :value(){const {return _value; } ListNode* ListNode: :next(){return _next; } void ListNode: :insertAfter(ListNode *before ){ _next = before->next (); before->_next = this; } void ListNode::setNext (ListNode *nextNode ){ _next = nextNode; } programminghomeworkhelp.com
  • 20. delete before->next (); before->_next = after; } void ListNode::deleteSection (ListNode *before, ListNode *after ){ auto *deleteFront = before->next (); while(deleteFront != after ){ auto *nextDelete = deleteFront->next (); delete deleteFront; deleteFront = nextDelete; } } ListNode* ListNode: :create(int theValue ){ return new ListNode{theValue }; } Here is the source code file reduce.cpp: #include "list.h" #include "reduce.h" int ReduceFunction ::reduce (const List &list )const { int result = identity (); for(auto it = list.begin (); it != list.end( ); ++it ){ result = function(result, *it ); } return result; } programminghomeworkhelp.com
  • 21. int SumReduce: :function (int x, int y )const { return x + y; } int ProductReduce: :function (int x, int y )const { return x * y; } Below is the output using the test data: cpplist: 1: OK [0.004 seconds] OK 2: OK [0.005 seconds] OK 3: OK [0.005 seconds] OK 4: OK [0.009 seconds] OK 5: OK [0.006 seconds] OK 6: OK [0.308 seconds] OK 7: OK [0.053 seconds] OK 8: OK [0.007 seconds] OK 9: OK [0.005 seconds] OK 10: OK [0.742 seconds] OK. programminghomeworkhelp.com