Monday, 25 April 2011

To sort an Sorted array which both first half and second half are sorted.


Question: Given an integer array of which both first half and second half are sorted. Write a function to merge the two parts to create one single sorted array
in place [do not use any extra space].
e.g. If input array is [1,3,6,8,-5,-2,3,8] It should be converted to: [-5,-2,1,3,3,6,8,8]
------------------------------------------------------------------------------------------------------

int* SortedArr(int b[],int n)
{
int mid=n/2;
int temp=0;
for(int i=0;i<mid;i++)
{
for(int j=n-1;j>=mid;j--)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
return b;
}
---------------------------------------------------------
This function will return in sorted array 
The main function will be like this

int* SortedArr(int*,int);
void main()
{


int b[8]={1,3,6,8,-5,-2,3,8};
SortedArr(b,8);
printf("\n");
for(int i=0;i<8;i++)
{
printf("%d\n",*(b+i));
}

}

Bubble Sort in C


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define N 10
int * BubbleSort(int*, int);
void main()
{

int a[10]={3,7,8,5,2,1,9,5,4,11};
//new sorted array
SelectionSort(a,N);
BubbleSort(a,N);
printf("BUBBLE SORT");
printf("\n");
for(int i=0;i<N;i++)
{
printf("%d\n",*(a+i));
}
_getch();
}

----------------------------------------------------
Bubble sort function
----------------------------------------------------

int* BubbleSort(int  a[],int n)
{
int temp=0;
for(int i=n-2;i>=0;i--)
{
for (int j=0;j<=i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return a;
}
----------------------------------------------------------------------------------------------------
output:



Monday, 18 April 2011

Creating a dll in C and how to use it in C++ program

The following Steps are required
1.Create a new project in Visual Studio 2010
2.Under Visual C tab create an empty project

-----------------------------------------------------------------

#include<iostream>
//C++ style header decoration in the dll
  __declspec(dllexport)  int __stdcall add(int a ,int b )
{
return a+b;
}
//c style header declartion in the dll
 extern "C"  __declspec(dllexport) int  _stdcall sub(int a ,int b )
{
return a-b;
}
//C++ style header decoration in the dll
  __declspec(dllexport)int _cdecl mul(int a ,int b )
{
return a*b;
}
//c style header declartion in the dll
extern "C" __declspec(dllexport)int  _cdecl divide(int a ,int b )
{
return a/b;
}
----------------------------------------------------------------------
Before Building the solution change the  solution configration from debug mode to release mode
go to Project property 
in configuration manager change the  configuration to release
In project properties change the configuration type to Dynamic link library(.dll)
--------------------------------------------------------------------------------------------

Build this project. 
there are two types of calling conventions used   (__stdcall)  and   (__cdecl)
By default  the C++ style header decoration is used.
But if we want to use the c style decoration. we will use  extern "C" .

--------------------------------------------------------------------------------------------
To use a C Dll in C++ C project.
Create a C project in Visual studio .
we will use the all function in the dll.
--------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;

//C++ style header decoration in the dll
int __stdcall add(int a ,int b );

//C style header declartion in the dll
extern "C" int  _stdcall sub(int a ,int b );

//C++ style header decoration in the dll
int _cdecl mul(int a ,int b );

//C style header declartion in the dll
extern "C" int  _cdecl divide(int a ,int b );

void main()
{
cout<<add(3,4);
cout<<sub(4,3);
cout<<mul(3,4);
cout<<divide(4,2);
}

-------------------------------------------------------------

In the project property  Go to  VC++ Directories   .In librery Dependencies add the  full path where the dll is placed....


In the linker -->Input-->additional Dependencies -->add the .lib file of the dll generated.


-------------------------------------------------------------------------------------
Compile the .cpp file.

Sunday, 17 April 2011

Cholesky decomposition of Matix

To do a Cholesky decomposition the given Matrix Should Be a Symmetric Positive-definite Matrix.


original Matrix


L Matix
















Here we have the origianl marix and its cholesky Matrix
A=LLT
LT= Transpose of L Matrix


***************************************************************************



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

using namespace std;

void main()
{

double a[3][3]={{2,-1,0},{-1,2,-1},{0,-1,2}};
double L[3][3]={0};
double LT[3][3]={0};
int i=0,j=0,k=0;
double temp=0;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp=0;
if(i==j)
{
for(k=0;k<j;k++)
{
temp+=((L[j][k])*(L[j][k]));
}

L[i][j]=sqrt((double)(a[i][j]-temp));
}

if(i>j)
{
for(k=1;k<j;k++)
{
temp+=((L[i][k]*L[j][k]));
}
L[i][j]=(1/(L[j][j]))*((a[i][j])-temp);
}

if(i<j)
{
L[i][j]=0;
}
}
}



printf("Given Matrixis\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
//printf("%2d\t",a[i][j]);
cout<<a[i][j]<<"\t\t\t";
}
printf("\n");
}
// Cheloskey Transform
printf("\nGiven Matrix cheloskey Transform is\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<L[i][j]<<"\t\t\t";
//printf("%f\t",L[i][j]);
}
printf("\n");
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
LT[i][j]=L[j][i];

}
}

printf("\n");
printf("Transpose of L MAtrix is");
printf("\n");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<LT[i][j]<<"\t";
//printf("%2d\t",LT[i][j]);
}
printf("\n");
}

_getch();
}
****************************************************************

The Output will be 
The Input  Matrix Was







The Cheloskey Transform Upper Triangular Matrix is









Its Lower trainguler Matrix is

                                                                                     








Friday, 15 April 2011

Recursion Function to find the Sum of digit of N -Digit Number


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

int SumRecursion(int );
void main()
{
int a=0,k=0;
printf("Enter any digit Number");
scanf("%d",&a);
k=SumRecursion(a);
}

int SumRecursion(int num1)
{
int sum=num1;                    //Initialize sum to the current number i.e. num1
sum=num1/10; // Divide The number by 10
int Reminder=num1-sum*10;       //Find the reminder of the number when divided by 10
num1=sum;                        // To use a recursive function we have to keep on   changing the num1 value

if(sum!=0)
{
Reminder=Reminder+SumRecursion(num1);
}
else
return Reminder;
}

Thursday, 14 April 2011

Time function in c++

To find the time difference in executing a part of program is done by using functions from (time.h)

it has got mainly two type of time 
1). Calander Time
2) System  Clock time :  It is represented by clock_t() and Clock().

The calender time has a time structure:
This structure contains different type of time data types.
Details of the structure is
struct tm{
              int tm_sec;                /// time in seconds
              int tm_min;              /// time in minute

              int tm_hour;           ///   time in hour
              int tm_mday;            // month day (1-31)
              int tm_mon;             ///month (1-12)
              int tm_wday;          /// days of week;
              int tm_yday;          ///days of year(0-365)
              int tm_year;         ///year 
              int tm_isdst;        ///Daylight Saving Time enabled (> 0), disabled (= 0), or unknown (< 0)
             }



Tuesday, 12 April 2011

C prog for Reversing and checking an integer is a palindrome or not


#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include <math.h>
int Palindrome(int);

void main()
{
int a=0,b=0;
long int n=1;
int k;
int  numdigit=5;
//for palindrome

printf("Enter the any  number");
scanf("%d",&a);
k=Palindrome(a);
if(k==1)
{
printf("The given number is a palindrome:\n");
}
else
printf("The given number is not  a palindrome:\n");



_getch();
}

int Palindrome(int a1)
{
int temp=1,remainder=0,temp1=0,a2=0;
int a3=0,count=1,a5=0;
a3=a1;
a5=a1;
for(int i=0;i<100;i++)
{

int a4=a3/10;
a3=a4;

if(a3==0)
{
a2=count;
break;
}
else
count++;
a2=count;
}

int* arr={(int*)malloc(sizeof(int)*a2)};

for(int i=0;i<(a2+1);i++)
{
temp= a1/10;
remainder=a1-temp*10;
*(arr+i)=remainder;
a1=temp;
if (temp==0)
break;
}


for(int i=0;i<(a2+1);i++)
{
temp1+=(*(arr+i))* pow (10.0f,(a2-(i+1)));
if(a2-(i+1)==0)
break;
}

if (temp1==a5)
{
return 1;
}

else

return 0 ;
}

Monday, 11 April 2011

C++ String Class

In C++ String Class is provided for manipulation of strings . It is more user friendly .The functions of this class can be accessed using a  (".") operators.
To include this class we have to use 
using namespace std
------------------------------------------------------------
To initialize a String
------------------------------------------------------------ 
string a="This is my code";
------------------------------------------------------------------------
Functions Provided In String class

find()-  This function returns the position of a particular character .
substr()- This function returns the desired string with in a string.
length()-This Function returns the number of character in the given string.
compare()- This function compare two string and return a bool result.
find_first_of()- This functions find the first occurring of a character in the string
find_last_of()This functions find the first occurring of a character in the string


////////////////////////////////////////////////////////////////////////////////////////////////////////
Program to find the number of vowels in a string
*******************************************************************

long numvowel(string a1)
{
int count=0;
string str1;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;
if(str1.compare("a")==0 || str1.compare("A")==0|| str1.compare("e")==0|| str1.compare("E")==0|| str1.compare("i")==0|| str1.compare("I")==0|| str1.compare("A")==0|| str1.compare("o")==0|| str1.compare("O")==0|| str1.compare("u")==0|| str1.compare("U")==0)
count++;

}
else 
count;
}
return count;

}
****************************************************************************
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function to reverse a string 
********************************************************************************

string reverse(string a1)
{
string temp;
string str1;
string::reverse_iterator rit;
int i=0;
for ( rit=a1.rbegin() ; rit < a1.rend(); rit++ )

{
  temp=*rit;
  str1.append(temp);
  i++;

}


return str1;
}
**********************************************************************************
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


To check if a given string is palindrome or not


*************************************************************************

string palindrome(string a1)
{
string temp;
string str1;
int i=0;
string::reverse_iterator rit;
str1=reverse(a1);

i=str1.compare(a1);
if(i==0)
{
return "TRUE";
}
else

return "False";
}
*************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Function to find number of blank space in a string
***********************************************************************

int blankspace(string a1)
{
string str1;
int count=0;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;


if(str1.compare(" ")==0)
{
count++;
}
else
count;
}

return count;

}
**************************************************************************


Function prototype
long numchar(string);
int blankspace(string);
long numwords(string);
long numvowel(string);
string reverse(string);
string palindrome(string);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


You must have to include using namespace std;

 main function which call all this function
**************************************************************************
void main()
{
string a="This code is primarily developed to understanding from basic";
string str1,str2;
string::iterator i;
printf("The number of charactres in this string is:%d \n",numchar(a));
printf("The number of blankspace in this string is:%d \n",blankspace(a));
printf("The number of vowels in this string is:%d \n",numvowel(a));
str1=reverse(a);
for(i=str1.begin();i<str1.end();i++)
{
printf("%c",*i);


}
str2=palindrome(a);
printf("\n Given String is palindrome  :");
for(i=str2.begin();i<str2.end();i++)
{
printf("%c",*i);


}
_getch();
}
****************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Sunday, 10 April 2011

Printing certain patterns of number in the console window

                                                              "problem 1"
for (int i=1;i<7;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",j);
}
printf("\n");
printf("\n");

}
printf("\n");
printf("\n");

*****************************************************************************
                                                //Problem 2:
printf("problem 2");

for (int i=1;i<7;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",i);
}
printf("\n");
printf("\n");

}

****************************************************************************

                                                                       //problem 3
for (int i=1;i<7;i++)
{
for(int j=0;j<i;j++)
{
printf(" %d \t",i-j);
}
printf("\n");
printf("\n");

}
printf("\n");
printf("\n");





***************************************************************************
                                                          "Problem 4"
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",j+1);
}
printf("\n");
printf("\n");
}

**************************************************************************


                                                                  "Problem 5"
printf("\n");
printf("\n");
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",i);
}
printf("\n");
printf("\n");

}
                                           
********************************************************************************
                                                                     "Problem 6"
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",i-j);
}
printf("\n");
printf("\n");

}
                                                  
**************************************************************************
                                                  "Problem 7"
for (int i=0;i<5;i++)
{
for(int j=5;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",6-j);
}
printf("\n");
printf("\n");

}
                                           
****************************************************************************
                                                     "Problem 8"
for (int i=1;i<6;i++)
{
for(int j=6;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",i);
}
printf("\n");
printf("\n");

}                  
                                          

******************************************************************************
                                                         "Problem 9"
for (int i=1;i<6;i++)
{
for(int j=6-i;j>0;j--)
{
//printf(" \t");
printf(" %d \t",6-i);
}
printf("\n");
printf("\n");

}
                                        
*****************************************************************************
                                       "Problem 10"
for (int i=1;i<6;i++)
{
for(int j=6-i;j>0;j--)
{
//printf(" \t");
printf(" %d \t",j);
}
printf("\n");
printf("\n");

}
                                                    

**************************************************************************
                                                "Problem 11"
for (int i=1;i<6;i++)
{
for(int j=6;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",j-1);
}
printf("\n");
printf("\n");

}

                                        
********************************************************************************
                                           "Problem 22"
for (int i=1;i<6;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",6-i);
}
printf("\n");
printf("\n");

}


****************************************************************************