Friday, October 28, 2022

Find Mistake

#include <iostream>
#include <math.h>
using namespace std;
#define N 4
int main()
{   int a[N][N], b;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
               a[i][j] = rand() % 10;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            b = a[i][j];
            a[i][j] = a[j][i];
            a[j][i] = b;
        }

    cout << "\n\n\n\n";
    
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
 }

2-d Array

#include <iostream>
#include <math.h>
using namespace std;
#define N 9
int main()
{   int a[N][N], b;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
               a[i][j] = rand() % 10;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
 }

Change by pairs

 #include <iostream>
#include <math.h>
using namespace std;
#define N 9
int main()
{   int a[N],b;
    for (int i = 0; i < N; i++) a[i] = rand() % 10;
    for (int i=0; i < N; i++) cout << a[i]<<" ";
    cout << "\n";
    //
    // Input here your code 
    //  
    // Inputted code:
    for (int i = 0; i < N-1; i = i + 2) 
    {
        b = a[i]; 
        a[i] = a[i + 1]; 
        a[i + 1] = b;
    }
    
    //
    for (int i=0; i < N; i++) cout << a[i] << " ";
    cout << "\n";
 }

Rotate (shift) data on array "a" to right on 1 position

// Add to the program on C++  your fragment of code 
// which has to rotate(shift) data on array "a"   to right on 1 position.
// For example,
// array 5, 2, 8, 4
// should transform to array   4, 5, 2, 8
// Don't change the initially given code. 
//
#include <iostream>
#include <math.h>
using namespace std;
#define N 10
int main()
{   int a[N],b;
    for (int i = 0; i < N; i++) a[i] = rand() % 10;
    for (int i=0; i < N; i++) cout << a[i]<<" ";
    cout << "\n";
    //
    // Input here your code 
    //  
    // Inputted code:
    b = a[N - 1];
    for (int i = N-1; i > 0; i--) a[i] = a[i - 1];
    a[0] = b;
    //
    for (int i=0; i < N; i++) cout << a[i] << " ";
    cout << "\n";
 }


Friday, October 21, 2022

Newton's Method

 //  Newton's Method

#include <iostream>

#include <math.h>

double f(double t){return t*t*t*t+t*t*t+t*t+t-9;}

double ff(double t) { return 4 * t * t * t + 3 * t * t + 2 * t + 1; }

int main()

{

    int N = 1000;

    double x=-100.;

    for (int i = 1; i < N; i++) { x = x - f(x) / ff(x); }

    std::cout << "Solution =" << x << "\n";

    return 0;

}

3-Dimensional Stepwise Descent Method

 //  3-Dimensional Stepwise Descent Method:
#include <iostream>
#include <math.h>
double f(double t, double x, double y)
{
    return (t - 1.111111)* (t - 1.111111) * (x - 2.2222222) * (x - 2.2222222)* (y - 3.3333333) * (y - 3.33333333)+9;
}
int main()
{
    double r=100.,p=100,q=100,step=1., minstep=1./1000.;
    while (step > minstep)  
    {
        while (f(r + step,p,q) < f(r,p,q)) r = r + step;
        while (f(r - step,p,q) < f(r,p,q)) r = r - step;
        while (f(r, p + step, q) < f(r, p, q)) p = p + step;
        while (f(r, p - step, q) < f(r, p, q)) p = p - step;
        while (f(r, p, q+step) < f(r, p, q)) q = q + step;
        while (f(r, p, q-step) < f(r, p, q)) q = q - step;
        step = step / 2;
     }
    std::cout << "Minimum is in x =" << r << " " << p << " " << q << " ";
    return 0;
}

One-dimensional Stepwise Descent Method

 //  One-dimensional Stepwise Descent Method:
#include <iostream>
#include <math.h>
double f(double t)
{
    return (t - 1.111111)* (t - 1.111111);
}
int main()
{
    double x=100.,step=1., minstep=1./1000.;
    while (step > minstep)  
    {
        while (f(x + step) < f(x)) x = x + step;
        while (f(x - step) < f(x)) x = x - step;
        step = step / 2;
     }
    std::cout << "Minimum is in x =" << x << "\n";
    return 0;
}

Interval Bisection Method

#include <iostream>
#include <math.h>
double f(double t)
{
    return t - 2;
}
int main()
{
    int N = 10000;
    double a, b, fa,fb,fc,c;
    a = 0.;
    b = 3.14;
    for (int i = 1; i < N; i++) {
        fa = f(a);
        fb = f(b);
        c = (a + b) / 2;
        fc = f(c);
        if (fa * fb > 0) {    std::cout << "wrong!"; break;   }
        if (fc * fb > 0) b = c; else a = c;
    }
    std::cout << "Solution =" << c << "\n";
    return 0;
}


Digital Integration

 // DigitalIntegration.cpp : This file contains the 'main' function. Program execution begins and ends there.

// Digital integration of the function sin(x) from a to b

#include <iostream>

#include <math.h>

int main()

{

    int N = 10;

    double a, b, dx,s;

    a = 0;

    b = 3.14 / 4;

    dx = (b - a) / N;

    double sol = -cos(b) + cos(a);

    std::cout << "Solution analitical="<< sol<< "\n";

    s = sin(a)/2;

    for (int i = 1; i < N; i++) s = s + sin(a + i * dx);

    s = s + sin(b) / 2;

    s = s * dx;

    std::cout << "Solution numerical=" << s << "\n";

}


Friday, October 14, 2022

Drawing Sin

 <button onclick="draw()">Draw</button>
<canvas height="400" id="canvas" width="500">
</canvas>
<script>
function draw() {
  
    var x,y;
    const canvas = document.querySelector('#canvas');
    if (!canvas.getContext) {
        return;
    }
    const ctx = canvas.getContext('2d');
    // set line stroke and line width
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 5;
    // draw a red line
     ctx.beginPath();
     ctx.moveTo(0, 200);
  
  
for(let i=1;i<3000;i++)
{   x=i/50;
    y=Math.sin(x);
    ctx.lineTo(x*10, 200-y*200);

  
  ctx.stroke();
}
</script>

JavaScript - Drawing a Line

 <button onclick="draw()">Draw line</button>
    <canvas height="400" id="canvas" width="500">
    </canvas>
<script>
function draw() {
    const canvas = document.querySelector('#canvas');
    if (!canvas.getContext) {
        return;
    }
    const ctx = canvas.getContext('2d');
    // set line stroke and line width
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 5;
    // draw a red line
    ctx.beginPath();
    ctx.moveTo(100, 100);
    ctx.lineTo(300, 100);
    ctx.stroke();
}
</script>

Slide Show

 <img class="mySlides" src="https://ih1.redbubble.net/image.505029134.7145/st,small,507x507-pad,600x600,f8f8f8.u2.jpg" style="width: 200px;" />
<img class="mySlides" src="https://ih1.redbubble.net/image.816377316.4417/st,small,507x507-pad,600x600,f8f8f8.u1.jpg" style="width: 200px;" />
<img class="mySlides" src="https://ih1.redbubble.net/image.877955172.6060/poster,504x498,f8f8f8-pad,600x600,f8f8f8.jpg" style="width: 200px;" />
<img class="mySlides" src="https://ih1.redbubble.net/image.810616502.5757/st,small,507x507-pad,600x600,f8f8f8.u2.jpg" style="width: 200px;" />


<script>
var myIndex = 0;
carousel();
function carousel() {
  var i;
  var x = document.getElementsByClassName("mySlides");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  myIndex++;
  if (myIndex > x.length) {myIndex = 1}    
  x[myIndex-1].style.display = "block";  
  setTimeout(carousel, 1000); // Change image every 1 seconds
}
</script>

Check Multiplications

<button onclick="Calc()">Start</button><br />
<input id="O1" size="1" value="" />x<input id="O2" size="1" value="" />=<input id="I" size="1" /><br />
<button onclick="Check()">Check</button><br />
<input id="Output3" size="3" value="" />
<script type="text/javascript">
function Calc()
  {var a,b,c;
   a=Math.floor(Math.random()*9)+1;
   b=Math.floor(Math.random()*9)+1;
   document.getElementById("O1").value=a; 
   document.getElementById("O2").value=b; 
}
  
function Check()
  {var x,y,z;
   x=Number(document.getElementById("O1").value);
   y=Number(document.getElementById("O2").value);
   z=Number(document.getElementById("I").value);
   if(x*y==z)document.getElementById("Output3").value="Correct";
   else document.getElementById("Output3").value="Wrong";
}
</script> 
<br />

atan(x)

 #include <iostream>

#include <math.h>

int main()

{

    double x,y,s,a,b;

    int k; k = - 1;

    x = 0.5;

    y = atan(x);

    std::cout << "x =" << x << ", y=" << y << " ";

    b = x * x;

    s = x; a = x * x * x;

    for (int i = 3; i < 10000000; i = i + 2)

    {

        s = s + k*a/i;

        a = a * b;

        k = k * (-1);

    }

    std::cout << "s = " << s << "\n";

  }

p=(x+y+z)/3, 1/q=(1/x+1/y+1/z)/3, r=(xyz)^(1/3)

 p=(x+y+z)/3

1/q=(1/x+1/y+1/z)/3

r=(xyz)1/3                                  https://start--js.blogspot.com/p/xyz.html


X=<input id="InputBox1" size="1" value="1" /><br />

Y=<input id="InputBox2" size="1" value="2" /><br /> 

Z=<input id="InputBox3" size="1" value="3" /><br /><br />

<br />

<button onclick="CalculationFunction()">Calculate</button><br /><br />

<input id="OutputBox1" size="18" /><input id="ExponentialOutputBox1" size="6" /><input id="RoundingOutputBox1" size="6" /><br />

<input id="OutputBox2" size="18" /><input id="ExponentialOutputBox2" size="6" /><input id="RoundingOutputBox2" size="6" /><br />

<input id="OutputBox3" size="18" /><input id="ExponentialOutputBox3" size="6" /><input id="RoundingOutputBox3" size="6" /><br />

<script>

function CalculationFunction()

  {var x,y,z,p,q, r,xx,yy,zz;

   x=Number(document.getElementById("InputBox1").value);

   y=Number(document.getElementById("InputBox2").value);

   z=Number(document.getElementById("InputBox3").value);

   p=(x+y+z)/3;

   q=1/((1/x+1/y+1/z)/3);

   r=Math.pow(x*y*z,1/3);

   document.getElementById("OutputBox1").value=p; 

   document.getElementById("OutputBox2").value=q; 

   document.getElementById("OutputBox3").value=r; 

   document.getElementById("ExponentialOutputBox1").value=p.toExponential(2); 

   document.getElementById("ExponentialOutputBox2").value=q.toExponential(2); 

   document.getElementById("ExponentialOutputBox3").value=r.toExponential(2); 

   xx=Number(document.getElementById("ExponentialOutputBox1").value);

   yy=Number(document.getElementById("ExponentialOutputBox2").value);

   zz=Number(document.getElementById("ExponentialOutputBox3").value);

   document.getElementById("RoundingOutputBox1").value=xx;

   document.getElementById("RoundingOutputBox2").value=yy;

   document.getElementById("RoundingOutputBox3").value=zz;

   }

</script> 

<br />Template 01 


Friday, October 7, 2022

a*a+b*b=c*c

 #include <time.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int m=0,N = 1000;

for ( int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
for (int k = 1; k <= N; k++)
{
if(k*k+i*i==j*j)
{
m++;
cout << k << "\t" << i << "\t" << j << "\n";
}
}
cout << "m=" << m << "\n";
return 0;
}


Pi

 // Calculation of the pi number using the area of the circle 
// and the area of the square circumscribed around a circle
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long int k; k = 0;
double x, y, piapr;
// Always set a seed value.
srand((unsigned int)time(NULL));
for (long long int i = 1; i < 10000000; i++)
{
x = (double)rand() / RAND_MAX;
y = (double)rand() / RAND_MAX;
if (x * x + y * y < 1) k++;
piapr = 4 * (double)k / i;
if ((i % 10000) == 9999) cout << piapr << " ";
}
return 0;
}

cos(x)

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x,s,a,b,c;
int k; k = -1;
cin >> x;
cout << x << " " << cos(x) <<" ";
s = 1; a = x*x; b = 1; c = x*x/2;
for (int i = 2; i < 1000; i+=2)
{
s = s + k* c;
c = c * a;
c = c / ((i + 1)*(i+2));
k = k * -1;
}
cout << s << "\n";
}

sin(x)

 #include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x,s,a,b,c;
int k; k = -1;
cin >> x;
cout << x << " " << sin(x) <<" ";
s = x; a = x*x; b = 1; c = x*x*x/6;
for (int i = 3; i < 1000; i+=2)
{
s = s + k* c;
c = c * a;
c = c / ((i + 1)*(i+2));
k = k * -1;
}
cout << s << "\n";
}

Exp #2

 #include <iostream>

#include <math.h>

using namespace std;

int main()

{

double x,s,a,b,c;

cin >> x;

cout << x << " " << exp(x) <<" ";


s = 1; a = x; b = 1; c = x;

for (int i = 1; i < 1000; i++)

{

s = s + c;

c = c * x;

c = c / (i + 1);

}

cout << s << "\n";

}

Exp

 #include <iostream>

#include <math.h>

using namespace std;

int main()

{

double x,s,a,b;

cin >> x;

cout << x << " " << exp(x) <<" ";

s = 1; a = x; b = 1;

for (int i = 1; i < 1000; i++)

{

s = s + a / b;

a = a * x;

b = b * (i + 1);

}

cout << s << "\n";

}


Homework

 #include <iostream>

using namespace std;

int main()

{

    int k1, k2;

    int a[5][2] = { 1,2,

                   2,1,

                   1,3,

                   3,4,

                   4,2 };

    for (int i = 0; i < 5; i++)

        cout << a[i][0] << " " << a[i][1] << "\n";

    cout << "input 2 cities\n";

    cin >> k1;

    cin >> k2;

    cout << k1 << " " << k2 << "?\n";

    int p = 0;

    for (int i = 0; i < 5; i++)

    {

        if ((a[i][0] == k1) && (a[i][1] == k2)) p = 1;

    }

    if (p == 1) cout << "yes!"; else cout << "no :(";

    for (int i = 0; i < 5; i++)

    for (int j= 0;j<5;j++)

    {

        if ((a[i][0] == k1) && (a[j][1] == k2) && (a[i][1] == a[j][0]))

            cout <<"\n" << k1 << " -> " << a[i][1] << " -> " << k2 << "\n";

    }

Saturday, October 1, 2022

HomeWork

Develop programs for the following mathematical expressions (equalities).  
A program must show by numerical calculation that the formula on the left side of the equality is equal (or not equal) to the limit of the sum indicated on the right side of the equality. 
Organize the calculations of the sums by separate functions.  

NaCl.xyz Generator with Interactive 3D Visualization

NaCl.xyz Generator with Interactive 3D Visualization NaCl.xyz Generator with Interactive 3D Visualization ...