Custom Search

Thursday 30 October 2014

2-D Matrix Multiplication with output


Write a C program to implement 2D Matrix Multiplication with output:

      Here a 3 x 3 matrix multiplication is given you can change the value of m and n for any m x n matrix . Given program can multiply any number of 2-D matrix.

Program:

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

void main()
{
int i,j,k,t,n=3,m=3,f,a1=3,b1=3,flag=1;
int a[3][3], b[3][50], c[3][50];
clrscr();
printf("enter A column wise:");

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


    do
    {

printf("enter B column wise:");
for(j=0;j<b1;j++)
{
for(i=0;i<a1;i++)
{
scanf("%d",&b[i][j]);
}
}

if(a1==m)
{
for(i=0;i<n;i++)
{
for(j=0;j<b1;j++)
{
c[i][j]=0;
for(k=0;k<a1;k++)
{
c[i][j]+=a[i][k]*b[k][j];

}
}
}
flag=0;
}
       else
{
printf("dimension mismatch::::::::");
}
if(t==0)
{
       printf("The transformed matrix is : \n");
}

for(i=0;i<n;i++)
{
for(j=0;j<b1;j++)
{
printf("%d\t",c[i][j]);
if(j==(b1-1))
             printf("\n");

}
}
if(t!=0)
        {
printf("do you want to multiply further?:\nenter\n 1 for yes\n0 for no ::: ");
scanf("%d",&f);
if(f==0)
{
flag=0;
}
else
{
flag=1;
for(i=0;i<a1;i++)
{
for(j=0;j<b1;j++)
{
a[i][j]=c[i][j];
}
}
printf("want to carry out other transformation?\nenter\n 1 for transformation\n0 for point ::: ");
scanf("%d",&f);
if(f==1)
    b1=3;
else
  {
printf("The transformation matrix is :\n");
for(i=0;i<n;i++)
{            
for(j=0;j<b1;j++)
{
          printf("%d\t",c[i][j]);
if(j==(b1-1))
printf("\n");
}
}
printf("enter no of points to be transformed:");
scanf("%d",&b1);
t=0;
}
      }
       }
      else
       flag=0;

   }while(flag==1);


getch();
}




OUTPUT:





Line using DDA algorithm in any quadrant.

Write a program to draw a line using DDA algorithm in any quadrant.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

void main()
{
     clrscr();
     intgd = DETECT, gm;
     detectgraph(&gd, &gm);
     initgraph(&gd, &gm, "");
     int x1,x2,y1,y2,xd,yd;
     xd=getmaxx();
     yd=getmaxy();
     line(xd/2,0,xd/2,yd);
     line(0,yd/2,xd,yd/2);
     printf("enter x1, y1\n");
     scanf("%d %d",&x1,&y1);
     printf("enter x2, y2\n");
     scanf("%d %d",&x2,&y2);
     intdx,dy,steps;
     dx=abs(x2-x1);
     dy=abs(y2-y1);

     if(dx>dy)
  {
    steps=dx;
      }
  else
      {
     steps=dy;
       }

     float xi,yi,x,y;
     xi=dx/float(steps);
     yi=dy/float(steps);
     int mx=xd/2,my=yd/2;

 // 1st quadrant

      if(x1>0 && y1>0)
      {
        if(x2>0 && y2>0)
     {
     if(x2<x1)
             xi=-xi;
              if(y2<y1)
      yi=-yi;
            }
          
      if(x2<0 && y2>0)
            {
                     xi=-xi;
             if(y2<y1)
          yi=-yi;
               }

       if(x2<0 && y2<0)
       {
                      xi=-xi;
      yi=-yi;
                }

        if(x2>0 && y2<0)
               {
                     if(x2<x1)
                          xi=-xi;
yi=-yi;
}
          }


// 2nd quadrant
        if(x1<0 && y1>0)
        {
if(x2>0 && y2>0)
              {
xi=xi;
if(y2<y1)
yi=-yi;
}

                if(x2<0 && y2>0)
                {
if(x2<x1)
xi=-xi;
if(y2<y1)
yi=-yi;
                }

               if(x2<0 && y2<0)
               {
     if(x2<x1)
          xi=-xi;
     yi=-yi;
               }

               if(x2>0 && y2<0)
               {
     xi=xi;
     yi=-yi;
       }
        }     


//3rd quadrant

       if(x1<0 && y1<0) 
       {
              if(x2>0 && y2>0)
              {
      xi=xi;
    yi=yi;
      }
     
              if(x2<0 && y2>0)
              {
                     if(x2<x1)
         xi=-xi;
                     yi=yi;
              }

              if(x2<0 && y2<0)
              {
      if(x2<x1)
          xi=-xi;
     if(y2<y1)
          yi=-yi;
     }

              if(x2>0 && y2<0)
              {
      xi=xi;
    if(y2<y1)
          yi=-yi;       
              }
      }

 //4th Quadrant

     if(x1>0 && y1<0)
     {
          if(x2>0 && y2>0)
          {
      if(x2<x1)
           xi=-xi;
      yi=yi;
          }

          if(x2<0 && y2>0)
          {
               xi=-xi;
yi=yi;
}

          if(x2<0 && y2<0)
          {
        xi=-xi;
if(y2<y1)
     yi=-yi;
          }

          if(x2>0 && y2<0)
          {
        if(x2<x1)
             xi=-xi;
               if(y2<y1)
     yi=-yi;
          }      
     }

 x=x1;
 y=y1;
putpixel(mx+x1,my-y1,4);
putpixel(mx+x2,my-y2,4);

for(int k=1;k<=steps;k++)
{
putpixel(mx+floor(x),my-floor(y),14);
x=x+xi;    
         y=y+yi;
delay(40);
}
getch();
}




Wednesday 22 October 2014

Write a Program in assembly language to Reverse a string and print whether it is Palindrome or Not. (8086 program)


Following program will check the string given at compile time as input in assembly language; and will print its reversed string and check whether the given string is palindrome or not.
Here the string is google; which is not palindrome.


Program:

data segment
       stri db "google$"
       str1len equ ($-stri)
       str2 db " reversr string $"
       str3 db " not palidrom$"
       str4 db " palidrome$"
data ends

code segment
       assume ds:data,cs:code,es:data

start:
       mov ax,data
       mov ds,ax
       mov es,ax
       mov ah,09h
       lea dx,stri
       int 21h
       mov ah,09h
       lea dx,str2
       int 21h
       lea bx,stri
       mov si,bx
       add si,str1len
       dec si
       dec si

   b: cmp bx,si
       jae a
       mov al,[bx]
       mov ah,[si]
       mov [si],al
       mov [bx],ah
       cmp al,ah
       jne e
       mov cx,01h

   e: inc bx
       dec si
       jmp b

   a: mov ah,09h
       lea dx,stri
       int 21h
       cmp cx,01h
       je c
       mov ah,09h
       lea dx,str3
       int 21h
       jmp skip

   c: mov ah,09h
       lea dx,str4
       int 21h

skip: code ends

end start



Output of the above 8086 program will be like this:


google
Reverse Str:elgoog
Not palindrome

Monday 20 October 2014

Java Program to illustrate garbage collection ;and how to override that method.


Java Program to illustrate garbage collection, Explain garbage collection giving example in Java language


What is Garbage Collection ?
Whenever memory is allocated using new keyword; a reference is created of the object and required equivalent space is allocated in main memory.Now; when object can not be further used anymore; which means when scope of that object is over; that memory is no more required and is just a kind of digital garbage values. Garbage collector frees this memory. We can override this method explicitly also. Example to override this method is as show below. It will count the milliseconds object was in the scope.

Program
import java.util.Vector;

public class GarbageCollector{
   
    public static void main(String[] args) 
{
    int SIZE = 200;

for (int i = 0; i < SIZE; i++) {
    }
    System.out.println("Garbage Collection started explicitly.");
    long time = System.currentTimeMillis();

System.gc();
System.out.println("It took " + (System.currentTimeMillis()-time) + " ms");
        }
}

Program to display selected item for the list and print when pressed a button

Aim: Write a program to display selected item for the list and print when pressed a button 

Program: 
<html>
<body>

<form>
Select your favorite fruit:
<select id="mySelect">
  <option value="apple">apple</option>
  <option value="mango">mango</option>
  <option value="grapes">grapes</option>
  <option value="banana">banana</option>
</select>
</form>



<button type="button" onclick="myFunction()">print</button>

<script>
function myFunction() {


document.write(mySelect.value);

}
</script>

</body>
</html>

Laptops