Tuesday, December 21, 2010

How the compiler treats an array internally ??

Solution: Let us suppose a 2 D  Array of type interger defined by the user as given below:
                          int array[5] [10] ; --------- 1)
   
  Complier will treat an array internally as given below:
                         int *(*(p+5)+10);

  However one cant declare an array  as the compiler treates it internally... Any1 can have try on it and compiler will give error . e.g
                       int *(*(p+5)+10)  will give an error while declaring array.

However You can use such expressions in sc panf and printf statements to enter and access elements of array.
Example:
#include<stdio.h>
#include<conio.h>
int func(int *,int);
  int arr[]={1,2,3,4,5};
int main()
{
    func(arr,5);
   getch();
   return 0;
}
int func(int *a,int z)
{
   int i;
   for(i=0;i<z;i++)
   {
      printf("%d\n",*(arr+i)); // making use of the concept i hav discussed above
    }
}

Thursday, December 16, 2010

Newton Forward Interploation (Numerical Methods)

// Newton Foward interpolation
#include<stdio.h>
#include<conio.h>
int main()
{
  int i,j,num;
  float data[10][10],u,abc,x,ch1,ch=1;
  printf("\n *** Welcome to the calculation in Newton Forward Interpolation ***");
  printf("\n\n Enter the number of values do u wanna Enter:\n Enter Your Choice:");
  scanf("%d",&num);
  for(i=0;i<num;i++)
  {
  printf("\n Enter  data value x(%d) is:",i+1);
  scanf("%f",&data[i][0]);
  }
  for(i=0;i<num;i++)
  {
  printf("\n Enter function value y(%d)is:",i+1);
  scanf("%f",&data[i][1]);
  }
  for(j=2;j<num+1;j++)
  {
   for(i=0;i<num-1;i++)
   {
   data[i][j]=((data[i+1][j-1])-(data[i][j-1]));
   }
  }
   printf("\n The Forward difference Table is:");
   printf("\n x            f(x)");
   for(i=0;i<num;i++)
   {
   printf("       %dDD  ",i);
    // if(i==4)
    // break;
   }
   for(i=0;i<num;i++)
   {
    printf("\n");
    for(j=0;j<num+1-i;j++)
    {
    printf(" %f",data[i][j]);
    }
     }
    printf("\n\n Enter the value of x:\n Enter Your Choice:");
    scanf("%f",&x);
    abc=((data[1][0])-(data[0][0]));
    u=(x-data[0][0])/abc;
    ch1=data[0][1];
    for(i=1;i<num;i++)
    {
    ch=ch*((u-(i-1))/i);
    ch1=ch1+(data[0][i+1])*(ch);
    }
    printf("\n\n\t Hence The value at the function at x=%f is =%f",x,ch1);
    printf("\n\n\t\t\t *** Bye Bye ***");
  getch();
  return 0;
}
Garry

NEWTON BACKWARD INTERPOLATION( NUMERICAL METHODS)

// Newton Backward Interpolation
#include<stdio.h>
#include<conio.h>
int main()
{
  int i,j,num;
  float data[10][10],u,abc,x,ch1=0.0,ch=1;
  //char str[80];
  printf("\n *** Welcome to the calculation in Newton Backward Interpolation***");
  printf("\n\n Enter the number of values do u wanna Enter:\n Enter Your Choice:");
  scanf("%d",&num);
  printf("\n\n Enter the set of data values:");
  for(i=0;i<num;i++)
  {
  printf("\n Enter  data value x(%d) is:",i+1);
  scanf("%f",&data[i][0]);
  }
  printf("\n\n\t *** Enter The Set Of Function Values ***");
  for(i=0;i<num;i++)
  {
  printf("\n Enter function value y(%d)is:",i+1);
  scanf("%f",&data[i][1]);
  }
  for(j=2;j<num+1;j++)
  {
   for(i=0;i<num-1;i++)
   {
   data[i][j]=((data[i+1][j-1])-(data[i][j-1]));
   }
  }
   printf("\n The Newton Backward difference Table is:");
   printf("\n x        f(x) ");
   for(i=0;i<num;i++)
   {
   printf("       %dDD  ",i);
    // if(i==4)
    // break;
   }
   for(i=0;i<num;i++)
   {
    printf("\n");
    for(j=0;j<num+1-i;j++)
    {
    printf(" %f",data[i][j]);
    }
     }
    printf("\n\n Enter the value of x:\n Enter Your Choice:");
    scanf("%f",&x);
    abc=((data[num-1][0])-(data[num-2][0]));
    u=((x)-(data[num-1][0]))/abc;
    ch1=data[num-1][1];
    for(i=1;i<num;i++)
    {
    ch=ch*((u+(i-1))/(i));
    //ch1=ch1+(data[0][i+1])*(ch);
    ch1=ch1+((data[num-i-1][i+1])*(ch));
    }
    printf("\n\n\t Hence The value at the function at x=%f is =%f",x,ch1);
    printf("\n\n\t\t\t *** Bye Bye ***");
  getch();
  return 0;
}
Gurpreet Singh

Child window on a parent window client area with caption chaging facility

#include<windows.h>


#include "resource.h"


#define ID_ABC 21


LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

LRESULT CALLBACK ChildProc(HWND,UINT,WPARAM,LPARAM);


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)


{

HWND hwnd;

MSG msg;

WNDCLASS w;

if(!hPrevInstance)


{

w.cbClsExtra=0;

w.cbWndExtra=0;

w.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

        w.hIcon=LoadIcon(NULL,IDC_ARROW);

w.hCursor=LoadCursor(NULL,IDI_APPLICATION);

w.hInstance=hInstance;

w.lpfnWndProc=WndProc;

w.lpszClassName=TEXT(
"MYCLASS");


w.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);

w.style=CS_HREDRAW|CS_VREDRAW;

if(!RegisterClass(&w))


{

return 0;


}

   }

  w.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

  w.lpfnWndProc=ChildProc;

  w.lpszClassName=TEXT(
"ChildClass");


  w.lpszMenuName=NULL;

 
if(!RegisterClass(&w))


{

return 0;


}

  hwnd=CreateWindow(TEXT(
"MyClass"),TEXT("WINDOW"),WS_OVERLAPPEDWINDOW,10,20,500,500,NULL,NULL,hInstance,NULL);


  ShowWindow(hwnd,nCmdShow);

  UpdateWindow(hwnd);

 
while(GetMessage(&msg,NULL,NULL,NULL))


  {

  TranslateMessage(&msg);

  DispatchMessage(&msg);

  }

 
return msg.wParam;


}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam)

{

static BOOL hcreate=TRUE;


static HWND hchild;


HDC hdc;

PAINTSTRUCT ps;

RECT rect;

switch(message)


{

case WM_CREATE:


return 0;


case WM_COMMAND:


switch(wParam)


{

case ID_CREATE:


if(hcreate==TRUE)


{

hcreate=FALSE;

hchild=CreateWindow(TEXT(
"ChildClass"),TEXT("CHILD"),WS_CHILD|WS_BORDER|WS_THICKFRAME|WS_SIZEBOX|WS_VISIBLE|WS_CAPTION,20,50,100,100,hwnd,NULL,(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);


}

break;


case ID_SEND:


SendMessage(hchild,WM_USER,0,0L);

break;


}

break;


case WM_PAINT:


hdc=BeginPaint(hwnd,&ps);

GetClientRect(hwnd,&rect);

TextOut(hdc,10,20,TEXT(
"Hello Sir Jiiiiiiii"),2);


EndPaint(hwnd,&ps);

break;


case WM_DESTROY:


PostQuitMessage(0);

break;


}

return DefWindowProc(hwnd,message,wParam,lParam);


}


LRESULT CALLBACK ChildProc(HWND h,UINT m,WPARAM w,LPARAM l)

{

static BOOL p=FALSE;


//static HANDLE hpr;


     //static HWND hpush;


//HINSTANCE his;


HDC hdc;

PAINTSTRUCT ps;

RECT rect;

switch(m)


{

  
if(!p)


   {

   p=TRUE;

  

case WM_CREATE:


//hpr=GetParent(h);




CreateWindow(TEXT(
"BUTTON"),TEXT("PUSH IT"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_BORDER,10,50,70,20,h,(HMENU) ID_ABC,(HINSTANCE)GetWindowLong(h,GWL_HINSTANCE),NULL);


     

}

break;


case WM_USER:


SetWindowText(h,TEXT("THIS IS A CHILD WINDOW DUDE"));


break;


case WM_COMMAND:


switch(w)


{

case ID_ABC:


MessageBox(h,TEXT("i m tired "),TEXT("WINDOW"),MB_OK);


break;


}

        
break;


     case WM_PAINT:


hdc=BeginPaint(h,&ps);

GetClientRect(h,&rect);

TextOut(hdc,20,30,TEXT(
"Hello Sir Jiiiiiiii u re in child window now"),29);


EndPaint(h,&ps);

break;


case WM_DESTROY:


PostQuitMessage(0);

break;


}

return DefWindowProc(h,m,w,l);


}


Gurpreet Singh

FIRST COME FIRST SERVE ( CPU Schdeuling)

#include<stdio.h>
#include<conio.h>
int temp,temp1,burst[10],arrival[10],waiting[10];
int i,j,num;
float Awt,Tat;
char pname[20][20];
void inputdata();
void fcfs();
void calculatedata();


void inputdata()
{
      printf("\n Enter the Number of processes:\n Enter Your Choice:");
      scanf("%d",&num);
      printf("\n Enter Info Of Each Process:");
      for(i=1;i<=num;i++)
      {
         fflush(stdin);
         printf("\n Enter the name of process:");
         scanf("%[^\n]s",pname[i]);
         printf("\n Enter the Burst time for process %s=",pname[i]);
         scanf("%d",&burst[i]);
          printf("\n Enter the Arrival time for process %s=",pname[i]);
         scanf("%d",&arrival[i]);
         }
      }
     
 void calculatedata()
 {
      waiting[1]=0;
      int Ttt=0; Att=0;
      for(i=2;i<=num;i++)
      {
        waiting[i]=(waiting[i-1]+burst[i-1]);
          }
      for(i=1;i<=num;i++)
      {
          Ttt=Ttt+(waiting[i]-arrival[i]);
          Att=Att+((waiting[i]+burst[i])-arrival[i]);        
       }            
      Awt=float(Att)/num;
      Tat=float(Ttt)/num;
      printf("\n\t\t Hence the average waiting time is=%3.2f",Awt);
      printf("\n\t\t Hence the average turn arround time is=%3.2f",Tat);
      }  
 void fcfs()
 {
      for(i=1;i<=num;i++)
      {
         for(j=i+1;j<=num;j++)
         {
            if(arrival[i]>arrival[j])
            {
              temp=arrival[i];
              temp1=burst[i];
              arrival[i]=arrival[j];
              burst[i]=burst[j];
              arrival[j]=temp;
              burst[j]=temp1;
            }
         }
        }
       calculatedata(); 
     
   }    
int main()
{
     printf("\n\t\t *** Welcome to first come first serve algorithm ***");
     printf("\n\n Enter Data:");
     inputdata();
     fcfs();
     getch();
     return 0;
}
Gurpreet Singh

FIFO(Memory Management)

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,k,l,page[10],buf,buff[10],pagefault,num,temp,flag=1;
    printf("\n Enter the number of paging sequence do u wanna enter:");
    scanf("%d",&num);
    printf("\n\n Enter the paging sequence:");
    for(i=0;i<num;i++)
    {
            printf("\n page %d.",i+1);
            scanf("%d",&page[i]);
    }
    printf("\n\n Enter the buffer size:");
    scanf("%d",&buf);
    for(j=0;j<buf;j++)
    {
            buff[j]=0;
    }
    pagefault=0;
    k=0;
    flag=1;
    for(i=0;i<num;i++)
    {
                      flag=1;
                      for(j=0;j<buf;j++)
                      {
                                        if(buff[j]==page[i])
                                        {
                                          flag=0;
                                          break;
                                        }
                                       
                      }
          j=0;
          if(flag==0)
          {
                     continue;
         
          }
          else
          {
              if(k <buf)
              {
                   buff[k]=page[i];
                   k++;
                   pagefault++;
                   printf("\n Now Pages Are:");
                   for(l=0;l<buf;l++)
                   {
                     if(buff[l]!=0)
                     {
                                   printf("    %d",buff[l]);
                     }
                   }
                   continue;
                   
               }
              for(j=0;j<buf-1;j++)
              {
                                  temp=buff[j+1];
                                  buff[j+1]=buff[j];
                                  buff[j]=temp;
              }
              buff[buf-1]=page[i];
              pagefault++;
               printf("\n Now Pages Are:");
                   for(l=0;l<buf;l++)
                   {
                     if(buff[l]!=0)
                     {
                                   printf("    %d",buff[l]);
                     }
                   }
                   }
          }
          printf("\n\n\t Hence the page faults are=%d",pagefault);
   
   getch();
   return 0;
}
Gurpreet Singh

Round Robin (OPERATING SYSTEM)

#include<stdio.h>
#include<conio.h>
int main()
{
    int i ,j,waiting[3],last[3],lefttime[3],burst[3];
    float awt=0.0;
    int gap=0;
    int sum=0;
   
    printf("\n\t Enter the process information");
    for(i=0;i<3;i++)
    {
      fflush(stdin);
       printf("\n\n\t Process %d",i+1);
       printf("\n\n Enter Burst Time:");
       scanf("%d",&burst[i]);
      
       waiting[i]=0;
       lefttime[i]=0;
       last[i]=0;
    }
    printf("\n\n Enter the interval time:");
    scanf("%d",&gap);
    printf("\n\n gap=%d", gap);
    for(i=0;i<3;i++)
    {
     sum=sum+burst[i];     
    }
        printf("\n\n sum=%d", sum);
    for(i=0;i<3;i++)
    {
       lefttime[i]=burst[i];
    }
    j=0;
    i=0;
    while(1)
    {
        if(lefttime[j]>0)
        {
           if(lefttime[j]<=gap)
           {
              waiting[j]=waiting[j]+(i-last[j]);
              i=i+lefttime[j];
              last[j]=i+lefttime[j];
              lefttime[j]=0;
           }
           else if(lefttime[j]>gap)
           {
              waiting[j]=waiting[j]+(i-last[j]);
              i=i+gap;
              last[j]=i;
              lefttime[j]=lefttime[j]-gap;
           }
        }
        if(i>=sum)
        break;
        if(j==2)
        j=0;
        else
        j++;
    }
    for(i=0;i<3;i++)
    {
        printf("\n\nWaiting time for p%d = %d",i+1,waiting[i]);
       awt=awt+waiting[i];
      
    }
    awt=awt/3.0;
    printf("\n\n\t Hence the Average waiting time =%.3f",awt);
   
    getch();
    return 0;
}

Gurpreet Singh

*** To geneare assembly of any c|c++ program using DEV-C ***

step1:
 write a simple c|c++ program using dev-c compiler

step2:
save it in bin directory

step3:
open command prompt

step4:
type cd..
again type cd..

step5:
go to C: \Dev-Cpp\bin

step6:
write gcc|g++ -o -op <filename>

step7:
op><filename.exe>

step8:
write gcc|g++ -S <filename>

step9:
go to dev-c

step10:
open c|c++ program for which you are generating assembly language...

step11:
open <filename.s> this will be nothing but the assembly of the c|c++ program which u hav made ....

The assmbely tht it will generte will be of complier dependent... becoz different compliers will generate different assembly of the c|c++ program ... according to the issue whether they are 32 bit or 64 bit....
thts all about generating the assembly of any c|c++ program...




Gurpreet Singh