Showing posts with label two. Show all posts
Showing posts with label two. Show all posts

Saturday, February 14, 2015

Java Program to Find Intersection of two Arrays

For example we have two sorted arrays a1[]={2,3,5,11} and a2[]={4,5,7,9,11} then intersection of a1 and a2 will be {5,11}. A Java program for finding intersection of two arrays is given below.

Also Read: Java Program to Find Union of two Arrays

Java Program to Find Intersection of two Arrays

import java.util.Scanner; //import Scanner class in our program

class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard

System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();

int a1[]=new int[n1];
int a2[]=new int[n2];

System.out.print("
Enter elements of first array in ascending order:");

for(i=0;i<n1;++i)
a1[i]=sc.nextInt();

System.out.print("
Enter elements of second array in ascending order:");

for(i=0;i<n2;++i)
a2[i]=sc.nextInt();


i=j=0;
System.out.print("
Intersection of Arrays: ");

while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
i++;
else
if(a2[j]<a1[i])
j++;
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}

}
}

Java Program to Find Intersection of two Arrays
Read more »

Toggle between two Divs for Pentaho CDE Charts

This post will cover the toggling among the charts.

i.e., Share one place holder for multiple charts by providing a link or a button.

Example developed on :

C-Tools of 14.07.29, foodmart of postgresql, pentaho 5.0.1 CE stable.

Step 1 :  Layout section.
1) Save the dashboard in bootstrap mode.
2) Row ->Column->Html

In Html write below code :

<p><a href="#" id="link">Show B</a></p>
<div id="a"></div>
<div id="b"></div>


<script type="text/javascript">
var $divA = $(#a),
    $divB = $(#b),
    $link = $(#link);

// Initialize everything
$link.text( Pie );
$divA.hide();

$link.click(function(){

  // If A is visible when the link is clicked
  // you need to hide A and show B
  if( $divA.is( :visible ) ){
    $link.text( Pie );
    $divA.hide();
    $divB.show();
  } else {
    $link.text( Bar );
    $divA.show();
    $divB.hide();
  }

  return false;
});
</script>
 

Step 2: Data Sources section

1) Give all the connection details.

Name : query1 
 URL : jdbc:postgresql://localhost:5432/foodmart
 Driver : org.postgresql.Driver
Username/Password : postgres/postgres
Query :

SELECT * FROM
(
SELECT
        DISTINCT brand_name AS "Brand Name",
       SUM(unit_sales) AS "Sales"
FROM product p
INNER JOIN sales_fact_1997 sf7
ON p.product_id=sf7.product_id
INNER JOIN time_by_day t
ON sf7.time_id=t.time_id
WHERE
(
to_char(t.the_date,YYYY-MM-DD)>=2012-01-01
AND
to_char(t.the_date,YYYY-MM-DD)<=2012-01-07
)
GROUP BY "Brand Name"
ORDER BY SUM(unit_sales) DESC
limit 5
)table1

Step 3 : Components section
1) Take  pie  chart & bar chart.
2) Set all the properties like name, htmlObject (for pie take "a" as htmlObject and for bar chart "b" as htmlObject).
3) Save your dashboard and see the preview.


Sample output:

Image 1 : Pie Chart.  



 Image 2 : Perform click action on Pie you will get bar chart in place of pie chart that means you are toggling b/w two chart (i.e., Single place holder is sharing by two charts and the logic of two divs applied for CDE charts).



Download example here : Click Me

References :
1) http://jsfiddle.net/QAxgD/
2) http://stackoverflow.com/questions/18110320/toggle-between-two-divs
3) http://forums.pentaho.com/showthread.php?170449-How-To-Toaggle-Between-Chart-amp-Grid



Read more »

Thursday, February 12, 2015

C Program to multiply two matrices

C Program to multiply two matrices

#include<stdio.h>
#include<conio.h>

void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);

if(n==p)
{
printf("
Enter first matrix:
");

for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);

printf("
Enter second matrix:
");

for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("
The new matrix is:
");


for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("%d ",c[i][j]);
}
printf("
");

}
}
else
printf("
Sorry!!!! Matrix multiplication cant be done");

getch();
}
Read more »

C Program to find quotient and remainder of two numbers


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int a,b,q,r;
cout<<"Enter two numbers:";
cin>>a>>b;


if(a>b)
{
q=a/b;
r=a%b;
cout<<"
Quotient="<<q;

cout<<"
Remainder="<<r;

}
else
cout<<"
First no. should be greater than second no....!!!";



getch();
}

Read more »

Tuesday, February 10, 2015

C Program for Addition of two Sparse Matrices

#include<stdio.h>
#include<stdlib.h>

#define MAX 20

void printsparse(int b[MAX][3]);
void readsparse(int b[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);

void main()
{
    int b1[MAX][3],b2[MAX][3],b3[MAX][3];
    readsparse(b1);
    readsparse(b2);
    addsparse(b1,b2,b3);
    printsparse(b3);
}

void readsparse(int b[MAX][3])
{
    int i,t,m,n;
    printf("
Enter no. of rows and columns:");
    scanf("%d%d",&m,&n);
    printf("No. of non-zero triples:");
    scanf("%d",&t);

    b[0][0]=m;
    b[0][1]=n;
    b[0][2]=t;

    for(i=1;i<=t;i++)
    {
        printf("Enter the triples(row,column,value):");
        scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
    }
}

void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
    int t1,t2,i,j,k;
    if(b1[0][0]!=b2[0][0]||b1[0][1]!=b2[0][1])
    {
        printf("
You have entered invalid matrix!!Size must be equal");
        exit(0);
    }

    t1=b1[0][2];
    t2=b2[0][2];
    i=j=k=0;
    b3[0][0]=b1[0][0];
    b3[0][1]=b1[0][1];

    while(i<=t1&&j<=t2)
    {
        if(b1[i][0]<b2[j][0])       //row numbers are not equal
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
        }

        else if(b2[j][0]<b1[i][0])       //row numbers are not equal
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
        }

        else if(b1[i][1]<b2[j][1])       //row numbers are equal, compare column
        {
            b3[k][0]=b1[i][0];
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2];
            k++;
            i++;
        }

        else if(b2[j][1]<b1[i][1])       //row numbers are equal, compare column
        {
            b3[k][0]=b2[j][0];
            b3[k][1]=b2[j][1];
            b3[k][2]=b2[j][2];
            k++;
            j++;
        }

        else
        {
            b3[k][0]=b1[i][0];      //row and column numbers are equal
            b3[k][1]=b1[i][1];
            b3[k][2]=b1[i][2]+b2[j][2];
            k++;
            i++;
            j++;
        }
    }

    while(i<=t1)        //copy remaining terms from b1
    {
        b3[k][0]=b1[i][0];
        b3[k][1]=b1[i][1];
        b3[k][2]=b1[i][2];
        i++;
        k++;
    }

    while(j<=t2)        //copy remaining terms from b2
    {
        b3[k][0]=b2[j][0];
        b3[k][1]=b1[j][1];
        b3[k][2]=b1[j][2];
        j++;
        k++;
    }
    b3[0][2]=k-1;       //set number of terms in b3
}

void printsparse(int b[MAX][3])
{
    int i,t;
    t=b[0][2];
    printf("
row column value");
    for(i=1;i<=t;i++)
    {
        printf("
%d %d %d",b[i][0],b[i][1],b[i][2]);
    }
}

Also Read: C Program for Finding Transpose of a Sparse Matrix

C Program for Addition of two Sparse Matrices

Time Complexity of Addition of Sparse Matrices

If t1 and t2 are number of non-zero elements in first and second matrixrespectively, then the time to add is O (t1 + t2).
Read more »