Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Create a directory ~/UnixCourse/makeAsst . Within that directory, create a pai

ID: 3917950 • Letter: 1

Question

1.Create a directory ~/UnixCourse/makeAsst. Within that directory, create a pair of subdirectories, project1 and project2

Copy the .h and .cpp files from your earlier “Compilation” assignment (should be in ~/UnixCourse/compileAsst) into your project1 directory. (~/UnixCourse/compileAsst/guess.cpp ~/UnixCourse/compileAsst/yesno.h ~/UnixCourse/compileAsst/yesno.cpp)

Copy all files in the ~cs252/Assignments/cookie/ directory into your project2 directory. (~cs252/Assignments/cookie/chompt.adt ~cs252/Assignments/cookie/mainProg.cpp)

2.In project1, create a makefile that will compile guess.cpp and yesno.cpp to produce the files guess.o and yesno.o, and will link those two .o files to produce an executable program named guess.

This should occur in 3 discrete steps (compiling guess.cpp, compiling yesno.cpp, and linking the results).

You may produce your makefile “from scratch” or as a modification of my self-updating makefile for single-program projects.

Verify for yourself that the make command (with no arguments) does produce the desired program. (You can do this by issuing the make command directly at the command line, or via emacs’ M-x compile command.)

Once you have successfully got make to build the project, run it again. Note that make realizes that the project is already up-to-date and does not rerun any of the compilation commands.

One big reason that we use make is because, on large projects consisting of many, many files, we want to avoing recompiling everything after changing just one or two files. make looks at what files have changed (by checking their modification dates - the dates you see when you do a ls -l command) and figures out the minimal number of steps required to rebuild the project based upon what has actually changed.

Now make a trivial change to either guess.cpp or yesno.cpp, but not both. For example, add a blank line at the beginning or end of the file. Then run make again and verify that your makefile really does recompile only the changed file.

You can simulate a change to a file without actually changing its contents via the touch command, which alters a file’s modification date without altering the contents of the file. For example, if you do

touch guess.cpp

and then run make again, it should recompile guess.cpp, but not yesno.cpp.

3.The code in project2 is for a program that plays a simple game called Chomp.

The programmers of this project have opted to package some of their code in a “module” called chomp.adt, from which the related files cookie.h and cookie.cpp files can be generated.

The steps necessary to produce this program are:

A. Run the command : csplit chomp.adt "/Split Here/"

and copy the resulting file xx00 to cookie.h

B. Run the command: csplit chomp.adt "/Split Here/"

and copy the resulting file xx001 to cookie.cpp

C.Compile cookie.cpp to produce cookie.o.

D.Compile mainProg.cpp to produce mainProg.o

E.Link the the .o files to produce an executable program named playChomp

Write a makefile that will carry out these steps. Your makefile should result in only the minimum required amount of steps when any input file to this process is changed. (Note: you will probably not be able to base this makefile upon my self-updating makefile as in the earlier part of the assignment. Instead, you will probably find it necessary to write this one from scratch.

Explanation / Answer

guess.cpp


#include <iostream>
#include <string>
#include "yesno.h"

using namespace std;


bool doGuesses (int low, int high)
{
if (low >= high)
    {
      cout << "Hey, no cheating! There is no number that is ";
      return false;
    }
else if (high == low + 2)
    {
      cout << "I'm guessing that your number is " << low+1 << endl;
      cout << "Is that right? " << flush;
    
      string response;
      getline(cin, response);
      if (yesNo(response))
   {
      cout << " Hooray! (Oops. Sorry - I don't mean to gloat.)" << endl;
      return true;
   }
      else
   {
      cout << "Hey, no cheating! There is no number other than "
           << low+1 << " that is ";
      return false;
   }
    }
else
    {
      int mid = (low + high) / 2;
      cout << "Is your number bigger than " << mid << "? " << flush;
      string response;
      getline(cin, response);
      if (yesNo(response))
   {
      if (doGuesses(mid,high))
        return true;
      else
        {
          cout << " bigger than " << mid << ", ";
          return false;
        }
   }
      else
   {
      if (doGuesses(low,mid+1))
        return true;
      else
        {
          cout << " smaller than " << mid+1 << ", ";
          return false;
        }
   }
    }
}


int main()
{
cout << "Think of an integer between 1 and 1000. I will try to guess it."
       << endl;

bool result = doGuesses(0, 1001);

if (!result) {
    cout << " and is between 1 and 1000." << endl;
    cout << " I demand a rematch!" << endl;
}

return 0;
}

yesno.cpp

#include "yesno.h"

using namespace std;

bool yesNo(string response)
{
if (response.length() == 1)
    {
      return (response == "Y") || (response == "y");
    }
else if (response.length() == 3)
    {
      return (((response[0] == 'Y') || (response[0] == 'y'))
          && ((response[1] == 'E') || (response[1] == 'e'))
          && ((response[2] == 'S') || (response[2] == 's')));
    }
else
    return false;
}


yesno.h

#ifndef YESNO_H
#define YESNO_H

#include <string>

bool yesNo (std::string response);


#endif