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


ECS 36C: Programming Assignment #1


1 Changelog

You should always refer to the latest version of this document.

● v.1: Initial version.

● v.2:

– Autograder details.

– Mentioned that – if you wish – you could write your entire implementation in array.hpp and not submit array.inl .

● v.3: Fixed a typo that originally said you are to submit three files instead of two files.


2 General Submission Details

Partnering on this assignment is prohibited.   If you have not already, you should read the section on academic misconduct in the syllabus.

This assignment is due the night of Sunday, 01/16. Gradescope will say 12:30 AM on Monday, 01/17, due to the “grace

period” (as described in the syllabus). Be careful about relying on the grace period for extra time; this could be risky.

* This content is protected and may not be shared, uploaded, or distributed.


3 Purpose of This Assignment

● To help you review templates and other C++ features that you should know for this course.

● To get you into the mindset of implementing a data structure (albeit, a very simple one in this case), since – throughout this course – we will see implementations of different data structures.

● To show you how exactly compile-time index range checking (which std::array from the C++ STL supports) can be done.  You have seen that sequential STL containers  (e.g.   std::vector) that support indexing do so through either operator[]  or the at()  method.  The operator[]  does no index range checking, whereas at()  does runtime  index range checking.  std::array takes it a step further by supporting compile-time  index range checking, and – by making your own, smaller version of std::array in this assignment – you will see how that is done.


4 Reference Environment

The autograder, which is talked about at the end of this document, will compile and run your code in a Linux environment. That means that you should test your code in a sufficiently similar environment.  The CSIF is one such environment. Each student should have remote access to the CSIF. (I talk more about the CSIF in the syllabus.)

You should avoid causes of undefined behavior in your code (i.e.  you should avoid things that make your code behave differently in one environment/context vs. another), such as uninitialized variables. If you have things like this in your code, then your code could end up generating the wrong answer when autograded, even if it generated the right answer when you tested it in an appropriate environment.


5 Programming Problems

Your code for this assignment must be written in C++.

Hopefully, in at least one of your prerequisite courses, the instructor encouraged you to learn how to use a debugger, regardless of the programming language.  There are many debuggers that you can use for C++.  One that is already on the CSIF (and should be on or easily installable on any other Linux environment) is GDB (gdb).  GDB is a command-line debugger, and some may not like that, so there is also DDD, a GUI version of GDB. Learning to use a debugger takes some time (just like learning to use the Linux command line), but once you are sufficiently experienced with a debugger, it becomes much easier to find many kinds of bugs.  As mentioned in the very first Canvas announcement, I uploaded my lecture slide deck that talks about how to use GDB, just in case it helps.

5.1 File Organization

You will submit two files for this assignment:

● array.hpp

● array.inl

If you wish, you can write your entire implementation in array.hpp  and not submit array.inl .


6 Templated Array

6.1 Static Assertions

Recall that the assert()  function1 can be used to check if a certain condition is true during runtime and crash if the condition is false. We may refer to this as a dynamic assertion.

In contrast, static_assert()  performs a static assertion2.  This means that static_assert()  checks if a condition is true during compile time and causes a compilation error if the condition is false. Because the check is done during compile time, this allows static assertion conditions to involve values that are known at compile time.  This is useful if you are trying to impose certain constraints on template parameters.

Below is an example. Notice that main2.cpp cannot be compiled due to the violation of the static assertion.

1 In C, this is in the  header. In C++, this is in the  header.

2 Here, “static” means “without running the program”, i.e. during compile-time, whereas “dynamic” means “while running the program”. You see these terms “static” and “dynamic” from time to time, e.g. with static profilers vs. dynamic profilers.

1    s   cat   test . hpp

2    template   < typename T , int Val >                                                                                                                                                                                           3    class   Test                                                                                                                                                                                                                                            4    {                                                                                                                                                                                                                                                                 5    public :

6                    //   For   no   reason ,   let ’ s   prevent   @val   from   being   2.

7                    //   ( Note :   I   don ’ t   think   it   matters   too   much   where   this   static   assertion

8                    //   is   placed .)

9                    s tati c _a s se rt ( Val   !=   2 ,   " Val   cannot   be   2.") ;

10

11                   Test ()   =   default ;

12    };

13    s   cat   main1 . cpp

14    # include   " test . hpp "

15

16     int   main ()

17    {

18                   Test < float , 5 >   tmp ;

19    }

20    s  g ++   - Wall   - Werror   - std = c ++14   main1 . cpp   - Wno - unused - variable

21    s   cat   main2 . cpp

22    # include   " test . hpp "

23

24     int   main ()

25    {

26                   Test < float , 2 >   tmp ;

27    }

28    s  g ++   - Wall   - Werror   - std = c ++14   main2 . cpp   - Wno - unused - variable

29    In   file   included   from   main2 . cpp :1:

30    test . hpp :   In   i ns t an ti a ti o n   of   ’ class   Test < float , 2 > ’:

31    main2 . cpp :5:20:        required   from   here

32    test . hpp :8:23:   error :   static   assertion   failed :   Val   cannot   be   2.

33                   8   |             s ta t ic _a s se r t ( Val   !=   2 ,   " Val   cannot   be   2.") ;

34                             |                                               ~~~~^~~~

35    s

Note:  Once the new C++ feature called concepts comes out, that would apparently take the place of the combination of templates and static assertions.

6.2 Goal: Implementation of Templated Array

You should start with the array.hpp  and array.inl  files that are on Canvas. In this assignment, your goal is to implement all of the methods that are declared in the definition of the templated Array class. There are also non-member functions below the definition of the class that you must define too.

You are  submitting both the header file and the source file for this assignment, which means that you  are  allowed to modify the header file.

Below are some tips/suggestions:

● It is possible to avoid using dynamic memory allocation entirely in this assignment.

● Code that involves templates may seem intimidating and more complicated than it actually is. It may help if – in your head – you imagine that T is int while you are writing the code for the methods.


6.3 Restrictions

You are not allowed to use any STL type, e.g.  std::array , std::vector, as that would totally defeat the point of the Array class. You are not allowed to use a linked list as the underlying implementation either; you should be using a C-style array in the underlying implementation.  (In this assignment, you are making your own version of the std::array class.)

There are certain operations that you can and cannot assume that T (the type template parameter given to Array) supports. You may assume the below operations are supported by T. (There might be some obvious ones that I missed, so feel free to ask me if you come across such operations that you think should be in this list.)

● Default constructor.

● Copy constructor.

● Copy assignment operator (=).

● ==

●  !=

● operator<< (That is, you can print out an element of type T with, for instance, std::cout.)