PL-Assignment 5- Prolog predicate maxodd Solved

30.00 $ 15.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

Assignment 5

  1.  Write a Prolog predicate maxodd(L,Res) that computes the maximum integer that appears in the odd position of an integer list L and stores the result in Res. Assume that L contains at least 1 element. E.g. |?-maxodd([1,5,3,4,2], Res).

Res = 3  //3 is the maximum number that appears in the odd position of the list [1,5,3,4,2]

  1.  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].


  1.  Write a Prolog predicate replace_first(L, X, Y, Res) that replaces the first occurrences of X in a list L with Y and stores the result in Res. IF L does not contain X, then return L. e.g. | ?- replace_first([2,1,3,1,4,1,5,1,6,1], 1, 6, Res).        Res = [2,6,3,1,4,1,5,1,6,1].
  1. 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].


  1.  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?


  1. [12 points] Question 10.20 Given the following code in C++:

class A{ public:

virtual void p(){cout << “A.p”<< endl;}          void q(){cout << “A.q” << endl;}              virtual void r(){p(); q();}


class B: public A{   public:

void p(){cout << “B.p” << endl;}

}; class C: public B{   public:

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?


  1. [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.