PROJ7
#include "stdafx.h"
void zad1()
{
cout << "zad 1\n";
//generacja macierzy losowej <100,1000>
const int n = 7;
double a[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
a[i][j] = rand()*(1000.0-100.0)/RAND_MAX + 100.0;
}
}
//wypisanie macierzy
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << setw(9) << a[i][j];
}
cout << endl;
}
//wyznaczenie max(a_ij)
double xmax = a[0][0];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (xmax < a[i][j]) xmax = a[i][j];
}
}
cout << " maxymalny wyraz macierzy = " << xmax << endl;
// a gdzie ten max
int imax, jmax;
xmax = a[0][0];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if ( xmax < a[i][j]) {
xmax = a[i][j];
imax=i;
jmax=j;
}
}
}
cout << "max = a[" << imax + 1 << "," << jmax + 1 << "] = " << xmax << endl;
//inna wersja gdzie_max
imax=0, jmax=0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if ( a[imax][jmax] < a[i][j]) {
imax=i;
jmax=j;
}
}
}
cout << "max = a[" << imax + 1 << "," << jmax + 1 <<
"] = " << a[imax][jmax] << endl;
}
void zad2()
{
cout << "zad 2\n";
const int n = 10;
double a[n][n] = {0};
for (int i = 0; i < n; i++){
for (int j = i; j < n; j++){
a[i][j] = j - i + 1;
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << setw(3) << a[i][j];
}
cout << endl;
}
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
PROJ 8
#include "stdafx.h"
double rnd01()
{
return double(rand()) / RAND_MAX;
}
double rnd_ab( double a, double b)
{
return (b-a) * rnd01() + a;
}
double iloczyn_skalarny( double u[], double *v, int dim)
{
double s = 0;
for (int i = 0; i < dim; i++) {
s += u[i] * v[i];
}
return s;
}
double norma_wektora( double w[], int wym)
{
return sqrt(iloczyn_skalarny( w, w, wym ));
}
void liczba_razy_wektor( double liczba, double x[], int dim, double z[])
{
for (int i = 0; i < dim; i++ ) {
z[i] = liczba * x[i];
}
}
//---------------------------------------------
void zad1()
{
cout << "zad 1 - dynamiczny wektor\n ";
int n;
cin >> n;
double *x = new_d( n);
double *y = new_d( n);
double *z = new_d( n);
for (int i = 0; i < n; i++) {
x[i] = rnd_ab( -5, 5) ;
y[i] = rnd_ab( -5, 5);
}
liczba_razy_wektor( 2, x, n, z);
for (int i = 0; i < n; i++) {
cout << setw(10) << x[i] <<
setw(10) << y[i] <<
setw(10) << z[i] << endl;
}
cout << "Iloczy skalarny = " << iloczyn_skalarny( x, y, n) << endl;
cout << "norma wektora x= " << norma_wektora( x, n) << endl;
cout << "norma wektora y= " << norma_wektora( y, n) << endl;
del( x);
del( y);
}
void zad2()
{
cout << "zad 2\n";
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
PROJ9
#include "stdafx.h"
void zad1()
{
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
PROJ 10
#include "stdafx.h"
double sinN( double alfa )
{
double y,pipol;
pipol=asin(1.0);
if (alfa<-pipol)
y=-1;
else
if (alfa>pipol)
y=1;
else
y=sin(alfa);
return y;
}
void sinN (double alfa, double & wynik)
{
double pipol;
pipol=asin(1.0);
if (alfa<-pipol)
wynik=-1;
else
if (alfa>pipol)
wynik=1;
else
wynik=sin(alfa);
}
void DaneStartowe (double & a, double & b,double & n)
{
cout << " Proc DaneStartowe " << endl;
// cin >> a >> b >> n;
a = -3; b=3; n=6;
}
void Tabelartyzacja (double a, double dx, double n,
double *x, double y[])
{
cout << "proc Tabelaryzacja" << endl;
for ( int i=0; i<n+1; i++ )
{
x[i]=a;
y[i]=sinN(a);
a+=dx;
}
}
void Wydruk ( double x[] , double *y, double n )
{
cout << "proc Wydruk" << endl;
ofstream plik;
plik.open ("wyniki.txt");
for (int i=0; i <=n; i++)
plik << x[i] << " " << y[i] << endl;
plik.close();
}
void zad1()
{
cout << "zad 1\n";
double x, y;
cin>>x;
y=sinN(x);
cout << "fun1 y= " << y <<endl;
y=100;
sinN (x,y);
cout << "fun2 y= " << y <<endl;
}
void zad2()
{
cout << "zad 2\n";
double x,y,a,b,n,dx;
// cin >> a >> b >> n;
a = -3; b=3; n=20;
dx = (b-a)/n;
for ( int i=0; i<n+1; i++ )
{
// y=sinN(a+i*dx);
y=sinN(a);
a+=dx;
cout << x <<" "<< y << endl;
}
cout << a << endl;
}
void zad3()
{
cout << "zad 3\n";
double y,a,b,n,dx;
// cin >> a >> b >> n;
a = -3; b=3; n=20;
dx = (b-a)/n;
for ( double i=a; i<= b; i+=dx )
{
y=sinN(i);
cout << i <<" "<< y << endl;
}
}
void zad4()
{
cout << "zad 4 wydruk do pliku \n";
ofstream plik;
plik.open ("wyniki.txt");
double y,a,b,n,dx;
// cin >> a >> b >> n;
a = -3; b=3; n=30;
dx = (b-a)/n;
plik << setw(10) << "x" <<setw(10) << "y" << endl;
for ( double i=a; i<= b; i+=dx )
{
y=sinN(i);
plik << setw(10) << i << " " << setw(10) << y << endl;
}
plik.close ();
}
void zad5()
{
cout << "zad 5\n";
double a,b,n;
DaneStartowe (a,b,n);
double dx = (b-a)/n;
double *x = new double [n+1];
double *y = new double [n+1];
Tabelartyzacja (a,dx,n,x,y);
Wydruk (x,y,n);
delete [] x;
delete [] y;
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
PROJ11
#include "stdafx.h"
void zad1()
{
int n, m;
int i, j;
ifstream input_f("K:/bufor/#ts/macierz.txt");
input_f >> n >> m;
if (n < 1 || m < 1) {
cerr << "Zle dane\n";
return;
}
double **A = new_d( n, m);
ReadMatrix( input_f, A, n, m);
input_f.close();
input_f.open("K:/bufor/#ts/wektor.txt");
input_f >> i;
if (i != m) {
cerr << "Zla dlugosc wektora\n";
return;
}
double *B = new_d( m);
ReadVector( input_f, B, m);
input_f.close();
//----------------------------------
double *C = new_d( n);
MatrixByVector( A, B, n, m, C);
//-------------------------------------
ofstream output_f("i:/wynik.txt");
output_f << n << endl;
for (i = 0; i < n; i++) {
output_f << C[i] << " ";
}
output_f << endl;
//-------------------------------
for (j = 0; j < m; j++) {
cout << setw(15) << B[j] << endl;
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << setw(10) << A[i][j];
}
cout << setw(15) << C[i] << endl;
}
}
void zad2()
{
cout << "zad 2\n";
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
PROJ 12
#include "stdafx.h"
#include "nd.h"
#include "algebra.h"
#include "gauss.h"
void zad1() //rozwiązywanie układu równań
{
const int n = 5;
double ** a = newrandommatrix(n,n,0,10);
double *t = new_d(n);
for (int i=0; i < n; i++) {
t[i] = 2*i + 1;
}
double *b = new_d(n);
MatrixByVector(a,t,n,n,b);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << setw(10) << a[i][j];
}
cout << setw(20) << b[i] << endl;
}
double *x = new_d(n);
gauss(a,b,x,n);
for (int i = 0; i < n; i++) {
cout << setw(10) << x[i] << endl;
}
del(x);
del(b);
del(t);
del(a,n);
}
void zad2()
//rozwiązywanie układu równań
//wersja z plikami
{
const int n = 5;
double **a = new_d(n,n);
double *b = new_d(n);
//wczytanie a i b
ifstream plik("k:/bufor/#ts/macierz5x5.txt");
ReadMatrix(plik, a,n,n);
plik.close();
plik.open("k:/bufor/#ts/wektor5.txt");
ReadVector(plik, b,n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << setw(10) << a[i][j];
}
cout << setw(20) << b[i] << endl;
}
double *x = new_d(n);
gauss(a,b,x,n);
for (int i = 0; i < n; i++) {
cout << setw(10) << x[i] << endl;
}
//sprawdzenie
double *r = new_d(n);
MatrixByVector(a,x,n,n,r);
for (int i = 0; i < n; i++) {
cout << setw(10) << r[i] << endl;
r[i]-=b[i];
//cout << setw(10) << r[i] << endl;
}
//porządki
del(x);
del(b);
del(a,n);
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
PROJ 13
#include "stdafx.h"
#include "nd.h"
#include "algebra.h"
void OutMatrix( double **mat, int n, int m)
{
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cout << setw(10) << mat[i][j];
}
cout << endl;
}
}
void zad1()
{
ifstream p("K:/bufor/#ts/macierzA.txt");
int m,n;
p>>n>>m;
double **a = new_d(n,m);
ReadMatrix( p, a, n, m);
OutMatrix( a, n, m);
cout << endl;
int r = (rand() % 8) + 1;
double **b = new_d(m,r);
RandomMatrix( b, m, r, 0, 10);
OutMatrix(b, m, r);
cout << endl;
double **c = new_d(n,r);
MatrixByMatrix( a, b, n, m, r, c);
OutMatrix(c, n, r);
}
void zad2()
{
cout << "zad 2\n";
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------
P12
#include "stdafx.h"
#include "nd.h"
#include "algebra.h"
#include "gauss.h"
void zad1()//rozwiązywanie układu równań
{
const int n=5;
double **a = new_d( n,n );ra
RandomMatrix(a,n,n,0,10);
double **A = new_d(n,n);
double *B = new_d(n);
double *T= new_d(n);
for (int i=0; i<n; i++){
T[i]=2*i+1;
}
double *b= new_d(n);
MatrixByVector(a,T,n,n,b);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j]=a[i][j];
B[i]=b[i];
cout << setw(10) << a[i][j];
}
cout << setw(20) << b[i]<<endl;
}
double *x= new_d(n);
gauss(a,b,x,n);
for (int j = 0; j < n; j++) {
cout << setw(10) << x[j] <<endl;
}
double *r=new_d(n);
MatrixByVector(A,x,n,n,r);
for (int i=0; i<n; i++)
r[i]-=B[i];
for (int j = 0; j < n; j++) {
cout << setw(10) << r[j] <<endl;
}
del(r);
del(x);
del(T);
del(b);
del(a,n);
}
void zad2()
//rozwiązywanie układu równań
//A i B z plików
{
const int n=5;
double **a = new_d( n,n );
double *b = new_d(n);
ifstream plik ("K:/bufor/#ts/macierz5x5.txt");
ReadMatrix(plik, a, n,n);
//plik.close();
ifstream plik2("K:/bufor/#ts/wektor5.txt");
ReadVector(plik2, b, n);
double **A = new_d(n,n);
double *B = new_d(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j]=a[i][j];
B[i]=b[i];
cout << setw(10) << a[i][j];
}
cout << setw(20) << b[i]<<endl;
}
double *x= new_d(n);
gauss(a,b,x,n);
for (int j = 0; j < n; j++) {
cout << setw(10) << x[j] <<endl;
}
double *r=new_d(n);
MatrixByVector(A,x,n,n,r);
for (int i=0; i<n; i++)
r[i]-=B[i];
for (int j = 0; j < n; j++) {
cout << setw(10) << r[j] <<endl;
}
del(r);
del(x);
del(b);
del(a,n);
}
void zad3()
{
cout << "zad 3\n";
}
void zad4()
{
cout << "zad 4\n";
}
void zad5()
{
cout << "zad 5\n";
}
//----------------------------------------------------
void main()
{
int index;
cout << "Podaj numer zadania: ";
cin >> index;
switch (index) {
case 1:
zad1();
break;
case 2:
zad2();
break;
case 3:
zad3();
break;
case 4:
zad4();
break;
case 5:
zad5();
break;
default:
cout << "Nie ma takiego zadania\n";
break;
}
cout << "Nacisnij jakis klawisz...";
_getch();
}
//----------------------------------------------------