Commit aa0f6e4b authored by Hans Buchmann's avatar Hans Buchmann

exams/2020-12-03/

 solutions
6-cia/src/geo2d-vz.cc   Lime
      src/svg-demo.svg  for drawings
parent 6e135d00
......@@ -25,6 +25,19 @@ class Point
double yc; //y-coordinate
};
//----------------------------------------------- Line
// declaration
class Line
{
public:
Line(const Point& p,const Point& q);
// |--------------|-------------- by C++ reference
~Line();
private:
Point pp; //by value
Point qq; //by value
};
//---------------------------------------------- Point
// implementation
......@@ -67,65 +80,31 @@ double Point::distance(const Point& other) const //is a member of Point
yc- other.yc);
}
//---------------------------------------------- Line
/*
constructor of Point 3,4 p
constructor of Point 6,8 q
destructor of Point 6,8 other
d=5
destructor of Point 6,8 q
destructor of Point 3,4 p
*/
/* with own copy constructor
constructor of Point 3,4 p
constructor of Point 6,8 q
copy constructor of Point 6,8 other
destructor of Point 6,8 other
d=5
destructor of Point 6,8 q
destructor of Point 3,4 p
*/
/* with other as pointer
constructor of Point 3,4 p
constructor of Point 6,8 q
d=5
destructor of Point 6,8 q
destructor of Point 3,4 p
with other as c++ reference
constructor of Point 3,4
constructor of Point 6,8
d=5
destructor of Point 6,8
destructor of Point 3,4
*/
/*
with other as const c++ reference
distance cannot change the variable of instance
constructor of Point 3,4
constructor of Point 6,8
d=5
destructor of Point 6,8
destructor of Point 3,4
*/
//----------------------------------------------- Line
// implementation
Line::Line(const Point& p,const Point& q)
:pp(p),qq(q) //copy constructor p->pp and copy constructo q->qq
{
std::cout<<"constructor Line\n";
}
Line::~Line()
{
std::cout<<"destructor Line\n";
}
int main(int argc,char**args)
{
Point p(3,4);
Point q(6,8);
Point p(3,4); //constructor of Point 3,4
Point q(6,8); //constructor of Point 6,8
/*
distance (3,4) -> (6,8) = sqrt(3*3+4*4)=sqrt(25)=5
*/
auto d1=p.distance(q); //address of q c++ reference no copy
std::cout<<"d="<<d1<<"\n";
copy constructor of Point 3,4 see pp by value
copy constructor of Point 6,8 see qq by value
*/
Line l(p,q); //constructor Line
return 0;
}
} //destructor Line
// destructor of Point 3,4
// destructor of Point 6,8
//destructor of Point 6,8
//destructor of Point 3,4
......@@ -5,7 +5,10 @@
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- see http://www.w3.org/TR/SVG11/ -->
<line x1="0" y1="0" x2="150" y2="100"
stroke-width="20" stroke="black"></line>
stroke-width="20" stroke="yellow"></line>
<line x1="150" y1="100" x2="300" y2="400"
stroke-width="5" stroke="red"></line>
<circle cx="100" cy="100" r="50" stroke="green"/>
<rect x="100" y="200" width="200" height="50"/>
</svg>
//---------------------
//cos-sin-table.cc
//(c) H.Buchmann FHNW 2020
// - creates the table used for cos_sin8
// - table in file cos-sin-table.txt
//---------------------
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
static unsigned PI =1<<31;
static auto SQRT2=std::sqrt(2);
static char Table[]="cos-sin-table.txt";
void table()
{
std::ofstream tbl(Table);
tbl<<"//table used in cos-sin8\n";
unsigned alfa=PI/4;
double x=0.5*SQRT2;
double y=0.5*SQRT2;
unsigned i=0;
while(alfa>0)
{
x=1+x;
auto lm=std::hypot(x,y);
x/=lm;
y/=lm;
alfa=alfa/2;
tbl<<"/*"<<std::setw(3)<<(i++)<<" */ "
<<std::scientific<<std::setprecision(20)<<1/lm<<",\n";
}
}
int main(int argc,char** args)
{
table();
std::cout<<"Table you will find in file '"<<Table<<"'\n";
return 0;
}
//---------------------
//cos-sin.cc
//(c) H.Buchmann FHNW 2020
//---------------------
#include <cmath>
#include <iostream>
#include <iomanip>
#include <array>
#include <numbers>
using SCALE=std::array<double,30>;
static auto SQRT2=std::sqrt(2);
static unsigned PI=(1<<31);
static const SCALE SC=
{
use program cos-sin-table.cc to generate the table
};
void cos_sin8(unsigned alfa,double* c,double* s) //0<=alfa<pi/4
{
double x0=1;
double y0=0;
unsigned alfa0=0;
double x1=0.5*SQRT2;
double y1=0.5*SQRT2;
unsigned alfa1=PI/4;
unsigned sci=0;
while((alfa1-alfa0)>1)
{
double xm=x0+x1;
double ym=y0+y1;
double sc=SC[sci++];
xm*=sc;
ym*=sc;
auto alfam=(alfa0+alfa1)/2;
if (alfa>alfam)
{
x0=xm;
y0=ym;
alfa0=alfam;
}
else
{
x1=xm;
y1=ym;
alfa1=alfam;
}
}
*c=x0;
*s=y0;
}
//cos_sin{
//your code
//cos_sin}
//****** DO NOT CHANGE BELOW THIS LINE
int main(int argc,char** args)
{
unsigned alfa=0;
for(unsigned k=0;k<2;++k)
{
for(unsigned i=0;i<PI;i+=PI/1024)
{
double c=0;
double s=0;
cos_sin(alfa,&c,&s);
std::cout<<alfa<<"\t"<<c<<"\t"<<s<<"\n";
alfa+=PI/1024;
}
}
return 0;
}
//---------------------
//horner.cc
//(c) H.Buchmann FHNW 2020
//---------------------
#include <iostream>
#include <fstream>
//eval{
//your code
//eval}
//****** DO NOT CHANGE BELOW THIS LINE
int main(int argc,char** args)
{
double coeff[]={4,0,-5,0,1};
std::ofstream out("horner-deriv.csv");
for(double x=-2.25;x<=2.25;x+=0.125)
{
double y;
double dy=0;
eval(coeff,4,x,&y,&dy);
out<<x<<"\t"<<y<<"\t"<<dy<<"\n";
}
return 0;
}
//-----------------------
//pointer.cc
//(c) H.Buchmann FHNW 2020
//-----------------------
#include <iostream>
//definit{
//definit}
int main(int argc,char** args)
{
//assign{
//assign}
return 0
}
//--------------------------
//sound.cc
//(c) H.Buchmann FHNW 2020
//--------------------------
#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>
#include <numbers>
static const char WAV[]="snd.wav";
template<typename T>
void put(std::ostream& out,T v)
{
out.write((const char*)&v,sizeof(v));
}
void wav(unsigned fs,double s[],unsigned n)
{
static const double SC=(1<<12);
unsigned l=12+24+8+2*n;
std::ofstream out(WAV);
out<<"RIFF";
put(out,l-8);
out<<"WAVE";
out<<"fmt ";
put(out,16u);
put<unsigned short>(out,1); //pcm
put<unsigned short>(out,1); //channels
put(out,fs);
put(out,fs*2); //16 bit
put<unsigned short>(out,2); //block align
put<unsigned short>(out,16); //bits per sample
out<<"data";
put(out,l-44);
for(unsigned i=0;i<n;++i)
{
put<short>(out, SC*s[i]);
}
}
static double PI=4*std::atan(1);
double sin_(double alfa)
{
static unsigned callN=0;
if (callN)
{
std::cerr<<"sin_ called more than once\n";
while(true){}
}
++callN;
return std::sin(alfa);
}
double cos_(double alfa)
{
static unsigned callN=0;
if (callN)
{
std::cerr<<"cos_ called more than once\n";
while(true){}
}
++callN;
return std::cos(alfa);
}
//use sin_ resp. cos_ for sin resp cos
//use sin_ resp. cos_ only once
//sine{
//sine}
//****** DO NOT CHANGE BELOW THIS LINE
static const unsigned FS =44'100;
static const double DUR=1; //seconds
int main(int argc,char** args)
{
unsigned len=FS*DUR;
double s[len];
sine(FS,440,s,len);
wav(FS,s,len);
return 0;
}
//---------------------
//cos-sin-table.cc
//(c) H.Buchmann FHNW 2020
// - creates the table used for cos_sin8
// - table in file cos-sin-table.txt
//---------------------
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
static unsigned PI =1<<31;
static auto SQRT2=std::sqrt(2);
static char Table[]="cos-sin-table.txt";
void table()
{
std::ofstream tbl(Table);
tbl<<"//table used in cos-sin8\n";
unsigned alfa=PI/4;
double x=0.5*SQRT2;
double y=0.5*SQRT2;
unsigned i=0;
while(alfa>0)
{
x=1+x;
auto lm=std::hypot(x,y);
x/=lm;
y/=lm;
alfa=alfa/2;
tbl<<"/*"<<std::setw(3)<<(i++)<<" */ "
<<std::scientific<<std::setprecision(20)<<1/lm<<",\n";
}
}
int main(int argc,char** args)
{
table();
std::cout<<"Table you will find in file '"<<Table<<"'\n";
return 0;
}
//---------------------
//cos-sin.cc
//(c) H.Buchmann FHNW 2020
//---------------------
#include <cmath>
#include <iostream>
#include <iomanip>
#include <array>
#include <numbers>
using SCALE=std::array<double,30>;
static auto SQRT2=std::sqrt(2);
static unsigned PI=(1<<31);
static const SCALE SC=
{
/* 0 */ 5.41196100146197012215e-01,
/* 1 */ 5.09795579104159179984e-01,
/* 2 */ 5.02419286188155678197e-01,
/* 3 */ 5.00602998235196383625e-01,
/* 4 */ 5.00150636020651018221e-01,
/* 5 */ 5.00037651915547720627e-01,
/* 6 */ 5.00009412535887820539e-01,
/* 7 */ 5.00002353106286046014e-01,
/* 8 */ 5.00000588274841284431e-01,
/* 9 */ 5.00000147068602074363e-01,
/* 10 */ 5.00000036767143773986e-01,
/* 11 */ 5.00000009191785554918e-01,
/* 12 */ 5.00000002297946388730e-01,
/* 13 */ 5.00000000574486569427e-01,
/* 14 */ 5.00000000143621670112e-01,
/* 15 */ 5.00000000035905389772e-01,
/* 16 */ 5.00000000008976375199e-01,
/* 17 */ 5.00000000002244093800e-01,
/* 18 */ 5.00000000000561106717e-01,
/* 19 */ 5.00000000000140332190e-01,
/* 20 */ 5.00000000000035083048e-01,
/* 21 */ 5.00000000000008770762e-01,
/* 22 */ 5.00000000000002220446e-01,
/* 23 */ 5.00000000000000555112e-01,
/* 24 */ 5.00000000000000222045e-01,
/* 25 */ 5.00000000000000111022e-01,
/* 26 */ 5.00000000000000000000e-01,
/* 27 */ 5.00000000000000000000e-01,
/* 28 */ 5.00000000000000000000e-01,
/* 29 */ 5.00000000000000000000e-01,
};
void cos_sin8(unsigned alfa,double* c,double* s) //0<=alfa<pi/4
{
double x0=1;
double y0=0;
unsigned alfa0=0;
double x1=0.5*SQRT2;
double y1=0.5*SQRT2;
unsigned alfa1=PI/4;
unsigned sci=0;
while((alfa1-alfa0)>1)
{
double xm=x0+x1;
double ym=y0+y1;
double sc=SC[sci++];
xm*=sc;
ym*=sc;
auto alfam=(alfa0+alfa1)/2;
if (alfa>alfam)
{
x0=xm;
y0=ym;
alfa0=alfam;
}
else
{
x1=xm;
y1=ym;
alfa1=alfam;
}
}
*c=x0;
*s=y0;
}
//cos_sin{
void cos_sin(unsigned alfa,double* c,double* s)
{
if (alfa<PI/4)
{
cos_sin8(alfa,c,s);
return;
}
//pi/4<=alfa<2*pi
if (alfa<PI/2)
{
auto a=PI/2-alfa; //0<=a<PI/4
cos_sin8(a,s,c);
return;
}
//pi/2<=alfa<2*pi
if (alfa<PI)
{
auto a=alfa-PI/2; //0<=a<PI/2
cos_sin(a,s,c);
*c=-*c;
return;
}
//pi<=alfa<2*pi
auto a=alfa-PI;
cos_sin(a,c,s);
*c=-*c;
*s=-*s;
}
//cos_sin}
//****** DO NOT CHANGE BELOW THIS LINE
int main(int argc,char** args)
{
unsigned alfa=0;
for(unsigned k=0;k<2;++k)
{
for(unsigned i=0;i<PI;i+=PI/1024)
{
double c=0;
double s=0;
cos_sin(alfa,&c,&s);
std::cout<<alfa<<"\t"<<c<<"\t"<<s<<"\n";
alfa+=PI/1024;
}
}
return 0;
}
//---------------------
//horner.cc
//(c) H.Buchmann FHNW 2020
//---------------------
#include <iostream>
#include <fstream>
//eval{
//your code
//eval}
//****** DO NOT CHANGE BELOW THIS LINE
int main(int argc,char** args)
{
double coeff[]={0,-10,0,4};
std::ofstream out("horner-integral.csv");
for(double x=-2.25;x<=2.25;x+=0.125)
{
double dy=0;
double y=0;
eval(coeff,3,x,&dy,&y);
out<<x<<"\t"<<dy<<"\t"<<y<<"\n";
}
return 0;
}
//-----------------------
//pointer.cc
//(c) H.Buchmann FHNW 2020
//-----------------------
#include <iostream>
//definit{
//definit}
int main(int argc,char** args)
{
//assign{
//assign}
return 0
}
//-------------------
//simpson.cc
//(c) H.Buchmann FHNW 2020
//-------------------
#include <iostream>
#include <cmath>
static const double SQRT2=std::sqrt(2);
//volume{
//volume}
double circle(double r,double y[],unsigned n)
{
if (n<1) return 0;
double h=(2*r)/(n-1);
double x=-r;
double r2=r*r;
for(unsigned i=0;i<(n-1);++i)
{
y[i]=std::sqrt(r2-x*x);
x+=h;
}
y[n-1]=0;
return h;
}
//****** DO NOT CHANGE BELOW THIS LINE
int main(int argc,char** args)
{
static const unsigned Y=7;
double y[Y];
auto h=circle(1,y,Y);
std::cout<<volume(h,y,Y)<<"\n"; //expected 2*(4/3)*sqrt(2)=3.771236166
return 0;
}
//--------------------
//trailing.cc
//(c) H.Buchmann FHNW 2020
//--------------------
#include <iostream>
/*
before: