0% found this document useful (0 votes)
27 views2 pages

Cheat Sheet Engin 135 Exam 3

This document contains code snippets for several different algorithms: 1. A program that counts the number of lines in a text file input by the user. 2. Code implementing the numerical differentiation method to approximate derivatives. 3. The Newton-Raphson method for finding roots of equations implemented in a program. 4. Functions to locate the number of occurrences of a value in an array, and to sort an array in descending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Cheat Sheet Engin 135 Exam 3

This document contains code snippets for several different algorithms: 1. A program that counts the number of lines in a text file input by the user. 2. Code implementing the numerical differentiation method to approximate derivatives. 3. The Newton-Raphson method for finding roots of equations implemented in a program. 4. Functions to locate the number of occurrences of a value in an array, and to sort an array in descending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostrea

m>
#include<cmath>
#include<iomanip
>
#include<string>
#include<fstream
>
using namespace
std;
void main()
check the
declaration
string fname,line;
int count=0;
cout<<filename:
;
cin>>fname;
ifstream
inf(fname_c.str());
while(!inf.eof())
{ inf>>line;
count++;
cout<<endl<<co
unt<<endl;
}

inf.close();
user inputs the file
name
double f(double x)
{return(x*sin(x)*e
xp(2.5*x); }
void main()
{ double
der,x,a=?,b=?,dx;
int n;
cout<<enter the
segments no:;
cin>>n;
dx=(b-a)/n;
for(int i=0;i<n;i+
+)
{ if(i==0)
x=a;
der=(f(x+dx)f(x))/dx;
else if(i==n-1);
x=b;
der=(f(x)-f(xdx))/dx;
else

der=(f(x+dx)-f(xdx))/(2*dx);
x=x+dx;
cout<<y; }
numerical diff
method
double y (double
x)
{ return(pow(x,3)
); }
double yp(double
x)
{ return{3*pow(x
^2)); }
void main()
{ double
x,xn,dx1,dx2,tol;
ofstream
outf(data.txt);
char again;
do{ cout<<enter
initial guess: :
cin>>x;
cout<<enter
tolerance:
cin>>tol;

xn=x-y(x)/yp(x);
dx2=fabs(xn-x);
outf<<x<<y(x)<<y
p(x)<<xn;

if(dx2<=tol)

outf<<x<<y(x)<<y
p(x)<<xn;

else{do{ x=xn;
dx1=dx2;
xn=x-y(x)/yp(x);
dx2=fabs(xn-x);
outf<<x<<y(x)<<y
p(x)<<xn;}while(dx
1>dx2&&dx2>tol);

if(dx1<dx2)
cout<<method
diverge;
else
outf<<one
root=<<xn;
}
cout<<enter y t
repeat:
cin>>again;

}
while(again==y
);
outf.close(); }
newton method
int locate(int[],int
n,u)
n is the size of
array
x is array
{ int count=0;
for (int i=0;i<n;i+
+)
if(x[i]==u)
u is a number to
find
count++;
return(count); }
void sort(double
x[], int n)
{double
max,maxloc,temp;
for(int j=0;j<(n1);j++)
{ max=x[j];
maxloc=j; }

for(in
k=j+1;k<n;k++)
{ if(x[k]>max)
{max+x[k];
maxloc=k; }}
temp=x[j];
x[j]=max;
x[max_loc]=temp;
}}

void main()
{ int
a[size],num,I,b[siz
e];
cout<<enter
data val:;
for(i=0;i<size;i+
+)
{ cin>>a[i];

b[i]=a[i];
cout<<endl>>n
umber to search:;
cin>>num;
int
l=locate(a,size,nu
m);
sort(a,size);
if(l==0)

cout<<endl<<nu
m<<does not
appear in the data
set;
else
cout<<endl<<nu
m<<appear<<l
<<times in the
data set; }

function to accept
an array(x),
number of times a
number a ppear
and sort in desc
order

You might also like