Wednesday, 23 October 2024

sffknfsfnsdks

 https://docs.google.com/document/d/16HPy3uJ1LrfLFnviRVVPnavEK-9SBM-FN1lxuYoxSE0/edit?tab=t.0



% male 

male(raj).

male(rahul).

male(sanjeev).

male(sam).

male(evan).


% female

female(neha).

female(elizabeth).

female(uli).

female(nupur).


% facts

parent(raj, rahul).

parent(raj, sanjeev).

parent(neha, rahul).

parent(neha, sanjeev).


parent(rahul, sam).

parent(elizabeth, sam).


parent(sanjeev, nupur).

parent(uli, nupur).


parent(sanjeev, evan).

parent(uli, evan).


% wife facts

wife(neha, raj).

wife(elizabeth, rahul).

wife(uli, sanjeev).



% rules

siblings(X, Y) :- parent(Z, X), parent(Z, Y), X \== Y.

husband(X, Y) :- wife(Y, X). 

father(X, Y) :- male(X), parent(X, Y).

mother(X, Y) :- female(X), parent(X, Y).

grandparent(X, Y) :- parent(X, Z), parent(Z, Y).





% Base case: If there is only 1 disk, just move it from Source to Destination.

move(1, Source, Destination, _) :-

    write('Move disk 1 from '), write(Source), write(' to '), write(Destination), nl.


% Recursive case: Move N disks from Source to Destination using Auxiliary.

move(N, Source, Destination, Auxiliary) :-

    N > 1,

    M is N - 1,

    

    % Step 1: Move N-1 disks from Source to Auxiliary using Destination as the helper

    move(M, Source, Auxiliary, Destination),

    

    % Step 2: Move the Nth disk from Source to Destination

    write('Move disk '), write(N), write(' from '), write(Source), write(' to '), write(Destination), nl,

    

    % Step 3: Move N-1 disks from Auxiliary to Destination using Source as the helper

    move(M, Auxiliary, Destination, Source).


% Facts

mammal(horse).         % Horses are mammals

mammal(cow).           % Cows are mammals

mammal(pig).           % Pigs are mammals

horse(bluebird).       % Bluebird is a horse

can_read(X) :- literate(X).  % Whoever can read is literate

beside_lake(tree(T)) :- aquatic_bird_sleeps_in(T).  % Any tree where aquatic birds sleep is beside some lake


% Rule for food

food(X) :- eats(Y, X), \+ killed_by(Y, X).  % Anything someone eats and is not killed by is food


% Queries to prove:

% ?- mammal(horse).

% ?- horse(bluebird).

% ?- literate(X).

% ?- beside_lake(tree(T)).

% ?- food(X).




% Facts

hungry(rimi).        % Rimi is hungry


% Rules

barking(X) :- hungry(X).      % If X is hungry, then X barks

angry(raja) :- barking(rimi). % If Rimi is barking, then Raja is angry


% Query (to prove that Raja is angry)

% ?- angry(raja).



Thursday, 24 August 2023

STACK

 #include <stdio.h>

#include <stdlib.h>  // For system("clear")

#define Max 10


int st[Max], top = -1;


void push(int st[], int val);

int pop(int st[]);

int peak(int st[]);

void display(int st[]);


int main() {

    int val, option;

    system("clear");


    do {

        printf("*****CHOOSE OPTION******\n");

        printf("1. Push\n");

        printf("2. Pop\n");

        printf("3. Peak\n");

        printf("4. Display\n");

        printf("5. Exit\n");


        printf("Enter your option: ");

        scanf("%d", &option);


        switch(option) {

            case 1:

                printf("Enter the number to Push: ");

                scanf("%d", &val);

                push(st, val);

                break;   ////

  • case 1:: Handles the case where the user chooses to push a value onto the stack.

  • printf("Enter the number to Push: ");: Prompts the user to enter a value to push onto the stack.

  • scanf("%d", &val);: Reads the value from the user and stores it in the val variable.

  • push(st, val);: Calls the push function to push the entered value onto the stack.

  • break;:


            case 2:

                val = pop(st);

                if(val != -1)

                    printf("The value deleted from the stack is %d\n", val);

                break;

Case 2 Explanation (Pop Operation):

This case corresponds to the "Pop" operation in the menu. When the user selects option 2, the program performs the pop operation on the stack. Let's break down the lines of code within this case:

  • val = pop(st);: This line calls the pop function with the st array as an argument. The pop function returns the value that was removed from the stack (or -1 in case of underflow) and assigns it to the val variable. This allows you to capture the value that was popped from the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an underflow condition, which means the stack was empty and the pop operation could not be performed. If val is not -1, it means the pop operation was successful.

  • printf("The value deleted from the stack is %d\n", val);: If the pop operation was successful (i.e., val is not -1), this line prints the value that was popped from the stack. This provides feedback to the user about the removed element.

  • break;: This line ends the execution of case 2. After performing the pop operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.



            case 3:

                val = peak(st);

                if(val != -1)

                    printf("The value on top is %d\n", val);

                break;


Case 3 Explanation (Peak Operation):

This case corresponds to the "Peak" operation in the menu. When the user selects option 3, the program performs the peak operation on the stack. The peak operation retrieves the value at the top of the stack without removing it. Let's break down the lines of code within this case:

  • val = peak(st);: This line calls the peak function with the st array as an argument. The peak function returns the value at the top of the stack (or -1 in case of an empty stack) and assigns it to the val variable. This allows you to capture the value at the top of the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an empty stack, and the peak operation cannot be performed.

  • printf("The value on top is %d\n", val);: If the peak operation was successful (i.e., val is not -1), this line prints the value that is currently at the top of the stack. This provides feedback to the user about the top element without removing it.

  • break;: This line ends the execution of case 3. After performing the peak operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.

            case 4:

                display(st);

                break;

        }

    } while(option != 5);


    getchar();  // Wait for a keypress

    return 0;

}


void push(int st[], int val) {

    if(top == Max - 1) {

        printf("Stack is overflow or full\n");

    } else {

        top++;

        st[top] = val;

    }

}


int pop(int st[]) {

    int val;

    if(top == -1) {

        printf("\nStack underflow or empty\n");

        return -1;

    } else {

        val = st[top];

        top--;

        return val;

    }

}


void display(int st[]) {

    int i;

    if(top == -1) {

        printf("\nStack is underflow or empty\n");

    } else {

        for(i = top; i >= 0; i--)

            printf("\n%d", st[i]);

        printf("\n");

    }

}


int peak(int st[]) {

    if(top == -1) {

        printf("\nStack is empty\n");

        return -1;

    } else {

        return st[top];

    }

}


Sunday, 20 August 2023

Structures ONE SHOT

                                                   Structures 


USER DEFINED DATA TYPE 

struct employee {

int emp_id; 

char name ;

float salary;

};



#include<stdio.h>


struct pokemon{

    int attack;

    int speed; 

    int hp;

    char class;  //A B C 

    

};


int main(){

    

    struct pokemon pika;

    pika.attack=60;

    pika.speed=40;

    pika.hp=30;

    pika.class= 'A';

    

    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);

    

    struct pokemon ash;

    ash.attack=30;

    ash.speed= 50;

    ash.hp=90;

     ash.class= 'B';

     printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);


}

//AISE HI AAP SIRF STRUCT POKEMON AUR USKE AAGE EK PECIFIER LAGA KE INFINITY INFORMATION STORE KAR SAKTE HAI 


YE JUB HUME INPUT LEA HO VALUES KO  note %c likh rehe ho tho ek space dedena kyu ki koi white space name ki chiz hoti hai   NOTE =%C BUS EK CHARACTER PRINT KARE GA  BUT TUMEHE PURA NAME PRINT KAR NA HO THO EK CHAR ARRAY BA NAO USKA SIZE RAKHO AUR %S KE SAATH PRINT KARAO.

   printf("enter attack speed hp and name ");

    scanf("%d%d%d %c", &pika.attack,&pika.speed,&pika.hp,&pika.class);

    

    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);



SAME PROGRAME HAI BUS PIKA   AUR   ASH UPPAR HI DEFIE KAR DIYA 


#include<stdio.h>




struct pokemon{


    int attack;


    int speed; 


    int hp;


    char class;  //A B C 


    


} pika, ash;              // ye TYPEDEF nhi hai samajh bhai niche dekh compare kar ye thoda alag hai  



int main(){


    


    


    pika.attack=60;


    pika.speed=40;


    pika.hp=30;


    pika.class= 'A';


    


    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);


    


   ;


    ash.attack=30;


    ash.speed= 50;


    ash.hp=90;


     ash.class= 'B';


     printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);


}



FOR PRINTING NAME 

strcpy(a1.name,"habits ");


NICHE WALE CODE MAI WITHOUT STRCPY USE KAR KE NAME  INPUT LIYE AUR PRINT KARWAYA 

#include<stdio.h>

#include<string.h>

struct Person{

    char name[100];

    int salary;

    int age;

    

}a1;


int main(){

   printf("enter employee details 1)name 2)salary 3)age \n");

   scanf("%s %d %d", a1.name, &a1.salary, &a1.age);  //a1.name ke pehele '&' nhi use hoga 

   

   

   

   printf("your input is \n %s\n %d\n %d\n ",a1.name, a1.salary, a1.age);

    

}



TYPEDEF KEYWORD 

typedef   is  used  for  renaming  something 

NOTE :look  apna collage notes. 

#include<stdio.h>

typedef float realNumber;


int main(){

    

    int x;

    realNumber y = 3.14;  //simpally humne float ko ek naya name de diya       realNumber ye jo realNumber hai vo float ka kam kare ga 

                   //basically typedef is used for renaming something.

    printf("%f",y);

}

TYPEDEF KEYWORD  apna collage

used to create a alias for data types 

in simple word jo bhi hum structure ana rahe hai uske liye ek nick name dena for example agar hum ne koi bade nam ka structure banaya par hum uss bade name ko bar bar likh na nhi chahate so uske liye ek shortform bana denge 

#include <stdio.h>

#include <string.h>

typedef struct student { //bhai yaha type def likh na mat bhul na bus 

    int roll; 

    float cgpa;

    char name[100];

stu;


int main(){

    

    stu a1;   //ya struct student a1 ki jage bus stu a1 likha bus       

    a1.roll = 34;

    a1.cgpa = 8.1;

    strcpy(a1.name, "ashish");

    

    printf(" roll no is : %d", a1.roll);

 

    return 0;

}

thoda galat hai  janae se pehele kar fir se 

#include <stdio.h>

#include<string.h>

struct adress{

    int hoouseno;

    int blockno;

    char city[100];

    char state[100];

};

void print(struct address a1);


int main() {

    struct address a1[100];

    printf("enter info here for person 1");

    scanf("%d", &a1[0].houseno);

    scanf("%d",&a1[0].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

   

      printf("enter info here for person 2");

    scanf("%d", &a1[1].houseno);

    scanf("%d",&a1[1].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

    

     printf("enter info here for person 3");

    scanf("%d", &a1[2].houseno);

    scanf("%d",&a1[2].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

    

    print(a1.[0]);

    print(a1.[1]);

     print(a1.[2]);


    return 0;

}

void print(struct address a1){

    

    printf("address is : %d, %d, %s, %s\n", a1.houseno, a1.blockno, a1.state, a1.city);


//make a structure to store bank account information of a customer of abc bank. also, make an alias for it 


#include<stdio.h>

#include<string.h>


typedef struct bankaccount{

    int accountno;

    char name[100];

}  acc;


int main(){

    

    acc a1 = {124,"ashish"};

    acc a2 = {234, "shradha"};

     acc a3 = {102, "shiv"};

     

     printf("acc no = %d \n",a1.accountno);

      printf("acc name = %s",a1.name);

      

      return 0;

}


                                                          ARRAY OF STRUCTURES 


#include<stdio.h>

int main(){
typedef struct pokemon{

    int speed;
    int attck;
    int hp;
    char class;
}pokemon;

pokemon arr[10];


    return 0;
}


#include<stdio.h>

#include<string.h>


int main(){


    typedef struct pokemon{

        int attack;

        int hp;

        int speed;

        char class;

        char name [15];

    }pokemon;


    pokemon arr[3];  // dekh yaHha 3n alag box bun gai 0,1,2


    arr[0].attack = 40;  //ab ye arr[0] ke box ke andar 4 type distribute hue hai 

    arr[0].hp = 30;

    arr[0].speed = 50;

    arr[0].class = 'A';

    // arr[0].name = "poki";  aise nhi likh sakte hai 

    

    strcpy(arr[0].name, "poki");


    arr[1].attack = 35;

    arr[1].hp = 64;

    arr[1].speed = 44;

    arr[1].class = 'B';

    strcpy(arr[1].name, "loki");


    arr[2].attack = 450;

    arr[2].hp = 305;

    arr[2].speed = 350;

    arr[2].class = 'C';

    strcpy(arr[2].name, "moki");

    

    for(int i=0;i<3;i++){  //ye ek loop se sari chiz print ho jaegi pehele 0 position pe check hoga aur print  fir 1 fir 2 

    

        printf("Name = %s\n", arr[i].name );    

        printf("Attack = %d\n", arr[i].attack);

        printf("Hp = %d\n", arr[i].hp);

        printf("Speed = %d\n", arr[i].speed);

        printf("Class = %c\n \n", arr[i].class);

        

    }

}

dekh humne uppar value declare kiya tha matlab attack =30 aise ab input lene hai tho bus printf ki jage scanf kar do LOOP   LAGA   KE


DECLARATION :   hum aise ek structure array ko declare kar te hai 

struct pokemon arr[size];  or 

agar typedef declare kiya hia tho pokemon arr[s];


ACCESS :   aur print kar na ho tho aise access kar te hai 

arr[i].attribute;



Q ) //A record contains name of cricketer his age number of test that he played and avarage reun . Creat  an array and then write a program to read these records


#include<stdio.h>

#include<string.h>


int main(){

    

    typedef struct cricketer {

        

        char name [20];

        int age;

        int nft;

        int avg;

        

    }cricketer;

    

    cricketer arr[10];

    

    printf("Enter player details 1) name 2) Age 3) No of test he played 4) His avarage \n ");

    

    for(int i=0;i<10;i++){

       scanf("%s %d %d %d \n", arr[i].name, &arr[i].age,&arr[i].nft, &arr[i].avg);

    }

    

    printf("Your player details are ");

    for(int i=0;i<10;i++){

        printf("Name = %s\n Age= %d\n No of test he played= %d\n His avarage= %d\n\n",arr[i].name,arr[i].age,arr[i].nft,arr[i].avg);

        

    }

    

}

//   PREFECTLLY RUN HO RAHA HAI EKDAM MUST CODE LIKHA HAI TUNE 




#include<stdio.h>


int main(){

    

    typedef struct date{

        int day;

        int month;

        int year;

        

    }date;

    date a,b;

    

    a.day = 12;

    a.month = 12;

    a.year = 2002;

    

    b.day = 12;

    b.month = 4;

    b.year = 2042;

    

    //if(a==b){

    //    printf("equal");

   // }                         a == b COMPAR NHI HO SAKTA HAI BUT USKE ANDAR KI CHIZ COMPARE HO SAKTI HAI JAISE HUMM NE NICHE KIYA HAI.

   // else{

   //     printf("not equal ");

  //  }

    

    

    if(a.day == b.day ){

        printf("equal");

    }

    else{

        printf("not equal")

    }

// hum ye bhi kar sakte hai 

date c;

c == a;


}


                                      NESTING ONE STRUCTURE WITH ANOTHER STRUCTURE                  

#include<stdio.h>

#include<string.h>


int main(){

    

    typedef struct pokemon{

        int hp;

        int speed;

        int attack;

        char name[20];

        

    }pokemon;   // ye hamare normal structure 

    

    typedef struct legendarypokemon{

        pokemon normal;     //yaha pokemon likh ne se hum upparwale pokemon ki               //sari ciz iss nested structure mai use kar sakte hai

        

        char ability[10];  // aur extra chiz bhi add kar sakte hai 

    }legendarypokemon;      // ye  first nested structure banaya humne

    

    typedef struct godpokemon{

        pokemon legend;   //ye pokemon likhne se upar wala main structure pokemon ki sari chiz issme aagai 

        

        int specialattack; //ye like ke maine is nested mai kuch extra add kiya

        

    } godpokemon;

    

    godpokemon az;

    

    az.specialattack = 300;

    

    strcpy(az.legend.ability, "turn anyone in stone");   // .legend likh ne se hune jo nseted loop banayaa tha legendary pokemon ka uske andar ki chiz use kar sak te hai jaise hum ne ussme ability banaya tha vo hum issme bhi use kar sakt te hai 

    

    az.legend.normal.attack = 500;

    

    

    

    

    

    legendarypokemon am;

    am.normal.hp = 150;    //aise hum access kar sakte hai upar wale pokemon ke                              // attributes

    strcpy(am.ability, "fire ball");

    am.normal.speed = 60;

    am.normal.attack = 50;

    strcpy(am.normal.name,"ash");

    

}




                                                         STRUCTURE AS FUNCTION