Tuesday, 22 December 2015

Implement simple Logic Network Using MP Neuron Model : AND Function implementation using McCulloch-Pitts neuron model ( MP Neuron Model )

%AND function using Mcculloch-Pitts neuron
clear;
clc;
%Getting weights and threshold value
disp('Enter weights');
w1=input('Weight w1=');
w2=input('weight w2=');
disp('Enter Threshold Value');
theta=input('theta=');
y=[0 0 0 0];
x1=[0 0 1 1];
x2=[0 1 0 1];
z=[0 0 0 1];
con=1;
while con
zin=x1*w1+x2*w2;
for i=1:4
if zin(i)>=theta
y(i)=1;
else
y(i)=0;
end
end
disp('Output of Net');
disp(y);
if y==z
con=0;
else
disp('Net is not learning enter another set of weights and Threshold value');
w1=input('weight w1=');
w2=input('weight w2=');
theta=input('theta=');
end
end
disp('Mcculloch-Pitts Net for AND function');
disp('Weights of Neuron');
disp(w1);
disp(w2);
disp('Threshold value');
disp(theta);

Tuesday, 24 March 2015

Write a concurrent program for Matrix Multiplication. Effective use of Multicore Architecture is expected.

//Program#include<iostream>
#include<pthread.h>
#include<cstdlib>

using namespace std;

#define size 3
int nthread;
int A[size][size],B[size][size],C[size][size];

void read(int m[size][size])
{
 int i,j,val;
 for(i=0;i<size;i++)
 for(j=0;j<size;j++)
 cin>>m[i][j];
}//read


void display(int m[size][size])
{
int i,j;
for(i=0;i<size;i++)
 {
  cout<<"\n";
  for(j=0;j<size;j++)
   cout<<m[i][j]<<" ";
 }
} //display


void* multiply(void* tcnt)
{
 int a=(int)tcnt;   
 int from=(a*size)/nthread;
 int to=((a+1)*size)/nthread;
 int i,j,k;

 //multiplication on individual thread
 cout<<"\n Thread Execution :"<<a<<"...[->from row "<<from<<"to "<<to-1<<"]"<<endl;
 for(i=from;i<to;i++)
 {
  for(j=0;j<size;j++)
   {
    C[i][j]=0;
    for(k=0;k<size;k++)
     C[i][j]+=A[i][k]*B[k][j];
   }
  }
 cout<<"\n Execution of thread "<<a<<"is finished. "<<endl;

}//multiply function




int main(int argc,char* argv[])
{
 int i;
 if(argc!=2)
 {
  cout<<"\n Usage :"<<argv[0]<<"number of threads \n ";
  exit(-1);
  }
 //read command line argument for thread count
 nthread=atoi(argv[1]);
 pthread_t thread[nthread];//create thread array of nthread size

 cout<<"\n Enter values of matrix A : \n";
 read(A);
 cout<<"\n Enter values of matrix B : \n";
 read(B);

 for(i=0;i<nthread;i++)
  {
   //create thread and use each thread for multiplication
   pthread_create(&thread[i],NULL,multiply,(void *)i);
    sleep(1);
  }

 //main thread works on thread 0 as it is first thread if main thread =1 then it does everything

 multiply(0);
 for(i=1;i<nthread;i++)
 pthread_join(thread[i],NULL);//main thread waits until other threads complete

 cout<<"\n \nMatrix A: \n";
 display(A);
 cout<<"\n \nMatrix B: \n";
 display(B);
 cout<<"\n \nMatrix C: \n";
 display(C);

 cout<<"\n \n ";
  return 0;
 }//main



/* Expected Output :
svcet@svcet-HoD:~/Desktop$ g++ multi.cpp -o multi -fpermissive -lpthread
svcet@svcet-HoD:~/Desktop$ ./multi 3

 Enter values of matrix A :
1 2 3
1 2 3
1 2 3

 Enter values of matrix B :
1 2 3
1 2 3
1 2 3

 Thread Execution :0...[->from row 0to 0]

 Execution of thread 0is finished.

 Thread Execution :1...[->from row 1to 1]

 Execution of thread 1is finished.

 Thread Execution :2...[->from row 2to 2]

 Execution of thread 2is finished.

 Thread Execution :0...[->from row 0to 0]

 Execution of thread 0is finished.


Matrix A:

1 2 3
1 2 3
1 2 3

Matrix B:

1 2 3
1 2 3
1 2 3

Matrix C:

6 12 18
6 12 18
6 12 18

*/

Monday, 23 February 2015

10. Implement C++/Java/Python program to create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called function get_data() to initialize base class data members and another member function display_area() to compute and display the area of figures. Make classes to suit their requirements. Using these three classes, design a program that will accept dimension of a triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of triangles, and used as follows: Area of rectangle= x*y Area of triangle =1/2*x*y

import java.io.*;
import java.util.*;


interface shape
{
  void getdata(double i,double j);
  void area();
}

class triangle implements shape
{
  public double x,y;
  public void getdata(double i,double j)
   {
     x=i;
     y=j;
     //System.out.println("\n Enter Base and Height of Triangle :"+"\n BAse :");
     //Scanner sc=new Scanner(System.in);
     //x=sc.nextDouble();
     //System.out.println("\n Height :");
     //y=sc.nextDouble();
    }
 public void area()
 {
  System.out.println("\n Area of Triangle is :"+(0.5*x*y));
  }
}


class rectangle implements shape
{
  public double x,y;
  public void getdata(double i,double j)
   {
     x=i;
    y=j;
     /* System.out.println("\n Enter Sides of Rectangle  :"+"\n Side 1 :");
     Scanner sc=new Scanner(System.in);
     x=sc.nextDouble();
     System.out.println("\n Side 2 :");
     y=sc.nextDouble();
    */   
   }
 public void area()
 {
  System.out.println("\n Area of Rectangle is :"+(x*y));
  }
}


class inherit
{
  public static void main(String []std)
  {
       int ch;
         double a,b;
    System.out.println("\n Enter two values :"+"\n a :");
     Scanner sc=new Scanner(System.in);
     a=sc.nextDouble();
     System.out.println("\n Height :");
     b=sc.nextDouble();
       do
      {
       System.out.println("\n Menu 1.Triangle \n 2.Rectangle \n 3.Exit  \n Enter your choice :");
    Scanner s=new Scanner(System.in);
    ch=s.nextInt();
    switch(ch)
        {
        case 1: triangle t1=new triangle();
        t1.getdata(a,b);
        t1.area();
    break;
        case 2: rectangle r=new rectangle();
    r.getdata(a,b);
    r.area();
    break;
    case 3: System.exit(1);
         }//switch
     }while(ch!=3);    //do
  } //main
}//class

9. Refer the standard template library to use list container and using C++/Java implement following member functions of list class: empty, insert, merge, reverse, sort



#include<iostream>
#include<list>
#include<stdlib.h>

using namespace std;

void display(list <int> &lst)
{
    list<int> :: iterator p;
    cout<<"\n";   
    for(p=lst.begin();p!=lst.end();++p)
    cout<<"\n"<<*p;
    cout<<"\n \n";
}
int main()
{
    list<int> list1;
    list <int> list2(5);
    for(int i=0;i<3;i++)
    list1.push_back(rand()/100);
    list<int>::iterator p;
    for(p=list2.begin();p!=list2.end();++p)
    *p=rand()/100;
    cout<<"list1\n";
    display(list1);
    cout<<"list2\n";
    display(list2);
    //add two elements at the end of list
    list1.push_front(100);
    list1.push_back(200);
    //remove an element at the front of list
    list2.pop_front();
    cout<<" Modified list1\n";
    display(list1);
    cout<<"Modified list2\n";
    display(list2);
    list<int>listA,listB;
    listA=list1;
    listB=list2;
    //Merging Two lists
    list1.merge(list2);
    cout<<"merged unsorted list";
    display(list1);

    //sorting and merging
    listA.sort();
    listB.sort();
    listA.merge(listB);
    cout<<"Merged sorted list\n";
    display(listA);

    //Reversing a list
    listA.reverse();
    cout<<"Reversed sorted list\n";
    display(listA);
    return 0;
}

Monday, 19 January 2015

Implement C++/Java/Python program for bubble sort using function template.

 // b_sort.cpp
                                                                                                                                                                                                                                                                                                                                         
#include<iostream> 
#include<iostream>
using namespace std;
template <class T>
void bubble(T a[],int n)
{
    for(int i=0;i<n-1;i++)
    {
    for(int j=0;j<n-i-1;j++)
    {
        if(a[j]>a[j+1])
        {
        T temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        }
    }
    }
}

        int main()
            {
                int x[5];
                float y[5];
                cout<<"Enter values of array x:\n";
                for(int i=0;i<5;i++)
                 cin>>x[i];
                cout<<"Enter values of array y:\n";
                for(int i=0;i<5;i++)
                cin>>y[i];
                bubble(x,5);
                bubble(y,5);
                cout<<"Sorted array X:";
                for(int i=0;i<5;i++)
                cout<<x[i]<<"\t";
                cout<<endl;
                cout<<"Sorted array Y:";
                for(int i=0;i<5;i++)
                cout<<y[i]<<"\t";
                cout<<endl;
                return 0;
            }


//java code

 import java.util.*;
import java.io.*;

class BubbleSort
{
  public static <T extends Comparable<T>> void bubbleSort (T[] list, int size)
  {
    T temp;
    // swapOccurred helps to stop iterating if the array gets sorted before
    // outCounter reaches to size
    for (int i=0;i<list.length;i++)
    {
      for (int j=0;j<list.length-1;j++)
      {
        if (list[j].compareTo(list[j+1]) > 0)
        {
          temp = list[j];
          list[j] = list[j+1];
          list[j+1] = temp;
        }
      }
    }
  }
}

public class BubbleSortDemo
{
  public static void main (String[] args)
  {
    Integer arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    BubbleSort.bubbleSort(arr, arr.length);

    System.out.println("Sorted Array: ");
    for(Integer i : arr)
    {
      System.out.println(i);
    }
    Double arr1[] = {10.5, 2.9, 6.8, 1.7, 6.8, 5.5, 4.4, 1.3, 2.91, 1.11};
    BubbleSort.bubbleSort(arr1, arr1.length);

    System.out.println("Sorted Array: ");
    for(Double i : arr1)
    {
      System.out.println(i);
    }


  }
}
       

Implement C++/Java/Python program to create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called function get_data() to initialize base class data members and another member function display_area() to compute and display the area of figures. Make classes to suit their requirements. Using these three classes, design a program that will accept dimension of a triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of triangles, and used as follows: Area of rectangle= x*y Area of triangle =1/2*x*y

////////Java Program

import java.io.*;
import java.util.*;


interface shape
{
  void getdata();
  void area();
}

class triangle implements shape
{
  public double x,y;
  public void getdata()
   {

     System.out.println("\n Enter Base and Height of Triangle :"+"\n BAse :");
     Scanner sc=new Scanner(System.in);
     x=sc.nextDouble();
     System.out.println("\n Height :");
     y=sc.nextDouble();
    }
 public void area()
 {
  System.out.println("\n Area of Triangle is :"+(0.5*x*y));
  }
}


class rectangle implements shape
{
  public double x,y;
  public void getdata()
   {

     System.out.println("\n Enter Sides of Rectangle  :"+"\n Side 1 :");
     Scanner sc=new Scanner(System.in);
     x=sc.nextDouble();
     System.out.println("\n Side 2 :");
     y=sc.nextDouble();
    }
 public void area()
 {
  System.out.println("\n Area of Rectangle is :"+(x*y));
  }
}


class inherit
{
  public static void main(String []std)
  {
       int ch;
       do
      {
       System.out.println("\n Menu 1.Triangle \n 2.Rectangle \n 3.Exit  \n Enter your choice :");
    Scanner s=new Scanner(System.in);
    ch=s.nextInt();
    switch(ch)
        {
        case 1: triangle t1=new triangle();
        t1.getdata();
        t1.area();
    break;
        case 2: rectangle r=new rectangle();
    r.getdata();
    r.area();
    break;
    case 3: System.exit(1);
         }//switch
     }while(ch!=3);    //do
  } //main
}//class





/////        C++ Program



#include <iostream> 
using namespace std;
 
class shape {
   protected:
      int width, height;
   public:
      shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
     virtual int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public shape{
   public:
      Rectangle( int a=0, int b=0)
      {
        shape(a, b); 
      }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public shape{
   public:
      Triangle( int a=0, int b=0)
      {
        shape(a, b); 
      }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
// Main function for the program
int main( )
{
   shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // store the address of Rectangle
   shape = &rec;
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   shape->area();
   
   return 0;
}

Tuesday, 9 December 2014

1. Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a C++/Java/Python program that creates a monthly report. a) Menu driven program with options to Enter data and Display report b) Report Format


/*Problem Statement: 

Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a C++/Java/Python program that creates a monthly report.
a) Menu driven program with options to Enter data and Display report
b) Report Format

*/

#include<iostream>
using namespace std;
class weather
{
    int d_o_m;
    float htemp,ltemp,amt_of_rain,amt_of_snow;
public:
    weather();
    void inputdata(int);
    void display();
    void average(weather *w);
};
weather::weather()
{
       htemp=999;
    ltemp=-999;
    amt_of_rain=0;
    amt_of_snow=0;
    d_o_m=99;
}
void weather::inputdata(int d)
{
    d_o_m=d;
    cout<<"Enter high Temp:";
    cin>>htemp;
    cout<<"\nEnter low Temp:";
    cin>>ltemp;
    cout<<"\nEnter Amt of Rain:";
    cin>>amt_of_rain;
    cout<<"\nEnter Amt of Snow:";
    cin>>amt_of_snow;
}
void weather::display()
{
         cout<<"\t"<<d_o_m;
     cout<<"\t\t"<<htemp;
     cout<<"\t"<<ltemp;
     cout<<"\t"<<amt_of_rain;
     cout<<"\t"<<amt_of_snow;
}
void weather::average(weather w[31])
{
    int train,tsnow,tltemp,thtemp;
    float avghtemp,avgltemp,avgrain,avgsnow;
    train=tsnow=tltemp=thtemp=0;
    int i,count=0;
    for(i=1;i<=31;i++)
    {
          if(w[i].d_o_m==90)
        continue;
        else
        {
          thtemp+=w[i].htemp;
        tltemp+=w[i].ltemp;
        train+=w[i].amt_of_rain;
        tsnow+=w[i].amt_of_snow;
        count++;
        }
    }
avghtemp=thtemp/count;
avgltemp=tltemp/count;
avgrain=train/count;
avgsnow=tsnow/count;
cout<<"\nAverage High Temp:"<<avghtemp;
cout<<"\nAverage Low Temp:"<<avgltemp;
cout<<"\nAverage Amount of Rain:"<<avgrain;
cout<<"\nAverage Amount of Snow:"<<avgsnow;
}
int main()
{
  weather data[12][31],temp[31],obj;
  int ch,i,day,month;
  char ans;
cout<<"\nWEATHER REPORT";
do
{
cout<<"\n Main Menu";
cout<<"\n1.Enter Data.";
cout<<"\n2.Display Report.";
cout<<"\n3.Exit.";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
 case 1:
           cout<<"\nEnter the month:";
    cin>>month;
    cout<<"\nEnter the day:";
    cin>>day;
    data[month][day].inputdata(day);
    break;
 case 2:
    cout<<"\nEnter the month:";
    cin>>month;
    cout<<"\n\n\tDay\tDay_o_M\tAmt_Rain\tAmt_Snow\tHigh_temp\tLow_temp";
    for(int i=1;i<=31;i++)
    {
             cout<<"\n"<<i;
        data[month][i].display();
    }
    for(int i=1;i<=31;i++)
    {
        temp[i]=data[month][i];
    }
    obj.average(temp);
    break;
}
cout<<"\nDo u want to continue?";
cin>>ans;
}while(ans=='y');
return 0;
}