Tuesday, January 17, 2012

Standard Deviation using Function


#include<stdio.h>
#include<conio.h>
#include<math.h>
float mean(int a[],int n);
float std(int a[],int n,float m);
void main()
{
float m,sd;
int n,a[10],i;
clrscr();
printf(“\nenter the number of values\n”);
scanf(%d”,&n);
printf(“\n enter the elements\n”);
for(i=0;i<n;i++)
scanf(%d”,&n);
m=mean(a,n);
printf(“mean=%f\n”,m);
sd=std(a,n,m);
printf(“\n sd=%f”,sd);
getch();
}
flaot mean (inta[],intn)
{
float f;
int sum=0;
for(i=0;i<n;i++)
sum=sum+ a[i];
f=(float)sum/n;
return f;
}
float std(int a[],int n,float m)
{
int i;
float std,sum=0.0,d;
for(i=0;i<n;i++)
{
d=a[i]-m;
a=d*d;
sum=sum+d;
}
sd=sqrt(sum\n);
return sd;
}

Output:
enter the number of values
5
enter the elements
2
4
6
8
10
mean=6.000000
sd=2.828427 

Substring Replacement


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],str1[15],str2[15],temp[50];
char *ptr;
int cnt;
clrscr();
printf(“enter a line of text..... \n”);
gets(str);
printf(“enter the string to be replaced... \n”);
gets(str1);
printf(“enter the replacing string...);
gets(str2);
printf(“\n the replaced line of the text....);
while(1)
{
ptr=strstr(str,str1);
if(ptr==?\o?)
break;
cnt=ptr-str;
strncpy(temp,str,cnt);
temp[cnt]=?10?;
strcat(temp,str+cnt+strlen(srt1));
strcpy(str1,temp);
puts(str);
}
getch();
}

Output:
enter the line of text... i love india
enter the string to be replaced..india
enter the replacing string...my parents
the replaced line of text
i love my parents