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;
}

Next Home
Twitter Delicious Facebook Favorites More