Sunday, February 13, 2011

Inline function

Inline functions are a crucial function in C + + and is often used with classes. Inline functions are functions where the call is made to all inline functions. The actual code that will be placed in the calling program. In a program called a function in the program jumps to the address of the function and when it reaches the end of the function it returns. This jump is actually much more involved and time consuming. But when an inline function is called the compiler replaces the call with the function code. So, in fact, at this point there will be no function calls, only the code of the function. No need to jump to different addresses. The general format of inline function is:

inline datatype function_name(arguments)

Example:
The following code creates and calls an inline function:
#include<iostream.h>
inline int average(int a, int b)
{
   return (a + b) / 2;
}
void main()

{
   int result = average(12, 14);

   cout << "The average of number 12, 14 is " << result << "\n";
   getch();

}
It 's very important to know that line is specified in the request, not a command for the translator. If for various reasons, the compiler is able to meet the demand, the operation has been translated into normal operation.

Thursday, February 10, 2011

What is macros in C

A macro is a fragment of code that took its name. When we use the name of the macro-program, which replaces the contents of the macro. You can specify any valid identifier as a macro, even if AC is the key word. There are two types of macros. One is a macro-like object, and the second is a function-like macro. Macro-object as the parameters of the function-like macros do not. The general syntax to declare a symbol as a macro for each of:
For object-like macros:
 #define <identifier> <replacement token list>
For example:
#include<stdio.h>
#include<conio.h>
#define sum a+b
void main()
{
Clrscr();
int a=5,b=10,c;
c=sum; //if we give the semicolon (;)when we define the macro; then we cannot give the semicolon when we write the name of identifier. If we give the semicolon when we define the macro, then we give the semicolon when we write the name of identifier in program
printf(“The sum of a+b is: %d”,c);
getch();}
For function-like macros:
#define <identifier> (<parameter list>) <replacement token list>
For example:   
#include<stdio.h>
#include<conio.h>
#define square(x) x*x;
Void main()
{
Clrscr();
int i=2,j;
j=square(i) //Here is no semicolon because I give the semicolon when I define the macros.
printf(“The value of j is: %d”,j);
getch();
}
 The #undef directive removes the definition of a macro.
Written by arnob;

Thursday, December 9, 2010

Reverse an integer by using Recursion

#include<stdio.h>
#include<conio.h>
int re (int,int,int*,int);
void main()
{
clrscr();
int i,j=0,n[5],k=0,m;
printf("Enter a number: ");
scanf("%d",&i);
m=re(i,j,n,k);
for(j=0;j<m;j++)
printf("%d",n[j]);
getch();
}
re(int i,int j,int *n,int k)
{
if(i==0) return k;
j=i%10;
n[k]=j;
i=i/10;
re(i,j,n,k+1);
program written by arnob;
Writer a C program that stores the student's name, identity and GPA. and do a serach possibility that when we give a name to search if the name is so show that the student's name, identity and GPA. If the name is not there then shows NOT FOUND



#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[40];
char id[20];
char gpa[5];
}stu[100];
void main()
{
clrscr();
int i=0,j,n;
char name[40];
printf("How many students informatio: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name: ");
scanf("%s",stu[i].name);
printf("\nEnter the id: ");
scanf("%s",stu[i].id);
printf("\nEnter the gpa: ");
scanf("%s",stu[i].gpa);
clrscr();
}
printf("\nEnter the search name: ");
scanf("%s",name);
for(i=0;i<n;i++)
{
clrscr();
j=strcmp(name,stu[i].name);
if(j==0)
{
printf("\nName :");
puts(stu[i].name);
printf("\nId :");
puts(stu[i].id);
printf("\nGPA :");
puts(stu[i].gpa);
break;
}
}
if(j!=0)
printf("Not found !");
getch();
}

program written by arnob;

Friday, December 3, 2010

47. Find the hight and second hight number by using RECURSION

/* Find the hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i,j;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int*);
j=hight(n,0,0,a);
printf("The hight number is %d",j);
return 0;
}

int hight(int n,int i,int m,int *a)
{
if(i>n) return m;
if(m<a[i])
m=a[i];
hight(n,i+1,m,a);
}


/* Find the second hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int,int*);
hight(n,0,0,0,a);
printf("The second hight number is %d",a[1]);
return 0;
}

int hight(int n,int i,int j,int k,int *a)
{
if(i>n-1) return 0;

for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
hight(n,i+1,j,k,a);
}


written by anrob.

46. Find the even number from a array by using RECURSION

Question: Get input a array frmon user and find the hight number. You can only use one array and can not use any globel variable.

Ans: If you can use more then one array then you can easyle find the hight number by only using for loop. But you can not use more then one array. So you can solve this problem by using recursion.

/*program for find the even number */

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

int main()
{
clrscr();
int a[100];n,i;
printf("How many number you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number: ");
scanf("%d",&a[i]);
}
void chk_evn(int,int,int*,int*)
chk_evn(0,0,&n,a);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}

void chk_evn (int i,int j,int *n,int *a)
{
if(i==n){*n=j;return n;}
if(a[i]%2==0)
a[j++]=a[i];
chk_evn(i+1;j,n,a);
}

program written by arnob

Thursday, November 11, 2010

45. Passing 2-D Array to a Function by using pointer

There are two ways in which er can pass a 2-D array to a function by using pointer. These are illustrated in the following program.

/*Two ways of accessing a 2-D array*/
#include<alloc.h>
void main()
{
int a [3][4]={
               1,2,3,4,
               5,6,7,8,
               9,0,1,6
             };
clrscr();
display(a,3,4);
show(a,3,4);
}

display(int *q,int row, int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d",*(q+i*col+j));
printf("\n");
}
printf("\n");
}

show(int (*q)[4],int row, int clo)
{
int i,j;
int *p;

for(i=0;i<row;i++)
{
p=q+i;
for(j=0;j<col;j++)
printf("%d",*(p+j));
printf("\n");
}
printf("\n");
}

And here is the output...

1234
5678
9016

1234
5678
9016

In the display() finction we have collected the base address of the 2-D array being passed to it in an ordinary int pointer. Thenthrough the two for loops using the ex[ression *(q+i*col+j) we have reached the appropriate element in the array. Suppose i is equal to 2 and j is equal to 3, then we wish to reach the element give this element or not. The exprewssion *(q+i*col+j) becomes *(4001+2*4+3). This turns out to be *(4001+11). Since 4001 is address of an integer, *(4001+11) turns out to be *(4023). Value at this address is +. This is indeed same as a[2][3]. A more general formula for accessing each array element would be:

*(base address + row *no of columns +column no)

In the show( ) function we have defined q to be a pointer to an array of 4 integers through the declaration
int (*q)[4];

To begin with, q hods the base address of the xeroth 1-D array, i.e 4001. This address is then assigned to p, an int pointer, and then using this pointer all elements of the xeroth 1-D array are accessed. Next thime through the loop when i takes a value 1, the expression q+i fetches the address of the first 1-D arry. This is because, q is a pointer to zeroth 1-D array and adding 1 to it would given us the address of the next 1-D array. This address is once again assigned to p, and using it all elements of the next 1-D arraY are accessed. 

Arnob