Wednesday, 4 April 2012

C programs using User Defined Functions



1) User defined functions to conduct a linear search.
    
 #include<stdio.h>
int n,i,a[10],key;

void input()
{
 printf("Enter the value of N =  ");
 scanf("%d",&n);

printf("Enter the numbers : \n ");
  for(i=0;i<n;i++)
{

  scanf("%d",&a[i]);
}}


void search()
{
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("The number %d is found\n",key);
exit(0);
}}
printf("The number %d is not found\n",key);
}


int main()
{
input();
printf("Enter the number to be searched :\n ");
scanf("%d",&key);
search();
}










2) User defined functions to sort numbers in ascending order using arrays.




#include<stdio.h>
int i,j,temp,a[10],n;

void input()
{
printf("Enter the value of N ;\n");
scanf("%d",&n);

printf("Enter numbers : \n");
for(i=0;i<n;i++)
{
 scanf("%d",&a[i]);
}
}

void sort()
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
   temp=a[j];
   a[j]=a[j+1];
   a[j+1]=temp;
}
}}}

void output()
{
for(j=0;j<n;j++)
{
printf("%d ",a[j]);
}}

int main()
{
input();

printf("The elements entered are ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
sort();
printf("Sorted elements are  ");
output();
}



3) User defined functions to compute mean, variance and standard deviation.

#include<stdio.h>
#include<math.h>
int n,a[20],i;
float v,m,sd;
 
int input()
{
printf("Enter the N value: \n");
scanf("%d",&n);
printf("Enter the numbers: \n");
for(i=0;i<n;i++)
{
   scanf("%d",&a[i]);
}
}
 
int mean()
{
 int sum=0;

for(i=0;i<n;i++)
{
 sum=sum+a[i];
}
m=sum/n;
}

int var()
{

float d;
for(i=0;i<n;i++)
{
d+=pow((a[i]-m),2);
}
v=d/n;
}

int s_d()
{
 sd = sqrt(v);
}

float output()
{
printf("Mean = %f \n",m);
printf("Variance = %f \n",v);
printf("Standerd deviation = %f \n",sd);
}

int main()
{
input();
mean();
var();
s_d();
output();
}






4) User defined functions to compute matrix multiplication
#include<stdio.h>
int a[10][10], b[10][10],mult[10][10],r1,c1,r2,c2,i,j,k;



void input()
{
printf("Enter the rows and columns of matrix A:\n");
scanf("%d %d",&r1,&c1);

printf("Enter the matrix A: \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}}
printf("Enter the rows and columns of matrix B :\n");
scanf("%d %d",&r2,&c2);

printf("Enter the matrix B: \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}}}



void output()
{
printf("The matrix entered A is \n");

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}



printf("The matrix entered is \n");

for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}}




void mul()
{


printf("The product of matrices A and B is \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=a[i][k]+b[k][j];
}
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}

int main()
{
input();
output();
if(c1==r2)

mul();
else
 printf("Matrix multiplication is not possible \n");
}



5) User defined functions to find the sum of the elements of specified row , column and sum of all the elements of a matrix.

#include<stdio.h>
int a[10][10],i,j,m,n,sumr,sumc,sumt,r,c;

void input()
{
printf("Enter the rows and columns of matrix :\n");
scanf("%d %d",&m,&n);

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


void output()
{
printf("The matrix entered is \n");

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



void sumrow()
{
printf("Enter the specified row :\n");
scanf("%d",&r);

for(j=0;j<n;j++)
{
sumr+=a[r-1][j];
}

printf("Row sum is %d\n",sumr);
}


void sumcol()
{
printf("Enter the specified column :\n");
scanf("%d",&c);

for(i=0;i<m;i++)
{
sumc+=a[i][c-1];
}

printf("column sum is %d\n",sumc);
}




void sumtot()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sumt+=a[i][j];
}
}
printf("sum of all elements is %d\n",sumt);
}


int main()
{
input();
output();
sumrow();
sumcol();
sumtot();
}


visit writehow.net for more cool stuff

Filled Under:

1 comments:

  1. Do Use "return 0" at the end of the program to know that program returns the desired output "0 = TRUE"

    #include
    #include
    int main()//void main() Can also Be Used
    {
    int a,b,c;
    clrscr();//To make this function work use #include

    //INPUT//
    printf("ENTER THE FIRST NUMBER : ");
    scanf("%d",&a);
    printf("\nENTER THE SECOND NUMBER : ");
    scanf("%d",&b);

    //FORMULA//
    c=a+b;

    //OUTPUT//
    printf("\nTHE SUM OF %d AND %d IS : %d\n",a,b,c);
    return 0;//Can use getch(); Function also If the Console window Disappears Immediately
    }
    //HINT: USING CALLING FUNCTIONS MAY SLOW DOWN THE WORKING OF PROGRAMMES
    // USE THEM WHEN YOUR WRITING A VERY BIG PROGRAMMES.
    //NOTE: getch(); FUCTION NEEDS TO IMPORT "conio.h" Header file by #include

    ReplyDelete