Tutorial 2 of Artificial Intelligence



1.     Write program to display digits 1 to 9 also in reverse order using recursion to understand winding down.

predicates
     count(integer)
     go

clauses
     go:-
          write("Winding Down"),nl,
          count(1),
          write("1"),nl.
    
     count(9):-
          write("9"),nl,
          write("Now unwinding"),nl.
         
     count(N):-
          write(N),nl,
          A = N+1,
          count(A),
          write(A),nl.

  2.    Write a program to perform login operation using:
i.                   Recursion
ii.                 repeat predicate

i.using Recursion

predicates
     getip(symbol,symbol)
     login(symbol,symbol)
     go

clauses
     getip(U,P):-
          write("Enter Username : "),
          readln(U),
          write("Enter Password : "),
          readln(P),
          login(U,P).
    
     go:-
          getip(_,_),
          write("Login Succeeded"),nl.
    
     go:-
          write("Invalid Username or Password"),nl.
    
     login(ce,ce).
     login(ec,ec).
     login(ic,ic).


Repeat predicate

predicates
     getip(symbol,symbol)
     login(symbol,symbol)
     go
     repeat

clauses
     repeat:-
          go.
         
     getip(U,P):-
          write("Enter Username : "),
          readln(U),
          write("Enter Password : "),
          readln(P),
          login(U,P).
    
     go:-
          getip(_,_),
          write("Login Succeeded"),nl.
    
     go:-
          write("Invalid Username or Password"),nl,
          repeat.
    
     login(ce,ce).
     login(ec,ec).
     login(ic,ic).

3.     Write a program to perform  such that user is allowed to input   username and password maximum three times.

predicates
  getip(symbol,symbol)
  login(symbol,symbol)
  count(integer)
      
clauses
     count(3).
    
     count(N):-
          getip(_,_),
          write("Login succeeded"),nl.
    
     count(N):-
          write("Invalid Username and Password"),nl,
          A = N+1,
          count(A).
    
     getip(U,P):-
          write("Enter Username : "),
          readln(U),
          write("Enter Password : "),
          readln(P),
          login(U,P).
    
     login(ce,ce).
     login(ec,ec).
     login(ic,ic).


4.     Define predicate location(City,State). Write a program such that it displays all the locations except city “Delhi”. Using:
i.                   not predicate
ii.                 Combination of cut and fail.
predicates
   find
   checkcity(string)
   location(string,string)

clauses
   location("Jaypur","Rajsthan").
   location("Delhi","UP").
   location("Rajkot","Gujarat").
   location("Bhopal","MP").

   find:-
       writef("%-10%6","CITY","STATE"),nl,
       writef("----------------"),nl,
       fail.
  
   find:-
       location(C,S),
       checkcity(C),
       writef("%-10%5",C,S),nl,
       fail.    
     find.
   checkcity("Delhi"):-
       !,fail.
   checkcity(_).

Read more...

Tutorial 4 of Parallel Processing



QUE 1) WRITE A PROGRAM TO CREATE 5 PROCESSES AND USING THAT PROCESSES COMPUTE SUM OF 10 ELEMENT BY LOOP SPLITING.


Pheader.h

void *sharei(int size,int* shmid)
{
        *shmid=shmget(IPC_PRIVATE,size,0666|IPC_CREAT);
        return shmat(*shmid,0,0);
}
void cleanup_memory(int *shmid)
{
        shmctl(*shmid,IPC_RMID,NULL);
}

T4p1.c:

#include<stdio.h>
#include "pheader.h"
int main()
{
        int shmid,a[10],*sum,final_sum=0,id,i;
        for(i=0;i<10;i++){
                printf("Enter Element %d:",i+1);
                scanf("%d",&a[i]);
        }
        sum=sharei(20,&shmid);
        id=process_fork(5);
        sum[id]=0;
        for(i=id;i<10;i=i+5)
        {
                sum[id]+=a[i];
        }
        process_join(id,5);
        for(i=0;i<5;i++)
        {
                final_sum+=sum[i];
        }
        printf("result=%d\n",final_sum);
        cleanup_memory(&shmid);
}


OUTPUT:
[08ce55@linux ~]$ gcc loop.c
[08ce55@linux ~]$ ./a.out
1
2
3
4
5
6
7
8
9
10
finalsum=55

QUE 2) WRITE A PROGRAM TO FIND MINIMUM AND MAXIMUM NUMBER FROM 10 NUMBERS USING 5 PROCESSES AND LOOP SPLITTING.

#include<stdio.h>
#include "header.h"
int main()
{
        int id,i,shmid,a[10],*max,finalmax,*min,finalmin;
        for(i=0;i<10;i++)
                scanf("%d",&a[i]);

        max=sharem(20,&shmid);
         min=sharem(20,&shmid);

        id=process_fork(5);
        max[id]=a[id];
        min[id]=a[id];
        for(i=id;i<10;i=i+5)
        {
           if(a[i]>max[id])
           {
              max[id]=a[i];

               }
        if(a[i]<min[id])
        {
          min[id]=a[i];

           }
        }
        process_join(id,5);
        finalmax=max[0];
        finalmin=min[0];
        for(i=0;i<5;i++)
        {
          if(max[i]>finalmax)
          {
           finalmax=max[i];
          }
            if(min[i]<finalmin)
            {
               finalmin=min[i];
  }
        }

        printf("Minimum=%d\n",finalmin);
         printf("Maximum=%d\n",finalmax);

        shmdt((void*)max);
         shmdt((void*)min);
        shmctl(shmid,IPC_RMID,NULL);
}
Output:-
[08ce59@linux ~]$ ./a.out

30
3
45
60
90
900
800
700
56
709
Minimum=3
Maximum=900 

Read more...
Related Posts Plugin for WordPress, Blogger...

Engineering material

GTU IDP/ UDP PROJECT

GTU IDP/ UDP PROJECT

Patel free software download

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP