Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSci 1113: Introduction to C/C++

Programming for Scientists and Engineers

Homework Seven

Fall 2022

Due Date: Friday Dec. 2, 2022 before 11:59pm.

Instructions: This is an individual homework assignment. There are two problems worth 20    points. Solve the problem below by yourself (unlike the labs, where you work collaboratively), and submit the solution as a C++ source code file. Here are a few more important details:

1. Unlike the computer lab exercises, this is not a collaborative assignment.

a. You must design, implement, and test the solution to each problem on your own without the assistance of anyone other than the course instructor or TAs.

b. You may use examples from the textbook, lectures, and code you and your partner wrote to solve lab problems.

c. You may not include solutions or portions of solutions obtained from any other  source. This includes Chegg, stackoverflow, etc. We check these sites against all submitted solutions.

d. Gradescope also has a similarity checker which automatically compares your submitted solution to all other submitted solutions.

e. Otherwise obtaining or providing solutions to any homework problems for this class is considered academic misconduct. See the Scholastic Conduct section in the syllabus in Canvas for more details, and ask the instructor if you have questions.

f. Follow the naming conventions strictly.

g.  No late assignments will be accepted.

h.   If a program doesn’t compile, the score will be a zero.

2.   Because all homework assignments are submitted and tested electronically, the following are important:

You follow the naming conventions mentioned for the problems.

You submit the correct file(s) on Gradescope ( https://www.gradescope.com/ ) by the due deadline.

You follow the example input and output formats exactly given in each problem description.

Regardless of how or where you develop your solutions, your programs compile and execute on gradescope computers (which run Linux/Ubuntu operating system like the cselabs machines).

3.   The problem descriptions will usually show at least one test case and the resulting correct output. However, you should test your program on other test cases (that you make up) as well. Making up good test cases is a valuable programming skill and is part of ensuring   your code solution is correct.

Problem A: Creating a Class (40 points)

Download the given file BeanCounter.hpp. This is a header file that describes the class that you are to build in this problem. Your task will be to implement all the functions      described there in your hw7.cpp file. You must implement the class as described here.

Dont forget that you will need to have the line

#include "BeanCounter.hpp"

the header file at the top of your hw7.cpp file.

BeanCounter is a class that represents the information that employees at Acme Bean   Counting corporation must track. At Acme Bean Counting Corporation (which will be     referred to as ABC Corp from here forward,) newly-hired employees count a single type of bean, the common kidney bean (referred to as Bean 0 from here forward.)

These employees use a simple counter with a button that ticks” the counter up by one every time it is pressed.

After an employee has mastered the skills of counting a single bean type and has been at ABC Corp long enough, they are rewarded with promotion! Promotion means             additional responsibilities. Now an employee must count lima beans (Bean 1) as well as continuing their duties counting Bean 0. These employees are issued a counter that can count two different types of things, and they need to keep them separate.

As employees get promoted, they get bigger and bigger counters to help them track all of their beans.

ABC Corp deals with hundreds of different bean types, and only the most senior employees count them all.

In this homework, we will write a class that will become part of the software system to  digitize the bean counters used at ABC Corp. ABC Corp is planning to run their system on some really old computers, so memory savings is a big issue.

The BeanCounter class uses a dynamic int array to hold the data for the counters. The  indexes in the array can be used to correspond to the bean types. When a BeanCounter object is created, the constructor may be passed a parameter that tells it how many        indexes that the int array should have.

Here are the functions and constructors that are in the BeanCounter.hpp file that you need to implement, along with some sample code to test if these are working:

Hint: If you implement these functions in the order they are listed here, you will have an easier time testing!

●   BeanCounter(int n) - Constructor that creates a BeanCounter object and initializes its counter array to have n indexes.

●   BeanCounter() - Constructor that creates a BeanCounter object and initializes its counter array to have 2 indexes.

●   void tick(int i) - Add one to counter index i, if that is a legal index. If i is not a legal index, do nothing.

●   friend ostream& operator<<(ostream& outs, const BeanCounter& b) - Overloaded << operator. It should format the counter values in the form

Once these four functions are implemented properly, you should be able to see the following output from this sample code:

BeanCounter b(4);

b.tick(1);

b.tick(0);

b.tick(1);

b.tick(2);

cout << b << endl;

BeanCounter other;

other.tick(0);

other.tick(0);

other.tick(0);

other.tick(1);

other.tick(0);

other.tick(1);

cout << other << endl;

Should output:

<4, 2>

Next, implement the following member functions:

●   maxReset() - This function should reset all indexes to 0, except the one with the largest value. If there are multiple indexes with the same largest value, then all of them should be retained.

●   BeanCounter operator+(const BeanCounter& first, const          BeanCounter& second) - overloaded + operator. This should create a new      BeanCounter object that has as many counter indexes as the largest of the two input BeanCounters, and then add the corresponding index values together.

Continuing from the previous example:

BeanCounter c = b + other;

cout << c << endl;

c.maxReset();

cout << c << endl;

Should output:

<5, 4, 1, 0>

<5, 0, 0, 0>

Finally, implement the proper support for the dynamic memory:

BeanCounter(const BeanCounter& b) - copy constructor

~BeanCounter() - destructor

void operator=(const BeanCounter& b) - assignment operator

Because our class uses a dynamic array, we need to implement these three functions. The copy constructor must correctly copy data into a new BeanCounter object to         support call-by-value, the destructor must correctly release memory, and the                assignment operator must correctly support assigning one BeanCounter variable to     another. Make sure to check for the case of assigning an object to itself.

Also, note that the assignment operator overload (unlike the overloaded operators + and <<) actually is a member function of the class, so you need to define it as                        BeanCounter::operator= in your .cpp file.

In order to test the copy constructor and destructor, you will need to use call-by-value for a BeanCounter object in a function. Here is a possible example:

void tick3(BeanCounter b) {

b.tick(3);

b.tick(2);

b.tick(1);

cout << "In the tick3 function: " << endl;

cout << b << endl;

}

int main() {

BeanCounter b(4);

b.tick(0);

b.tick(1);

b.tick(2);

cout << b << endl;

tick3(b);

cout << b << endl;

}

Which should output the following:

<1, 1, 1, 0>

In the tick3 function: <1, 2, 2, 1>

<1, 1, 1, 0>

To test the assignment operator, you will need to write your own test code.

Importantly, when you submit, make sure that your program does not have a main function. Either delete it or comment it out. We will use our own main function to test your code.

When you are done, name the source code file hw7.cpp. Then log into gradescope and upload your file for the “Homework 7” submission. If you name your file incorrectly it will be unable to compile and run your code, so you will fail all test cases. You may submit cpp files as many times as you want until the deadline   to try and fix the code if you fail a test case. Following rigorous naming conventions and using test cases are something computer programmers often must do in real life”         programming, and so submitting your program with the correct name and functionality is part of doing this assignment correctly.