Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2. Write a program to create a file with some integers and which reads the numbers &
main()
{
i
nt t=0,s=0,i,n,s,mean,t,d;
FILE *fp;
Fp=fopen(msd.txt,w+);
p
rintf(
\
n
Enter n:);
s
canf(%d,&n);
i=0;
while(n!=
1)
{
f
putw(fp,n)
s
canf(%d,&n);
I=i+1;
}
p
rintf(
\
n Total numbers written into the file: %d,i);
rewind(fp);
while((n=getw(fp)!=EOF)
s=s+n;
printf(
\
n Sum of all numbers is :%d,s);
mean=s/i;
p
rintf(
\
n Mean is : %d,mean);
fclose(fp);
fopen(msd.txt,r);
while((n=getw(fp)!=EOF)
t=t+pow((n
mean),2);
d=sqrt(t/i);
printf(
\
n Standard Deviation is: %d,d);
fclose(fp);
}
#include<stdio.h>
#include<conio.h>
main()
{
float xi[20],yi[20],num,den,x,y=0;
int i, j, n;
clrscr();
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the values of x and y:\n");
for(i=0;i<n;i++)
scanf("%f%f",&xi[i],&yi[i]);
printf("Enter value of x at which value of y is to be
calculated: ");
scanf("%f",&x);
for(i=0;i<n;i++)
{
num=1;
den=1;
for(j=0;j<n;j++)
if(j!=i)
{
num *= x-xi[j];
den *= xi[i]-xi[j];
}
y+ = (num/den)*yi[i];
}
printf("When x=%4.1f y=%f\n",x,y);
getch();
}
#include<st
dio.h>
#include<math.h>
float fval(float x);
main()
{
float x0,x1,x2,eval=0.001;
float f0,f1,f2,e;
int stage=1;
clrscr();
printf("Enter the values where the root lies: ");
scanf("%f %f",&x0,&x1);
f0=fval(x0);
f1=fval(x1);
if(f0*f1<0)
{
printf("\nItr\tx0\tx1\tf0\tf1\tx2\tf2\te\n");
printf("----------------------------------------------\n");
while(1)
{
x2=(x0+x1)/2;
f2=fval(x2);
e=fabs((x0-x1)/x1);
printf("%d\t%0.5f\t%0.5f\t%0.3f\t%0.3f",stage,x0,x1,f0,f1);
printf("\t%0.5f \t%0.3f \ t%f \n",x2,f2,e);
if(e<=eval)
break;
if(f0*f2<0)
{
x1=x2;
f1=f2;
}
else if(f0*f2>0)
{
x0=x2;
f0=f2;
}
else
if(f2==0)
break;
stage++;
}
}
else {
printf("Incorrect initial
values");
exit(0);
}
printf("\nSolution converges to a root %f in %d iterations.",x2,stage);
getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}