CS571 Assignment 5 solved


Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment


5/5 - (1 vote)

1. [10 points] Write a Prolog predicate max(L,Res) that computes the maximum number in an integer list L and
stores the result in Res. Assume that L contains at least 1 element.
E.g. |?-max([1,3,5,4,2], Res).
Res = 5 //5 is the maximum number in the list

2. [15 points] Define a Prolog predicate deleteNList(N,L,Res) that delete every Nth argument of a list : and store
the results in Res.
E.g. | ?- deleteNList(3, [2,3,4,5,6,7,8], Res).
Res = [2,3,5,6,8].

3. [20 points] Write a Prolog predicate replace_first_k(K,L, Res) that replaces the first K occurrences of
1 in a list L with 8 and stores the result in Res.
e.g. | ?- replace_first_k(3, [2,1,3,1,4,1,5,1,6,1] , Res).
Res = [2,8,3,8,4,8,5,1,6,1].

4. [15 points] Write a Prolog program position(X, L, Res) that takes an integer X and an integer list L, returns a list
of positions of X in L. The result is stored in Res.
e.g. |?- position(1, [1,3,1,2,5,1], Res).
Res = [1,3,6].

5. [12 points] Given the following code in Java
public class A
{ public void p() { System.out.println(“A.p”);}
public void q() { System.out.println(“A.q”);}
public void r() { p(); q();}
class B extends A
{ public void p() { System.out.println(“B.p”);}
class C extends B
{ public void q() { System.out.println(“C.q”);}
public void r() { q(); p();}

A a = new B();
a = new C();
What does the above program print?

6. [12 points] Question 10.20 Given the following code in C++:
class A{
virtual void p(){cout << “A.p”<< endl;}
void q(){cout << “A.q” << endl;}
virtual void r(){p(); q();}
class B: public A{
void p(){cout << “B.p” << endl;}
class C: public B{
void q(){cout << “C.q” << endl;}
void r(){q(); p();}

A a; C c; a = c; a.r();
A* ap = new B; ap -> r();
A* ap1 = new C; ap1 -> r();
What does the above program print?

7. [16 points] Question 10.48
Class A
{ public:
virtual void f();
virtual void g();
private: int a;
class B: public A
{ public:
void f();
void h();
private: int b;
Class C: public B
{ public: void g();
Private: int c;

Draw the VMT of each class and the layout of memory for a dynamically-allocated object of each class.

Instruction of Submission:
• Create a directory [userid]_5 (e.g. pyang_5), which contains
o Prolog program assignment5.P
o A .pdf file containing the name and email address of the group members, and solutions to Q5-Q7
• Tar the contents of this directory using the following command.
tar –cvf [directory_name].tar [directory_name]
E.g. tar -cvf pyang_5.tar pyang_5/
• Upload the tared file you created above to mycourses.

Academic Honesty:
All students should follow Student Academic Honesty Code (http://watson.binghamton.edu/acadhonorcode.html).
All forms of cheating will be treated with utmost seriousness. You may discuss the problems with other students,
however, you must write your OWN codes and solutions. Discussing solutions to the problem is NOT acceptable.

Copying an assignment from another student or allowing another student to copy your work may lead to an automatic F for this course. If you borrow small parts of code/text from Internet, you must acknowledge this in your
submission. Also, you must clearly understand and be able to explain the material. Copying entire material or large

parts of such material from the Internet will be considered academic dishonesty. Moss will be used to detect plagiarism in programming assignments. You need ensure that your code and documentation are protected and not accessible to other students. Use chmod 700 command to change the permissions of your working directories before you
start working on the assignments. If you have any questions about whether an act of collaboration may be treated as
academic dishonesty, please consult the instructor before you collaborate.