Starting from:

$25

PL Assignment 5-Input/output format Solved

Input/output format (including the function name and the number of arguments) 
 

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]

 

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

        no

 

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

 

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

       No

 

5.   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.r();

a = new C();

a.r();

 

What does the above program print? 

 

6. [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?

 

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.

 

 

 

More products