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

EECS 281: Data structures and Algorithms

Spring 2023

Lab 1:  Tools for EECS 281

1    Logistics

1. what is the due date of project 1?

A. 9/9/2023

B. 9/19/2023

C. 9/21/2023

D.  10/19/2023

2. what is the date of the Midterm?

A.  10/17/2023

B.  10/18/2023

C.  10/19/2023

D.  10/20/2023

3. what percentage of your grade is each project worth?

A.  1

B. 5

C.  10

D. 20 

2    Tools

4. what is the correct login string for CAEN?

NOTE:

In this class, when we want to indicate that you should use a variable but we are writing text in a document, we will surround the variable with < and >.  For example, for option A in the following answers, Dr. paoletti,s personal login string would be [email protected], because he would replace <uniqname> with his uniqname (paoletti).

A. ssh <uniqname>@login.engin.umich.edu

B. ssh <uniqname>@engin.umich.edu

C. ssh <uniqname>@caen.umich.edu

D. ssh <uniqname>@umich.edu

5. why do we use Makeiles?

A. They can create the submission iles automatically.

B.  They automate the (sometimes long and complex) compilation process for us. C. They can run custom testing scripts.

D.  Typing out compilation commands every time is annoying.

E. All of the above.

6. will your program compile on the autograder without the project identiier? A. yes.

B. NO, and I,ll potentially waste the submission.

7. what is the Makeile command to create a submission ile that includes custom test cases?

A. make

B. make  all

C. make  partialsubmit

D. make  fullsubmit

8.  which of the following is a debugging tool?

A. Makefile

B. perf

C. valgrind

D.  cin

9. what does perf do?

A.  Detects segmentation faults in a program and displays line numbers that they occurred at.

B. proiles program execution time.

C. proiles program memory usage.

D.  Automates the testing process.

1O. You have to use perf on CAEN to complete the following question.  If you are having trouble logging on to CAEN, please contact a staf member or email [email protected].  After successfully connecting to CAEN, follow the instructions below:

. visit the lab 1 repository on the EECS 281 GitHub: https://github.com/eecs281staf/lab- O1-music-sorting.

.  Retrieve the contents of the repository.  There should be two folders:  mu网ic orting and perf profiling . In the perf proiling folder, ind the profiling.cpp ile .

.  Compile the code on CAEN (with make, the Makeile is already supplied in that folder ready to go, you can run make sync2caen with the Makeile to move your folder iles to caen) and then use the perf tool (on CAEN) to proile the program.  Note that running proiling (including the perf command) will probably take around 1O seconds, so don,t panic if it seems like it,s not doing anything.

.  once you login to CAEN, you,ll want to load in GCC permanently.  If you already have a  .ba网h profile ile in your home directory on CAEN with the module  load gcc/11.3.0 command in the ile, you can skip this step.  otherwise, run the following commands on CAEN:

cd   ~

touch   . bash profile

echo   " module   load  gcc /6.2.0 "  >>   . bash profile

source   ~/. bash profile

.  Type the following commands to build the program, time it, and display the results (be sure that you use make  debug and  ./profiling debug,  otherwise  you will get incorrect results):

make   debug

perf  record   -F   1000   -- call -graph  dwarf   -e   cycles : u   ./ profiling 一 debug   <  test -1. txt

perf  report

Answer the following two questions based on the results of the process above.  Choose the best answer for both questions.  You may have to make your terminal window wider to see the full function name(s).   Both  questions  are worth O.5 points each.   when  you,re done, make sure to remove the perf.data ile: it can be up to 1OO MB in size!  The make clean command included in the lab iles will remove the executable, object iles, and the perf.data ile.

(a)  Approximately what percentage of time was spent in the in网ertionsort() function? Make sure that you are looking at the elf column of the output.

A.  1O%

B.  2O%

C. 4O%

D.  6O%

E. 8O%

(b)  Approximately what percentage of time was spent running the vector subscript oper-

ator (it will appear as 网td::vector<...>::operator[])? Again, use 网elf data.

A.  1O%

B.  2O%

C. 4O%

D.  6O%

E. 8O%

3    Debugging

we have found in previous semesters that students entering 281 are not comfortable in their development environments, speciically with using git, gcc/clang, make, valgrind and debuggers.   These  tools and more are vital for success in projects and labs in 281,  but also in future EECS courses you may take, and in industry.  This assignment uses small toy programs to help you get more comfortable and hopefully learn a thing or two before jumping into more complex projects.   For each question, assume that the given code is compiled according to the lags given in the Makeile (shown below), under the CAE笑 Linux environment.

Assume that all the programs are complete and that no other source iles exist.

11. what is wrong with the following program?

int  add(int  a ,  int  b ;

int  main() {

int  x  =   1;

int  y  =  5;

return   addx ,  y ;

}

A.  add() has no deinition.

B.  add() is called with x and y, but accepts a and b.

C.  The program does not compile because main() cannot return a value of 6. D. A function cannot be called after a return statement.

E. Nothing is wrong with this program.

12. what is wrong with the following program?

int  main() {

int  x  =  0;

return  x  +  y ;

}

A. main() cannot return the result of an arithmetic expression.

B. main() cannot return x  +  y because x  +  y is a double.

C. main() does not contain a declaration for y.

D. main() cannot return an integer.

E. Nothing is wrong with this program.

13. what is wrong with the following program?

# include   < vector >

int *  get-some-ints() {

std :: vector < int >  ints  =  1 ,  2 ,  3 ,  4 ,  5};

return   ints . data();

}

int  main() {

int *   some-ints  =  get-some-ints();

delete []   some-ints ;

return  0;

}

A.  The memory pointed to by some ints is freed twice .

B. main() leaks the memory pointed to by some ints .

C. A function cannot return a pointer.

D.  some ints is a pointer and not an array, so delete should be used instead of delete[].

E. Nothing is wrong with this program.

14. what is wrong with the following program?

struct  Thing  {  };

int  main() {

Thing  a ;

Thing  b ;

bool   less  =  a  <  b ;

}

A. main() cannot declare an instance of a Thing object.

B. main() must have a return value.

C. Thing cannot have an empty deinition, so this code does not compile.

D. main() tries to use the < operator, which is not deined for the Thing type. E. Nothing is wrong with this program.

15. what is wrong with the following program?

int  main() {

int   some-ints [5]  =  {  1 ,  2 ,  3 ,  4 ,  0  };

for (int *  p  =   some-ints ;  p ;  ++ p)

* p  =  0;

return  0;

}

A. main() indexes out of the bounds of some ints .

B. main() leaks the memory pointed to by some ints .

C.  some ints points to uninitialized memory.

D.  The for loop is missing a curly brace, so the code will not compile. E. Nothing is wrong with this program.


16. what is wrong with the following program?

# include   < iostream >

int   sum-ints() {

int  * some-ints  =  new   int [50];

for (int  i  =  0;  i  <  50;  ++ i  {

some-ints [ i ]  =  i ;

}

int   sum  =  0;

for (int  i  =  0;  i  <  50;  ++ i  {

sum  +=   some-ints [ i ];

}

return   sum ;

}

int  main() {  std :: cout   <<  sum-ints();  }

A.  sum ints() indexes out of the bounds of  some ints .

B.  sum ints() leaks the memory pointed to by some ints .

C.  some ints points to uninitialized memory.

D. main() must have a return value.

E. Nothing is wrong with this program.

17. what is wrong with the following program?

int  factorial(int  n  {  return  n  *  factorial(n  -   1);  }

int  main() {  return   factorial(3) -  6;  }

A. factorial(3) returns an uninitialized value.

B. A mathematical operation cannot follow a return statement.

C.  factorial(3) never returns (and may cause a stack overlow).

D. main() cannot return the result of a recursive function.

E. Nothing is wrong with this program.

18. what is wrong with the following program?

# include   < iostream >

int  what-is- 2x281() {

int  x ,  y  =  281;

return  x  +=  y ;

}

int  main() {  std :: cout   <<   " what   is  2  x  281?\ n "  <<  what-is- 2x281();  }

A.  The  += operator cannot be used  after  a  return  statement,  since  x  would be

returned before being added to y.

B. what is 2x281() returns an uninitialized value.

C. The type of y is not deined.

D. main() must have a return value.

E. Nothing is wrong with this program.


19. what is wrong with the following program?

void  takes-an-integer(int  x ) {}

int  main() {

size-t  x ;

std :: cin   >>  x ;

takes-an-integer(x ;

}

A.  The code does not compile because takes an  integer() has an empty deinition . B. main() must have a return value.

C. takes an integer() takes an int but is called with a size t, which may cause  a loss of precision.

D.  size t is not a valid type .

E. Nothing is wrong with this program. 

4    File Input & output

2o.  This code applies to the next two questions.

int  main(int  argc ,  char []*   argv  {

int  i ;

//  Read   in  an   integer   from  a  file .

}

(a) which line(s) of code would read an integer from a ile using ile redirection? A.  if网tream readfile; readfile  >>  i;

B.  cout  <<  i;

C.  cin  >>  i;

D. of网tream writefile; writefile  <<  i;

(b) which of the following command line commands would run the above program  (in main.cpp, already compiled to the executable main) with input ile redirection?

A. make  all

B.  ./main  input file.txt

C.  ./main  >  input file.txt

D.  ./main  <  input file.txt

21. A text ile contains a list of words that are all on separate lines, with only a trailing newline after every word.  one program, program T1, reads the ile with cin  >>, reading in to a tring, S1.  Another program, program T2, reads the ile with getline, also reading into a tring, S2.

(a) what will be the diference, if any, between the two resulting tring variables? A.  S2 will contain a trailing space, while S1 will not.

B. Both strings will be exactly the same.

C.  S1 will have a newline character at the end, while S2 will not. D. T1 will not read the ile.

(b) what if there is a space at the end of each word in the ile?

A.  S2 will contain a trailing space, while S1 will not.

B. Both strings will be exactly the same.

C.  S1 will have a newline character at the end, while S2 will not. D. T1 will not read the ile.

5    Getopt

22. You,re writing a simple text-based game on the command line.  when the user runs your executable ( ./281quest) the game starts at the irst level.  If the user wants to skip to a certain level, they can instead run the program with the command  ./281quest  ––level 5 (where ––level is followed by the level they want to skip to).  which of the following correctly speciies this lag for getopt?

A.  {"level",  no argument,  nullptr,  ,l,  }

B.  {"level",  optional argument,  nullptr,  ,l,  }

C.  {"level",  required argument,  nullptr,  ,l,  }

23.  short options You,re writing a program that will take ive command line options:  -p - paoletti, -d -darden, -a -angstadt, -m -markov, and -g -garcia.  options d, m, and g will have required arguments.  The rest take no arguments. which of the following strings is a correct short options string?

A. pd:am:g

B. p:d:a:m:g

C. p:dam:g:

D. pd:am:g:

E. p:da:mg

6    Handwritten problem

This problem is to be submitted independently.  we recommend trying it on your own, checking your answer with your group and discussing solutions, and then submitting it to Gradescope. These will be graded on completion, not by correctness. However, we want to see that you were thinking about the problem.

writing your solution by hand is good practice for the exams and therefore strongly rec- ommended. You may submit your solution to Gradescope in the form of a PDF, image, or similar. You may also submit your solution to Gradescope in the form of a .cppile. For this lab, if you implement your solution in palindrome.Cpp and submit this ile to Gradescope, the Gradescope autograder will provide feedback.

Your grade does not depend on the format of your submission or the feedback from the autograder. Your solution is graded solely on completion and not by correctness.

The starter iles can be found on canvas.

24. Linked-List Palindrome Given the start and end nodes of a doubly linked list, determine if the values create a palindrome.  A palindrome is a sequence that is the same backwards and forwards.  see the deinition of the Node struct below, or in palindrome.h.  Return true if the list is a palindrome.

struct  Node  {

Node *  prev ;

Node *  next ;

char  value ;

}

bool   isPalindrome(Node *  start ,  Node *  end ;

7    coding Assignment

You can work on these problems by yourself or with your group, but a solution must be submitted to the autograder for each individual.

For this lab, you will be familiarizing yourself with the get opt function, as well as our Make-

ile and the autograder. To accomplish this task, goto the music sorting directory from the

repository you retrieved from Canvas or GitHub (https://github.com/eecs281staf/lab-O1-

music-sorting). To clone the directory on to your local machine, goto your terminal and type

the command git  clone  https://github.com/eecs281staff/lab–01–music–sorting.git.

There should be two iles present,  sorting.h and lab1.cpp.   There is nothing for you to do in sorting.h, but you need to have it in the same directory as lab1.cpp for the code to compile. Make sure to step through lab1.cpp to ind and complete all the TODO statements. There is one test ile for you to test your code on in the folder.

lab1.cpp contains a deinition for a MusicLibrary object.  This object will read a Csv ile, put the entries into a vector, and then sort the vector and print the top n songs based on several command line options that will be processed with get opt.  Carefully read the comments and observe what is happening in the code;there are a lot of concepts contained in the functionality that will help you later in the course.

sorting.h contains the song deinition, as well as an overloaded operator<< for printing the songs. please look around the ile, but there,s nothing that you have to do in the code.

Once you have completed the coding portion, take a look at the Makeile. There are a few TODO statements in the Makeile as well.  If the Makeile is confusing, don,t worry; we will go over it in lab.  Once you have a grasp of how to use it, compile your code and use the command make  fullsubmit.

Finally, submit your code to the autograder. when you run the make  fullsubmit command in the Makefile, there will be a tarball in the directory that contains all of the iles.  Go to the autograder, click‘choose ile, under Lab 1, and ind the tarball in your ile system. Then upload your submission and watch as the test cases run!

Make sure that all code iles submitted to the autograder include the assignment identiier listed on the irst page of this assignment, or below.

project Identiier:   CD7E23DEB13C1B884OF765F9O16CO84FD5D3F13O