Sei sulla pagina 1di 13

1.

Write a program to count the number of alphabets, integers and special


characters in a file.
Algorithm:
Step 1: Start
Step 2: Initialize nos=0, noi=0, noc=0
Step 3: Read filename
Step 4: Open file in read mode to fp
Step 5: While ch=getc(fp)!=EOF
Step 5.1: If ch is a alphabet
Step 5.1.1: Increment noc by 1
Step 5.2:
Else If ch is a digit
Step 5.2.1: Increment noi by 1
Step 5.3: Else
Step 5.3.1: Increment nos by 1
Step 5.4: EndIf;
Step 6: EndWhile
Step 7
: Close the file
Step 8
: Print Number of alphabets = , noc
Step 9
: Print Number of integers = , noi
Step 10
: Print Number of special characters = , nos
Step 11
: Stop

2. Write a program to create a file with some integers and which reads the numbers &

determines the mean and standard deviation.


Algorithm:
Step 1: Start
Step 2: initialize t=0,s=0
Step 3: Open a
file in W+ mode, pointer to the file is fp
Step 4: Read a number as n
Step 5: initialize i=0 (count)
Step 6: repeat until n!=
1(
1 to terminate the loop)
Step 6.1: Write n into the File (i.e. , fputw(fp,n)
Step 6.2: read next number int n
Step 6.3: in
crement the counter i
Step 7: print the count i
Step 8: Rewind the file pointer to the beginning of the file (i.e., rewind(fp))
Step 9: repeat until (n=getw(fp))!= EOF(end of file)
Step 9.1: s=s+n ( s holds the sum of the numbers in the file)
Step 10:
print the sum i.e., s
Step 11: calculate mean=s/i
Step 12: print the mean i.e., mean
Step 13: close the file
Step 14: reopen the file in read mode
Step 15: repeat until (n=getw(fp))!= EOF(end of file)
Step 16: calculate t=t+pow((n-mean),2)
Step 17: d=sqrt
(t/i) (here d is the standard deviation)
Step 18: print the standard deviation i.e., d
Step 19: close the file
Step 20: Stop

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

Write a program to print and display the contents of a file in


reverse order.
Algorithm:
Step 1: Start

Step 2: Open the text file in


read
mode
Step 3
: Initialize n=1
Step 4
:
While character c from file is not End Of File
Step 5: EndWhile;
Step 6: Get the position of file pointer fp and store it in variable i
Step
7: Set the pointer at the end of the file and position n
Step 8: While i>=0
Step 8.1: Get character from the position at fp and store in c
Step 8.2: Print c
Step 8.3: Increment n by 1
Step 8.4: Decrement i by 1
Step 8.5: Move the position of file
pointer fp backwards n from end of file
Step 9: EndWhile;
Step 10: Stop

Write a program to implement Lagrange'sinterpolation.


Algorithm:
Step 1: Start

Step 2: Read n value


Step 3: For i= 0 to n do
Step 3.1: Read xi[i], yi[i]
Step 3.2: Increment i by 1
Step 4: EndFor;
Step 5: Read x value at which value of y is to be calculated.
Step 6: For i = 0 to n do
Step
6.1: Set num = 1 and den = 1
Step 6.2: For j = 0 to n do
Step 6.2.1: If j!=i
Step 6.2.1.1: Compute num= num*(x-xi[j])
Step 6.2.1.2: Compute den = den * (xi[i]xi[j])
Step 6.2.2: EndIf;
Step 6.3: EndFor;
Step 6.4:
Compute y =y +(num/den)*yi[i]
Step 7: EndFor;
Step 8: Display When x = , x, y = , y
Step 9: Stop

#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();
}

Write a program to find the square of given x2-25 by false-position method.


Algorithm:
Step 1: Start
Step 2: Read Initial approximate roots as x0 and x1
Step 3:
Find f0 and f1 by passing the x0 and x1 values to the function fval(), respectively
Step 4: Check is f0*f1<0, if true goto step 4.1, otherwise goto step 4.3
Step 4.1: r
epeat until true (i.e., infinite loop)
Step 4.1.1: Calculate x2 i.e., x2=(xo*f1-x1*fo)/(f1-f0)
Step
4.1.2: Find f2 by passing the x2 value to the function fval()
Step 4.1.3: find the error i.e., e=|f2|
Step 4.1.4: Print stage,x0,x1,f0,f1,x2,f2,e
Step
4.1.5: Check is e<=eval, if true goto Step 4.1.5.1, otherwise goto Step 4.1.5.2
Step 4.1.5.1: Display the solution converges at value x2 in stage
iterations, goto step 5
Step 4.1.5.2: Check is f1*f2<0, if true goto Step 4.1.5.2.1,
Otherwise goto 4.1.5.2.2
Step 4.1.5.2.1: set x1=x2 and f1=f2 goto step 4.1.6
Step 4.1.5.2.2: Check is f0*f2>0, if true goto Step 4.1.5.2.2.1,
Otherwise goto 4.1.5.2.2.2
Step 4.1.5.2.2.1: Check is f2==0, if true goto Step 4.1.5.2.2.1.1,
Otherwise goto 4.1.5.2.2.1.2
Step 4.1.5.2.2.1.1: goto step 4.1.5.1
Step 4.1.5.2.2.1.2: goto step 4.1.6

Step 4.1.5.2.2.2: set x0=x2 and f0=f2 goto step 4.1.6


Step 4.1.6: set stage=stage+1, goto step 4.1
Step 4.2: Display Incorrect Initial Values, goto step 5
Step 5: Stop
Function: fval(x)
Step 1: Begin
Step 2: return (x*x)-25
Step 3: End
Write a program to find the square of given x2-25=0 using bisection method.
Algorithm:
Step 1: Start
Step 2: Read initial approximate values x0,x1
Step 3: Substitute the values x0,x1 in the given function f(x) and calculate f(x0) and f(x1) respectively
Step 4: If both f(x0) and f(x1) are of opposite signs go to step 5
Otherwise go to step 6
Step 4.1: repeat until true (infinite loop)
Step 4.1.1: Calculate the mean of x0, x1 ie., x2=(x0+x1)/2
Step 4.1.2: Substitute x2 in f(x) and find f(x2)
Step 4.1.3: if f(x2) has same sign as f(x1) then replace x1
with x2 go to step 4.1.6
Step 4.1.4: if f(x2) has same sign as f(x0) then replace x0 with x2 go to step 4.1.6
Step 4.1.5: if f(x2) is zero go to step 5
Step 4.1.6: end if
Step 5: Print x2 as root of the given function f(x)
Step 6: Stop

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

Potrebbero piacerti anche