for Robot Artificial Inteligence

18.Knapsack problem

|

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int knapSack(int W, int wt[], int val[], int n)
{

    // bas case
    if(n == 0 || W == 0)
        return 0;

    // if weight of the n-th item is more than Knapsack capacity W,
    // Then this item cannot be included in the optimal solution
    if(wt[n-1]>W)
        return knapSack(W,wt,val,n-1);

    // Return the maximum of two cases:
    // (1) n-th item included
    // (2) not included
    else
        return max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-1));
}

int main()
{
    int val[] = {60,100,120};
    int wt[] = {10, 20, 30};
    int W = 50;

    int n = sizeof(val)/sizeof(val[0]); //len of array

    cout<<knapSack(W,wt,val,n);

    return 0;
}

Comment  Read more

17.Dynamic Programming - Fibonachi

|

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int fibonachi(int x)
{
    int y;
    if (x==0)
        return 0;
    if (x==1)
        return 1;
    if (x==2)
        return 1;
    y = (x-2)+(x-1);
    return y;
}
int main()
{
    int k = 5;

    int result = fibonachi(k);

    cout<<result;

    return 0;
}

Comment  Read more

16.sorting structure

|

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct Car
{
    int speed, price;
    char name[20];
}cars[100];// Global variable are initialized: number - 0, char - NULL(nothing)

bool cmp(Car A, Car B)
{
    if (A.speed!=B.speed)
        return A.speed > B.speed;
    else
        return A.price<B.price;

    /*
    return (A.speed/A.price) > B.speed/B.price)
    */

}

int main()
{
    cars[1].speed = 100; cars[1].price=50;
    cars[2].speed = 200; cars[1].price=147;
    cars[3].speed = 200; cars[1].price=153;
    cars[4].speed = 132; cars[1].price=344;
    cars[5].speed = 97; cars[1].price=45;
    cars[6].speed = 97; cars[1].price=89;

    sort(cars+1, cars+7, cmp);

    for (int i = 1; i<=6; i++)
    {
        cout<<cars[i].speed<<"\n";
    }

    return 0;
}

Comment  Read more

15.Constructors example

|

CAR

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;

    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3];

    return 0;
}

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]

void printingcar(car car_)
{
    cout<<car_.model<<"\n";
    cout<<car_.ID<<"\n";
}
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;
    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3]<<"\n";
    for (int i=1; i<=100; ++i)
    {
        cars[i].ID = i;
        strcpy(cars[i].model, "Fast and Furious");
        cars[i].year = 2017;
    }
    printingcar(cars[13]);
    printingcar(cars[37]);
    return 0;

}

Student

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]

void printingcar(car car_)
{
    cout<<car_.model<<"\n";
    cout<<car_.ID<<"\n";
}
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;
    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3]<<"\n";
    for (int i=1; i<=100; ++i)
    {
        cars[i].ID = i;
        strcpy(cars[i].model, "Fast and Furious");
        cars[i].year = 2017;
    }
    printingcar(cars[13]);
    printingcar(cars[37]);
    return 0;

}

Product

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct Product
{
    double price;
    char name[20];
    // initialized
    Product()
    {
        price = 0;
        memset(name,0,sizeof(name));
        // if we don't make the name NULL, it will be randomly intialized.
    }
    Product(double newPrice)
    {
        price = newPrice;
    }

    Product(int Newprice)
    {
        price = Newprice;
    }
    Product(char newName[], double newPrice)
    {
        price = newPrice;
        memset(name,0,sizeof(name));
        strcpy(name, newName);
    }
}student1;// Global variable are initialized: number - 0, char - NULL(nothing)


int main()
{
    Product X("apple", 2.5);
    Product Y(3.6);
    return 0;
}

Comment  Read more

14.data structure - bookshelf

|

Part 1

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

/*
struct [structure tag]
{
    member definition;
    member definition;
    ...
    member definition;
}[one or more structure variables](optional);
*/

int main()
{
    return 0;

}

Part 2

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

/*
struct [structure tag]
{
    member definition;
    member definition;
    ...
    member definition;
}[one or more structure variables](optional);
*/
struct Books
{
    char Title[20];
    char Author[20];
    int ID;
    float price;
}book1;
// book1 is now a variable of Books data type(Global variable(declare on top on the program)
int main()
{
    // Local variable(declared inside a block of code)
    book1.ID = 10;
    book1.price = 13.67;
    strcpy(book1.Title, "Data structure");
    strcpy(book1.Author, "Mark");
    cout<<"Book1 title is: " <<book1.Title<<"\n";
    cout<<"Book1 Author is: " <<book1.Author<<"\n";
    cout<<"ID : " <<book1.ID<<" and the price : "<<book1.price<<"\n";

    Books book2;
    book2.ID = 100;
    book2.price = 16.4;

    cout<<"ID : " <<book2.ID<<" and the price : "<<book2.price<<"\n";
    return 0;

}

Part3

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

/*
struct [structure tag]
{
    member definition;
    member definition;
    ...
    member definition;
}[one or more structure variables](optional);
*/
struct Books
{
    char Title[20];
    char Author[20];
    int ID;
    float price;
}book1;
// book1 is now a variable of Books data type(Global variable(declare on top on the program)

void printingBooks (Books variableBook)
{
    cout<<"Book1 title is: " <<variableBook.Title<<"\n";
    cout<<"Book1 Author is: " <<variableBook.Author<<"\n";
    cout<<"ID : " <<variableBook.ID<<" and the price : "<<variableBook.price<<"\n";
}

int main()
{
    // Local variable(declared inside a block of code)
    book1.ID = 10;
    book1.price = 13.67;
    strcpy(book1.Title, "Data structure");
    strcpy(book1.Author, "Mark");

    printingBooks(book1);

    return 0;
}

Comment  Read more