Saturday, March 31, 2012

Algorithm (Lab report 05)

Theory: The merge sort is very important to sorting algorithm. It can sort different two (Arrays) elements with Divide, conquer and combine approach. Merge sort is very predictable. It makes between 0.5*lg(n) and lg(n) comparisons per element, and between lg(n) and 1.5*lg(n) swaps per element. The minima are achieved for already sorted data; the maxima are achieved, on average, for random data. If using Θ(n) extra space is of no concern, then merge sort is an excellent choice: It is simple to implement, and it is the only stable O(n·lg(n)) sorting algorithm. Note that when sorting linked lists, merge sort requires only Θ(lg(n)) extra space (for recursion).

Apparatus:
           i.   Computer.
          ii.   Coding Language.
         iii.   Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
         iv.   Coding skill.
Algorithm: (Computational (MERGE-SORT(A,p,q,r)

Step01
:  if p <r
Step02:   then q ← (r + p)/2
Step03:   MERGE-SORT(A, p, q )
Step04:   MERGE-SORT(A,q+1,r)
Step05:   MERGE(A, p, q, r)
Step06:    n1←q-p+1
Step07:    n2←r-q
Step08:    create arrays L[1...N1+1] and R[1...N2+1]
Step09:    for i←1 to N1
Step10:   do L[i] ← A[p+i-1]
Step11:   for j ← 1 to n2
Step12:   do R[j] ← A[q+j]
Step13:   L[N1+1] ← ∞
Step14:   R[N2+1] ← ∞
Step15:   i ← 1
Step16:   j ← 1
Step17:   for k ← p to r
Step18:   do if L[i] ≤ R[j]
Step19:   then A[k] ← L[i]
Step20:   i ← i+1
Step21:   else A[k] ← R[j]
Step22:    j ← j+1


Source code 01:
(Random merge sort)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#define max 5000
void MergeSort(int low, int high);
void Merge(int low, int mid , int high);
int a[max];
int b[max];
int low=0;
int high;
int main(void)
{
char check='y';
int i,n;
clock_t start, finish;
while(check=='y')
  {
  textmode(C80);
  cout<<"How many number do you generate(Merge sort):";
  cin>>n;
  for(i=0; i<n; i++)
    {
    a[i]=random(n);
    }
  start=clock();
  high=n;
  for(i=0; i<n; i++)
    {
    MergeSort(low,high);
    cout<<a[i]<<"\t";
    }
  finish=clock();
  cout<<"\nTotal time is:"<<(finish-start)/CLK_TCK<<" second";         check=getche();
}
return 0;
}
void MergeSort(int low, int high)
{
int mid;
if(low<high)
  {
  mid=(low+high)/2;
  MergeSort(low, mid);
  MergeSort(mid+1,high);
  Merge(low, mid, high);
  }
}
void Merge(int low, int mid, int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
  {
  if(a[h]<=a[j])
    {
    b[i]=a[h];
    h=h+1;
    }
  else
    {
    b[i]=a[j];
    j=j+1;
    }
  i=i+1;
  }
if(h>mid)
  {
  for(k=j; k<=high; k++)
    {
    b[i]=a[k];
    i=i+1;
    }
  }
else
  {
  for(k=h; k<=mid; k++)
    {
    b[i]=a[k];
    i=i+1;
    }
  }
for(k=low; k<=high; k++)
a[k]=b[k];
}                                                            Result:
Sample Input 01:
How many number do you generate(Merge sort): 10
Sample Output 01:
0          0          0          0          1          2          3          3          5          7
Total time is 0 second.
===========================================================
Sample Input 02:
How many number do you generate(Merge sort): 14
Sample Output 02:
0          0          0          0          1          2          3          3          4          4          6          7          9
9
Total time is 0 second.

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.

Friday, March 23, 2012

Algorithm (Lab report 04)

Theory:
The exponential differential equations which are the topic of this thesis will be defined in terms of invariant differential forms on commutative algebraic groups, in particular semiabelian varieties. The theory of the equations depends on a good understanding of the algebraic subgroups of commutative algebraic groups.

The main work of the thesis, establishing the theory of exponential   differential equations, Differential equations contains background material from differential algebra and algebraic geometry, and the final gives an application.

Apparatus:
        i.     Computer.
        ii.    Coding Language.
        iii.   Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
        iv.   Coding skill.
Algorithm: (Computational (Exponential) of  X^n)
Step 01: Exponential (X^n)
Step 02: m=n; pow=1; z=X;
Step 03: while(m>0) do
Step 04: while(m mod 2==0) do
Step 05:  {
Step 06       m=(m/2); z=z^2
Step 07:      m=m-1;
Step 08:     pow=pow*z;
Step 09:   }
Step 10: return pow;

Source code 01: (Exponential)
# include<stdio.h>
# include<math.h>
# include<conio.h>
int main()
{
int i,test;
float x,total=0;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
  scanf("%f",&x);
  total+=pow(x,i);
}
printf("%.0f",total);
getch();
return 0;
}
Source code 02: (Exponential)
# include<stdio.h>
# include<math.h>
# include<conio.h>
int main()
{
int i,test;
float x,y,total=0;
scanf("%d",&test);
for(i=1;i<=test;i++)
  {
  scanf("%f%f",&x,&y);
  total+=pow(x,y);
}
printf("Total: %.0f",total);
getch();
return 0;
}


Result 01:
Sample Input                                           Sample output

3
10                                                                           Total: 27480
20
30


Result 02:
Sample Input                                           Sample output

3

10    1                                                                       Total: 27480
20    2
30    3

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.

Saturday, March 3, 2012

Microsoft Visual C++ data types

Type Name
Bytes
Other Names
Range of Values
int
*
signed,
signed int
System dependent
unsigned int
*
unsigned
System dependent
__int8
1
char,
signed char
–128 to 127
__int16
2
short,
short int,
signed short int
–32,768 to 32,767
__int32
4
signed,
signed int
–2,147,483,648 to 2,147,483,647
__int64
8
none
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
bool
1
none
false or true
char
1
signed char
–128 to 127
unsigned char
1
none
0 to 255
short
2
short int,
signed short int
–32,768 to 32,767
unsigned short
2
unsigned short int
0 to 65,535
long
4
long int,
signed long int
–2,147,483,648 to 2,147,483,647
long long
8
none (but equivalent to __int64)
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long
4
unsigned long int
0 to 4,294,967,295
enum
*
none
Same as int
float
4
none
3.4E +/- 38 (7 digits)
double
8
none
1.7E +/- 308 (15 digits)
long double
same as double
none
same as double
wchar_t
2
__wchar_t
0 to 65,535