Developers Archive for the 'c++ programming' Category

Merge Sort Algorithm

Merge Sort Algorithm Tuesday, February 20th, 2007

The basic idea behide the merge sort algorithm is recursively split the array into two halves till last and sorth them at the end.
// Include files
#include <iostream> // used for cin, cout
#include <conio.h>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

// Global Type Declarations

// Function Prototypes
void instruct (void);
void pause ();
void mergesort(int data[], size_t n);
void merge(int data[], size_t n1, size_t n2);
void display(int data[], size_t n);

//Global Variables - should not be used without good reason.

int main ()
{
// Declaration section
const size_t size = 6;
int num[size];
srand (time (0) );

// Executable section
instruct ();

for (int i = 0; i < size; i++)
num[i] = rand() % 50;

cout << “Unsorted Array” << endl;
display(num,size);

mergesort(num, size);

cout << “\n\nSorted Array” << endl;
display(num, size);

pause ();
return 0;
}

void mergesort(int data[], size_t n)
{
size_t n1;
size_t n2;

if (n > 1)
{
n1 = n / 2;
n2 = n - n1;

mergesort(data, n1);
mergesort((data + n1), n2);

merge(data, n1, n2);
}
}

void merge(int data[], size_t n1, size_t n2)
{
int *temp;
size_t copied = 0;
size_t copied1 = 0;
size_t copied2 = 0;
size_t i;

temp = new int[n1 + n2];

while ((copied1 < n1) && (copied2 < n2))
{
if(data[copied1] < (data + n1)[copied2])
temp[copied++] = data[copied1++];

else
temp[copied++] = (data + n1)[copied2++];
}

while (copied1 < n1)
temp[copied++] = data[copied1++];
while (copied2 < n2)
temp[copied++] = (data + n1)[copied2++];

for (i = 0; i < n1 + n2; ++i)
data[i] = temp[i];
delete [] temp;

}

void display(int data[], size_t n)
{
int row = 0;

for (int i = 0; i < n; i++)
{
cout << data[i] << ” “;

row++ ;
if (row % 10 == 0 )
{
cout << “\n”;
}

}
}

void instruct (void)
{
// Declaration section

// Executable section
}

void pause ()
{
// Declaration section

// Executable section
cout << “\nPress any key to continue…”;
getch();
cout << “\r”;
cout << ” “;
cout << “\r”;
}

/*
Program Output

Unsorted Array
40 16 23 13 23 6

Sorted Array
6 13 16 23 23 40
Press any key to continue…

*/

Socket (Using UDP)

Socket (Using UDP) Thursday, December 14th, 2006

When We creating sockets using UDP Protocol, we can Use the Following APIs

bind
connect
sendto
recvfrom

bind and connect are same as in TCP.

sendto:

Syntax:
int sendto ( int sid , const void *bufferPtr,size_t bufferLength , int flag ,
struct sockaddr *addrPtr , socklen_t addrLength )

Here,
Send a buffer, bufferPtr, of length bufferLength to address specified by addrPtr of size addrLength. Returns number of bytes sent or -1 on error.

recvfrom:

Syntax:
int recvfrom ( int sid , void *bufferPtr , int bufferLength ,
int flag , sockaddr *addrPtr ,int *addrLengthPtr )

Here,
Receive a buffer in bufferPtr of maximum length bufferLength from an unspecified sender.
Sender address returned in addrPtr, of size *addrLengthPtr.
Returns number of bytes receive or -1 on error.

Sockets in C++

Sockets in C++ Thursday, December 14th, 2006

Sockets are used to create connection between processes.
we can create socket using TCP or UDP. In Connection based (TCP), socket communicate client and server.We done this by using some of socket APIs

In Server Side:

1. socket
2. bind
3. listen
4. accept
5. send
6. recv
7. shutdown
8. close

In Client Side:

1. socket
2. connect
3. send
4. recv
5. shutdown
6. close

1.Socket
Used for creating socket.
Syntax:
int socket ( int domain , int type , int protocol ) ;
Here,

domain - AF UNIX(address format is UNIX pathname) or
AF INET (address format is host and port number)
type - SOCK_STREAM (Establishes a virtual circuit for stream)
SOCK_DGRAM (Establishes a datagram for communication)
SOCK SEQPACKET( Establishes a reliable, connection based,
two way communication with maximum message size.)
protocol - usually zero, so that type defines the connection within domain.

bind:
Give the address of the socket on the server

Syntax:
int bind ( int sid , struct sockaddr *addrPtr , int len )

Here:
sid - socket id
addrPtr - a pointer to the address family dependent address structure
len - size of *addrPtr

listen:
Specifies the maximum number of connection requests that can be pending for this process

Syntax:
int listen ( int sid , int size ) ;

accept:
Establish the connection with a specific client
Syntax:
int accept ( int sid , struct sockaddr *addrPt r , int * lenPt r )

send,recv:
Stream-based equivalents of read and write

Syntax:
int send ( int sid , const char *bufferPtr ,int len , int flag )
int recv ( int sid , const char *bufferPtr ,int len , int flag )

connect:
Client requests a connection request to a server

Syntax:
int connect (int sid , struct sockaddr *addrPt r , int len )

shutdown:
End reading or writing

Syntax:
int shutdown ( int sid , int how)

close:
Release kernel data structures


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.