### 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)
go

clauses
getip(U,P):-

go:-
getip(_,_),

go:-

Repeat predicate

predicates
getip(symbol,symbol)
go
repeat

clauses
repeat:-
go.

getip(U,P):-

go:-
getip(_,_),

go:-
repeat.

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

predicates
getip(symbol,symbol)
count(integer)

clauses
count(3).

count(N):-
getip(_,_),

count(N):-
A = N+1,
count(A).

getip(U,P):-

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(_).

### 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.

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>
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>
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