Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Sunday, August 21, 2011

Program to pass a structure variable to Function.

//Program to pass a structure variable to Function.
#include<stdio.h>
#include<conio.h>
 struct student
 {
  int rn;
  char name[20];
 }s;
 void main()
 {
 void display(struct student m);
 printf("Enter the rollno and name of student");
 scanf("%d%s",&s.rn,&s.name);
 display(s);
 getch();
}
void display(struct student m)
{
printf("%d %s",m.rn,m.name);
}

Saturday, August 20, 2011

Program to enter line using getchar and print it using putchar function.

//Program to enter line using getchar and print it using putchar function.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j;
char c[50],ch;
clrscr();
printf("\n Enter the line: ");
while(ch!='\n')
{
 ch=getchar();
 c[i]=ch;
 i++;
}
printf("\n Entered line is :");
for(j=i-1;j>=0;j--)
{
 putchar(c[j]);
}
getch();
}

Print Fibonacci series using while loop.

//Program to print Fibonacci series using while loop.
#include<stdio.h>
#include<conio.h>
void fib(int);
void main()
{
 int n;
 clrscr();
 printf("Enter the number upto which u want to find fibnacci series");
 scanf("%d",&n);
 fib(n);
 getch();
}
void fib(int x)
{
int a=0,b=1,c;
c=a+b;
printf(" %d  %d ",a,b);
while(c<=x)
{
printf(" %d ",c);
a=b;
b=c;
c=a+b;
}
}

Sort an array using Bubble sort.

//Program to sort an array using Bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
  int *a,n,i,t,k;
  clrscr();
  printf("\nEnter the no. of element in the array: ");
  scanf("%d",&n);
  a=(int *)malloc(n* sizeof(int));
  printf("\nEnter the values of %d elements: ",n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(k=0;k<n-1;k++)
  {
    for(i=0;i<n-1-k;i++)
    {
      if(a[i]>a[i+1])
      {
       t=a[i];
       a[i]=a[i+1];
       a[i+1]=t;
      }
    }
  }
 printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
      printf("\n %d",a[i]);
  }
  getch();
}

Program to find Multiplication of two matrices created by using concept of malloc.

//Program to find Multiplication of two matrices created by using concept of malloc.
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int mul(int **,int,int,int **,int);
void main()
{
 int **a,**b,**c,m,n,p,q,i,j;
 clrscr();
 printf("\n Enter the number of rows in 1st  matrix:");
 scanf("%d",&m);
 printf("\n Enter the number of columns in  1st matrix:");
 scanf("%d",&n);

 printf("\n Enter the number of rows in 2nd  matrix:");
 scanf("%d",&p);
 printf("\n Enter the number of columns in  2nd matrix:");
 scanf("%d",&q);

 a=(int**)malloc(m*sizeof(int*));
 for(i=0;i<m;i++)
 a[i]=(int*)malloc(n*sizeof(int));

 b=(int**)malloc(p*sizeof(int*));
 for(i=0;i<p;i++)
 b[i]=(int*)malloc(q*sizeof(int));

printf("Enter the elements of matrix a");
 for(i=0;i<m;i++)
  for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);

 printf("Enter the elements of matrix b");
 for(i=0;i<p;i++)
  for(j=0;j<q;j++)
   scanf("%d",&b[i][j]);

 if(n==p)
 {
 c=(int**)malloc(m*sizeof(int*));
 for(i=0;i<m;i++)
 c[i]=(int*)malloc(q*sizeof(int));

 c=mul(a,m,n,b,q);
 printf("matrix A:\n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",a[i][j]);
  printf("\n");
 }
 printf("matrix B:\n");
 for(i=0;i<p;i++)
 {
  for(j=0;j<q;j++)
   printf(" %d ",b[i][j]);
  printf("\n");
 }
  printf("mul of matrix:\n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<q;j++)
   printf(" %d ",c[i][j]);
  printf("\n");
 }
 }
 else
 {
 printf("multiplication not possible");
 }

 getch();
}
int mul(int **a,int m,int n,int **b,int q)
{
int i,j,k;
int **t;
 t=(int**)malloc(m*sizeof(int*));
 for(i=0;i<m;i++)
 t[i]=(int*)malloc(q*sizeof(int));

 for(i=0;i<m;i++)
 {
 for(j=0;j<q;j++)
 {
  t[i][j]=0;
  for(k=0;k<n;k++)
  { t[i][j]+=a[i][k]*b[k][j];}
 }
 }
 return t;
}

Program to find Multiplication of two matrices using function.

//Program to find Multiplication of two matrices using function.
#include<stdio.h>
#include<conio.h>
void mul();
void main()
{
 int a[3][3],b[3][3],i,j,m=3,n=3;
 clrscr();
 printf("Enter the elements of matrix a");
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);

 printf("Enter the elements of matrix b");
 for(i=0;i<m;i++)
  for(j=0;j<n;j++)
   scanf("%d",&b[i][j]);

 printf("matrix A:\n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",a[i][j]);
  printf("\n");
 }
 printf("matrix b:\n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",b[i][j]);
  printf("\n");
 }

 mul(a,m,n,b);
 getch();
}

void mul(a,m,n,b)
int m,n, a[][3],b[][3];
{
int i,j,k;
int c[3][3];
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 {
  c[i][j]=0;
  for(k=0;k<n;k++)
  { c[i][j]+=a[i][k]*b[k][j];}
 }
 }
  printf("mul of matrix:\n");

  for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   printf(" %d ",c[i][j]);
  printf("\n");
 }
}

Friday, August 19, 2011

Program to recieve the address of variable using pointer through functions.

//Program to recieve the address of variable using  pointer through functions.
#include<stdio.h>
#include<conio.h>
main()
{
 int a,b;
 clrscr();
 printf(" enter the value of a and b:");
 scanf("%d%d",&a,&b);
 printf("\n values of a and b before swapping");
 printf("\n a=%d b=%d",a,b);
 void swap(int *x,int *y);
 swap(&a,&b);
 printf("\n values of a and b after swapping");
 printf("\n a=%d b=%d",a,b);
 getch();
}
void swap(int*x,int *y)
{
 int t;
 t=*x;
 *x=*y;
 *y=t;
}

Program to find the Factorial of number using Recursion function.

//Program to find the Factorial of number using Recursion function.
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int x,temp;
clrscr();
printf("\n \n enter the value of n: ");
scanf("%d",&x);
temp=fact(x);
printf("\n The value of the factorial is %d",temp);
getch();
}
int fact(int n)
{
   int value;
   if(n==1)
    return(n);
   else
   {
     value=n*fact(n-1);
     return(value);
   }
}

Passing 2D array to a function

                 2D arrays are passed to a function in the same manner as that of one dimensional array. To pass a 2D array to a function, array name must be specified without any bracket, as an actual argument within the function call. The corresponding formal argument in the called function must be declared as an array with two size specification. The first size specialization may or may not be specified in the formal declaration, but empty pair of braces must be written with an array name. The other size specification needs to be specified.
E.g.
Int main()
{
Int x[10] [5];
……………………                       
Void func1 (int x[ ] [5], int , int);          //function declaration
……………………….                              
Func1 (x, 10, 5);      //function call         
}                                                             
Void func1 (int x[ ] [5], int n)        //no semi colon
{                                               
……………
}

Passing one dimensional array to function

                   It is possible to pass an array to a function. While passing an array to a function, the called function does not know the size of the array and neither does the compiler so we need to explicitly pass array size as a separate argument so that the called function know the specific number of array elements to be processed.
E.g.
Int main()
{
Int x[10];
……………………
Void func1 (int  [ ], int);          //function declaration
……………………….                                 
Func1 (x,10);      //function call         
}                                        
Void func1 (int x[ ], int n)        //no semi colon
{                                             
……………
}
When an array is passed to a function, it is passed by reference and not by value which means that the elements of an array are not copied to the called function but the array name is interpreted as the address of its first element. Since the elements of an array are stored in contiguous memory location so the called function can access all the elements of the original array. Therefore, any changes made to array elements within the called function can be reflected in the original array.

Next Home
Twitter Delicious Facebook Favorites More