Monday, November 19, 2012

Simple Thread program in c++

//Filename: SimpleThreadTest.cpp
#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  cout << "Start Time: " << time(NULL) << endl;


  HANDLE myThread = (HANDLE)_beginthread(myThreadFunction, 0, (void*)5);


  WaitForSingleObject(myThread, 100000);
  return 0;
}


______________________________________________________________________________________

//Filename: SimpleMultiThreadTest.cpp

#include <iostream>
#include <windows.h>
#include <process.h>
#include <time.h>

using namespace std;

void myThreadFunction(void *p)
{
  int n = (INT_PTR)p;
  Sleep(n * 1000);
  cout << "Ending Thread At: " << time(NULL) << endl;
}

int main(int argc, const char* argv[])
{
  srand(time(NULL));
  const int threadSize = 5;
  HANDLE myThreads[threadSize];
  for(int i = 0; i < threadSize; i++)
      myThreads[i] = (HANDLE)_beginthread(myThreadFunction, 0, (void*)(rand() % 10 + 1));

  for(int i = 0; i < threadSize; i++)
      WaitForSingleObject(myThreads[i], 100000);
  return 0;
}

Friday, November 9, 2012

program to understand actual parameter and formal parameter

  • Formal and actual parameter are associated with the concept of 'call by value' and 'call by reference'
  • see the effect of 'call by value' and 'call by reference'
====================================================
//this program to illustrate call_by_reference
#include <iostream>
using namespace std;

int add(int &);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int &a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
3
9


====================================================== 

// this program to illustrate call_by_value
#include <iostream>
using namespace std;

int add(int);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
2
9 

======================================================
// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'

#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
   
    add(a, b);     //actual parameter
}

Friday, November 2, 2012

program to find GCD using recursion

//also help to understand the command line argument

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

int gcd(int, int);

int main(int argc, const char* argv[])       //command line argument
{
    int a, b;
    a = atoi(argv[1]);
    b = atoi(argv[2]);
   
    printf("The greatest commaon divisor of %d and %d is %d\n", a, b, gcd(a,b));
    return 0;
}

int gcd(int a, int b)
{
    if (b == 0)
        return a;                   //recursion implementation
    else
        return gcd(b, a % b);

}


 

Thursday, November 1, 2012

A simple program to understand recursion

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

void recTest(int i)
{
    if(i > 0)
    {
        recTest(i - 1);          //understand this crazy little insane item
        printf("%i\n", i);
    }
}

int main()
{
    recTest(5);
}


OUTPUT:
1
2
3
4
5