Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

ProLog Program to read 10 integers in a list and 10 from the database and find 2nd maximum of all of them

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 3420

Write a complete prolog program to read 10 integers in a list and 10 from the database and find 2nd maximum of all of them.

Code for ProLog Program to read 10 integers in a list and 10 from the database and find 2nd maximum of all of them in Artificial Intelligence

%trace
domains
    l=integer*
database
    db(integer)
predicates
    start
    readList(l,integer,l)
    printdb
    readdb(integer)
    marge(l,l)
    findMax(l,integer,integer)
    findSecondMax(l,integer,integer,integer)
clauses
    start:-
        write("Enter the 10 list data"),
        readList([],1,New),
        write(New),
        readdb(1),
        printdb,
        marge(New,Concat),
        write(Concat),
        findMax(Concat,0,Max),
        findSecondMax(Concat,0,Max,SecondM),
        write("Second max is::",SecondM).
    readList(List,N,New):-
        N < 4,
        write("\nEnter the ",N," value::"),
        readint(Val),
        NN=N+1,
        Fr = [Val|List],
        readList(Fr,NN,New).
    readList(List,4,List).
    readdb(N):-
        N < 4,
        write("Enter the ",N," value::"),
        readint(Val),
        assert(db(Val)),
        NN=N+1,
        readdb(NN).
    readdb(4).
    
    printdb:-
        db(Val),
        write(Val),
        fail.
    printdb.
    marge(List,Concat):-
        retract(db(Value)),
        New = [Value| List],
        marge(New,Concat).
    marge(List,Concat):-
        Concat = List.
    findMax([Head|List],Max,M):-
        Head > Max,
        findMax(List,Head,M).
    findMax([Head|List],Max,M):-
        findMax(List,Max,M).
    findMax([],Max,Max).
    findSecondMax([Head|List],Zero,Max,SecondM):-
        Head < Max and Head > Zero,
        findSecondMax(List,Head,Max,SecondM).
    findSecondMax([Head|List],Zero,Max,SecondM):-
        findSecondMax(List,Zero,Max,SecondM).
    findSecondMax([],Zero,Max,Zero).
  
Share: 



Milind Mishra
Milind Mishra author of ProLog Program to read 10 integers in a list and 10 from the database and find 2nd maximum of all of them is from India.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in Artificial Intelligence:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!