## 3Dvectors, Fraction & Matrix

The first project manipulates three-dimensional vectors, I used classes, data validation, and pointers.

The second project is a little bit more complicated, it's about overloading operators, it can add, subtract, multiply and divide fractions. I used the "abs" function (stdlib.h) to calculate the smallest common denominator for the fractions, the rest is typical.

The third project handles two-dimensional and three-dimensional matrices. It's a little bit more complicated, I think it's a very good pointers practice. I also added a show help function that can be called externally using "-h".

I have organised the programs as recommended by mister wlodarczyk, which i would like to thank for this amazing course.

3Dvectors

MAIN.CPP

#include <iostream>
#include "Vector3D.h"
using namespace std;

int main()
{
cout <<"This program takes two 3D vectors from the user and returns the sum, the scalar product and the norms \n";
Vector3D V1;
Vector3D V2;
cout <<"Enter the first vector, it will be displayed alongside the address of it's first element\n";
cout<<V1.SetVector()<<endl;
V1.GetVector();
cout<<endl;
cout<< "Enter the second vector, it will be displayed alongside the address of it's first element\n";
cout<<V2.SetVector()<<endl;
V2.GetVector();
cout<<endl;
cout<< "The sum is ";
SumVectors(V1.GetVector(),V2.GetVector());
cout<<endl;
cout<<"The scalar product is "<< ScalarProd(V1.GetVector(),V2.GetVector())<<endl;
NormMax(V1.GetVector(),V2.GetVector());

return 0;
}

VECTOR3D.H

#ifndef VECTOR3D_H_INCLUDED
#define VECTOR3D_H_INCLUDED
class Vector3D //Declaration of class.
{
private:
float x,y,z;
float p[3];
float q[3];

public:
Vector3D();
float * SetVector();//A function that takes the 3 coordinates from the console, it returns the address &p[0] and the vector.
float * GetVector();//A function that saves the input given by the console.
~Vector3D();
};

void SumVectors(float *,float *);//A function that returns the sum of two vectors, it is not a class function.
float ScalarProd(float *,float *);//A function that returns the scalar product of the two vectors.
void NormMax(float *,float *);//A function that calculates the norm of the two vectors and displays the vector with the highest norm.

#endif // VECTOR3D_H_INCLUDED

VECTOR3D.CPP

#include <iostream>
#include "Vector3D.h"
#include <limits>
#include <math.h>

using namespace std;

Vector3D::Vector3D()
{
cout << "A constructor has been defined, it sets the two vectors to (0)\n";
x=0;y=0;z=0;
cout<<endl;
}

float * Vector3D::SetVector()
{
while(!(cin>>x) || !(cin>>y) || !(cin>>z))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter a valid input, you have to re-enter 3 real numbers \n";
}
this->p[0]=x;
this->p[1]=y;
this->p[2]=z;
cout<<"["<<p[0]<<","<<p[1]<<","<<p[2]<<"]"<<endl;
return p;
}

float* Vector3D::GetVector()
{
this->q[0]=x;
this->q[1]=y;
this->q[2]=z;
return q;

}

void SumVectors(float *p,float *q)
{
cout<<"["<<p[0]+q[0]<<","<<p[1]+q[1]<<","<<p[2]+q[2]<<"]";
}

float ScalarProd(float*p,float*q)
{
return p[0]*q[0]+p[1]*q[1]+p[2]*q[2];
}

void NormMax(float *p,float *q)
{
double a,b;
a=sqrt(p[0]*p[0]+p[1]*p[1]+p[2]*p[2]);
b=sqrt(q[0]*q[0]+q[1]*q[1]+q[2]*q[2]);
if(a>b)
cout<<"["<<p[0]<<","<<p[1]<<","<<p[2]<<"] has the biggest norm "<<a<< " The second vector's norm is "<< b<<endl;
else
cout<<"["<<q[0]<<","<<q[1]<<","<<q[2]<<"] has the biggest norm "<<b<< " The second vector's norm is "<< a<<endl;
}

Vector3D::~Vector3D()
{
cout <<"A destructor has been defined"<<endl;
}

Fraction

MAIN.CPP

#include <iostream>
#include "Fraction.h"
#include <stdlib.h>

using namespace std;

int main()
{
Fraction f1(-1,5);
Fraction f2(1,4);
Fraction f3(1,7);
Fraction f4(5,2);

f1.getFraction();
f2.getFraction();
cout<<endl;
(f1*f2).getFraction();
(f1+f2).getFraction();
(f1-f2).getFraction();
(f1/f2).getFraction();
cout<<endl;
(f1+f2-f3*f4).getFraction();

return 0;
}

FRACTION.H

#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED

using namespace std;

class Fraction
{
int num,den;

public:

Fraction(int,int);//A constructor that defines the fractions.
~Fraction();// A destructor.
void getFraction();
int SCM(int,int);// A function that returns the smallest common multiple of two numbers.
};

#endif // FRACTION_H_INCLUDED

FRACTION.CPP

#include "Fraction.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

Fraction::Fraction(int num,int den)
{
this->num=num;
try
{
if(den==0)
throw den;

else
this->den=den;
}
catch(int e)
{
cout<<"Cannot divide by zero, the denominator will be chosen randomly\n";
}
}

Fraction::~Fraction()
{

}
void Fraction::getFraction()
{
cout<<num<<"/"<<den<<" ";
}
int Fraction::SCM(int a,int b)
{
for(int j=1;j<=min(abs(a),abs(b));j++)
{
for(int i=1;i<=max(abs(a),abs(b));i++)
{
if(min(abs(a),abs(b))*i==max(abs(a),abs(b))*j)
return min(abs(a),abs(b))*i;
}
}

}

Fraction Fraction::operator*(Fraction f)
{
Fraction tmp(this->num*f.num,this->den*f.den);
return tmp;
}

Fraction Fraction::operator+(Fraction f)
{
Fraction tmp(this->num*SCM(this->den,f.den)/this->den+f.num*SCM(this->den,f.den)/f.den,SCM(this->den,f.den));
return tmp;
}

Fraction Fraction::operator-(Fraction f)
{
Fraction tmp(this->num*SCM(this->den,f.den)/this->den-f.num*SCM(this->den,f.den)/f.den,SCM(this->den,f.den));
return tmp;
}
Fraction Fraction::operator/(Fraction f)
{
Fraction tmp(this->num*f.den,this->den*f.num);
return tmp;
}

Matrix

MAIN.CPP

#include "Matrix.h"

using namespace std;
void showHelp();

int main(int argc,char*argv[])
{
unsigned short listChoice;
char repeat;
do{
system("cls");
for(short i=0;i<argc;i++)
{
if(strcmp(argv[i],"-h")==0 && i==1)
showHelp();
}
cout<<"This program can add, multiply and invert 2 by 2 and 3 by 3 matrices \n Enter the associated number to begin\n";
cout<<"1.Invert a 2 by 2 matrix.\n2.Add and multiply two 2 by 2 matrices.\n3.Invert a 3 by 3 matrix.\n4.Add and multiply two 3 by 3 matrices.\n";

Matrix2by2 M1,M2,M3;
Matrix3by3 N1,N2,N3;
while(!(cin>>listChoice))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
switch(listChoice)
{
case 1:
M1.setMatrix();
M2=M1.invertMatrix();
M2.getMatrix();
break;

case 2:
M1.setMatrix();
M2.setMatrix();
cout<<"The sum is\n";
(M1+M2).getMatrix();
cout<<"The product is\n";
(M1*M2).getMatrix();
break;

case 3:
N1.setMatrix();
N2=N1.invertMatrix();
N2.getMatrix();
break;

case 4:
N1.setMatrix();
N2.setMatrix();
cout<<"The sum is\n";
(N1+N2).getMatrix();
cout<<"The product is\n";
(N1*N2).getMatrix();
break;

default:
break;
}
cout<<"Enter c or C to continue.\n";
cin>>repeat;
}while(repeat=='c' || repeat=='C');

return 0;
}

void showHelp()
{
cout<<"To enter the matrices, you simply type the numbers on the same line and then press enter.\nYou can also enter the numbers in a matrix style\na b c\nd e f\ng h i\n";
}

MATRIX.H

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include <iostream>
#include <limits>
#include <cstdlib>
#include <string.h>

class Matrix2by2
{
protected:
float q[9];
float p[9];
float r[3][3];
public:
Matrix2by2();
virtual ~Matrix2by2();
Matrix2by2(float*);//Convert constructor.
float*setMatrix();
float*getMatrix();
float*invertMatrix();
Matrix2by2 operator+(Matrix2by2);
Matrix2by2 operator*(Matrix2by2);

} ;

class Matrix3by3:public Matrix2by2
{
public:
Matrix3by3();
~Matrix3by3();
Matrix3by3(float*);
float*setMatrix();
float*getMatrix();
Matrix3by3 operator+(Matrix3by3);
Matrix3by3 operator*(Matrix3by3);
float*invertMatrix();

};

#endif // MATRIX_H_INCLUDED

MATRIX.CPP

#include "Matrix.h"

using namespace std;

Matrix2by2::Matrix2by2()
{
for(short i=0;i<9;i++)
{
q[i]=0;
p[i]=0;
}
for(short i=0;i<3;i++)
{
for(short j=0;j<3;j++)
r[i][j]=0;
}
}
Matrix2by2::~Matrix2by2()
{

}
Matrix2by2::Matrix2by2(float*q)
{
for(short i=0;i<4;i++)
{
this->q[i]=q[i];
}
}

float*Matrix2by2::setMatrix()
{
cout<<"Enter the 2 by 2 matrix horizontally\n";
for(short i=0;i<4;i++)
cin>>q[i];
return q;
}
float*Matrix2by2::getMatrix()
{
for(short i=0;i<2;i++)
cout<<q[i]<<" ";
cout<<endl;
for(short i=2;i<4;i++)
cout<<q[i]<<" ";
cout<<endl;
return q;
}
float*Matrix2by2::invertMatrix()
{
float tmp=q[0]*q[3]-q[2]*q[1];
if(tmp!=0)
{
p[0]=q[3]/tmp;
p[1]=-q[1]/tmp;
p[2]=-q[2]/tmp;
p[3]=q[0]/tmp;
}
else cout<<"The matrix is not invertible\n";
Matrix2by2 M=p;
return p;

}
Matrix2by2 Matrix2by2::operator+(Matrix2by2 M)
{
for(short i=0;i<4;i++)
{
p[i]=this->q[i]+M.q[i];
}
M=p;
return M;
}
Matrix2by2 Matrix2by2::operator*(Matrix2by2 M)
{
for(short i=0;i<4;i+=2)
{
for(short j=0;j<2;j++)
{
r[i][j]=this->q[i]*M.q[j]+this->q[i+1]*M.q[j+2];
}
}
p[0]=r[0][0];p[1]=r[0][1];p[2]=r[2][0];p[3]=r[2][1];
M=p;
return M;
}

Matrix3by3::Matrix3by3()
{
for(short i=0;i<9;i++)
{
q[i]=0;
}
}
Matrix3by3::~Matrix3by3()
{

}
Matrix3by3::Matrix3by3(float*q)
{
for(short i=0;i<9;i++)
{
this->q[i]=q[i];
}
}
float* Matrix3by3::setMatrix()
{
cout<<"Enter the 3 by 3 matrix horizontally\n";
for(short i=0;i<9;i++)
cin>>q[i];
return q;
}
float*Matrix3by3::getMatrix()
{
for(short i=0;i<3;i++)
cout<<q[i]<<" ";
cout<<endl;
for(short i=3;i<6;i++)
cout<<q[i]<<" ";
cout<<endl;
for(short i=6;i<9;i++)
cout<<q[i]<<" ";
cout<<endl;
return q;

}
Matrix3by3 Matrix3by3::operator+(Matrix3by3 M)
{
for(short i=0;i<9;i++)
{
p[i]=this->q[i]+M.q[i];
}
M=p;
return M;
}

Matrix3by3 Matrix3by3::operator*(Matrix3by3 M)
{
for(short i=0,k=0;i<9,k<3;i+=3,k++)
{
for(short j=0;j<3;j++)
r[j][k]=this->q[i]*M.q[j]+this->q[i+1]*M.q[j+3]+this->q[i+2]*M.q[j+6];
}
for(short i=0,j=3,k=6;i<3,j<6,k<9;i++,j++,k++)
{
p[i]=r[i][0];
p[j]=r[j-3][1];
p[k]=r[k-6][2];
}
M=p;
return M;
}
float* Matrix3by3::invertMatrix()
{
float det=q[0]*(q[4]*q[8]-q[5]*q[7])-q[1]*(q[3]*q[8]-q[5]*q[6])+q[2]*(q[3]*q[7]-q[4]*q[6]);
if(det!=0)
{
p[0]=(q[4]*q[8]-q[5]*q[7])/det;
p[3]=(q[5]*q[6]-q[3]*q[8])/det;
p[6]=(q[3]*q[7]-q[4]*q[6])/det;
p[1]=(q[2]*q[7]-q[1]*q[8])/det;
p[4]=(q[0]*q[8]-q[2]*q[6])/det;
p[7]=(q[1]*q[6]-q[0]*q[7])/det;
p[2]=(q[1]*q[5]-q[2]*q[4])/det;
p[5]=(q[2]*q[3]-q[0]*q[5])/det;
p[8]=(q[0]*q[4]-q[1]*q[3])/det;
}
else {
cout<<"The matrix is not invertible"<<endl;

}
Matrix3by3 N=p;
return p;
}