MELJUN CORTES's - C++ Source Code Algebra Application
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
217 -
download
0
Transcript of MELJUN CORTES's - C++ Source Code Algebra Application
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
1/34
1.) Calculating Area for Circle, Triangle andRectangleC++ Program to calculate the area for Circle,Triangle and Rectangle.
This sample code is also an example for the conceptof polymorphism.******************************************************************************
***********
Source Code******************************************************************************
***********
#include #include #include #include
class CShape{public:
virtual double CalculateArea() = 0;};
class CTriangle : public CShape{
double height;double base;double area;
public:CTriangle() : base(12.2), height(18.8){
}
CTriangle(double b, double h) : base(b), height(h){}void SetHeight(double h) { height = h; }void SetBase(double b) { base = b; }double GetHeight() const { return height; }double GetBase() const { return base; }
double CalculateArea(){
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
2/34
area = base / 2 * height;
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
3/34
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
4/34
return area;}
};
void main(){
CRectangle *shapeRect = new CRectangle();CCircle *shapeCircle = new CCircle();CTriangle *shapeTriangle = new CTriangle();CShape *shapeArea[] = { shapeRect, shapeCircle,
shapeTriangle };
for(int i = 0; i < sizeof(shapeArea) / sizeof(shapeArea[0]); i++)
shapeArea[i]->CalculateArea();
shapeRect->SetLength(16.8);
shapeRect->SetWidth(8.8);
shapeCircle->SetRadius(13.8);
shapeTriangle->SetBase(2.5);
shapeTriangle->SetHeight(3.8);
for(int i = 0; i < sizeof(shapeArea) / sizeof(shapeArea[0]); i++)
shapeArea[i]->CalculateArea();
delete shapeRect;delete shapeCircle;delete shapeTriangle;
}
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
5/34
Rectangle
Input: Length = 16.8 width = 8.8
Output: Area = ***147.8
Circle
Input: Radius = 13.8Output: Area = ***598.3
Triangle
Input: Base = 2.5 Height = 3.8
Output: Area = ****4.75
OutputRectangle
Input: Length = 26.6 width = 15.8
Output: Area = ***420.3
Circle
Input: Radius = 6.8Output: Area = ***145.3
Triangle
Input: Base = 12.2 Height = 18.8
Output: Area = ***114.7
2.) Square Root of a GivenNumberC++ Program to calculate the Square Root of a Given Number:
You can solve the problem in two ways with out using the library function
sqrt(...) defined in math.h header file.**Method 1 - Like binary search, have a minimum and maximum possible values. Do the square operation
and compare the result. Then adjust minimum or maximum until we find the correct sqrt of the given
number. NOTE: This is NOT a perfect square root and it has got accuracy of 4 decimal points. Method 1 is
explained on this page.
**Method 2 - The traditional way of doing with out calculator or any assumption. It will be perfect square
root up to N number of decimal points meaning the limitation of float and double. (I Will update this topic
very shortly with sample code).
******************************************************************************
***********
Source Code******************************************************************************
***********
#include
#include
#include
#include
double SQRTByGuess(double num)
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
6/34
{
// Assume that the number is less than 1,000,000. so that the maximum of SQRT would be 1000.
// Lets assume the result is 1000. If you want you can increase this limit
if(num < 0)
{
std::cout answer)
{
// min needs be moved
min = test;
}
else if(num < answer)
{
// max needs be moved
max = test;
}
if(num == answer)
break;
if(num > (answer - 0.0001) &&num < (answer + 0.0001))
break;
}
return test;
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int i = 1; i
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
7/34
SQRT(1) = 1.0000, 1.0000SQRT(4) = 2.0000, 2.0000
SQRT(7) = 2.6458, 2.6458
SQRT(10) = 3.1623, 3.1623
SQRT(13) = 3.6055, 3.6056
SQRT(16) = 4.0000, 4.0000
SQRT(19) = 4.3589, 4.3589
SQRT(22) = 4.6904, 4.6904SQRT(25) = 5.0000, 5.0000
SQRT(28) = 5.2915, 5.2915
SQRT(31) = 5.5678, 5.5678
SQRT(34) = 5.8309, 5.8310
SQRT(37) = 6.0828, 6.0828
SQRT(40) = 6.3246, 6.3246
SQRT(43) = 6.5574, 6.5574
SQRT(46) = 6.7823, 6.7823
SQRT(49) = 7.0000, 7.0000
SQRT(52) = 7.2111, 7.2111
SQRT(55) = 7.4162, 7.4162
SQRT(58) = 7.6158, 7.6158
SQRT(61) = 7.8102, 7.8102
SQRT(64) = 8.0000, 8.0000
SQRT(67) = 8.1854, 8.1854
SQRT(70) = 8.3666, 8.3666
SQRT(73) = 8.5440, 8.5440
SQRT(76) = 8.7178, 8.7178
SQRT(79) = 8.8882, 8.8882
SQRT(82) = 9.0554, 9.0554
SQRT(85) = 9.2195, 9.2195
SQRT(88) = 9.3808, 9.3808
SQRT(91) = 9.5394, 9.5394
SQRT(94) = 9.6954, 9.6954
SQRT(97) = 9.8489, 9.8489
SQRT(100) = 10.0000, 10.0000
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
8/34
The idea here is to find out the square root of given number andconvert it into an integer and do the square to check whether it is aperfect square or NOT. If the number falls between the square ofmin and square of max, then it is not a perfect square.******************************************************************************
***********
Source Code******************************************************************************
***********
#include
#include
#include
#include
int CheckPerfectSquare(int num)
{
int min = 0, max = 1000;int answer = 0;
int test = 0;
while(1)
{
test = (min + max) / 2;
answer = test * test;
if( num > answer)
{
// min needs be moved
min = test;
}
else if(num < answer)
{ // max needs be moved
max = test;
}
if(num == answer) {
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
9/34
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
10/34
******************************************************************************
***********
Source Code******************************************************************************
***********
#include #include
#include
class StdDeviation
{
private:
int max;
double value[100];
double mean;
public:
double CalculateMean()
{
double sum = 0;
for(int i = 0; i < max; i++)
sum += value[i];
return (sum / max);
}
double CalculateVariane()
{
mean = CalculateMean();
double temp = 0;
for(int i = 0; i < max; i++)
{
temp += (value[i] - mean) * (value[i] - mean) ;}
return temp / max;
}
double CalculateSampleVariane()
{
mean = CalculateMean();
double temp = 0;
for(int i = 0; i < max; i++)
{temp += (value[i] - mean) * (value[i] - mean) ;
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
11/34
}
return temp / (max - 1);
}
int SetValues(double *p, int count)
{
if(count > 100)
return -1;
max = count;
for(int i = 0; i < count; i++)
value[i] = p[i];
return 0;
}
double GetStandardDeviation()
{ return sqrt(CalculateVariane());
}
double GetSampleStandardDeviation()
{
return sqrt(CalculateSampleVariane());
}
};
int main()
{
double arrNumbers[] =
{
15.17, 16.94, 14.94, 14.99, 13.77, 13.75,
12.67, 12.14, 12.59, 12.48, 14.81, 14.29,
12.74, 12.52, 11.65, 12.24, 11.42, 12.25,
12.72, 11.64, 11.09, 11.22, 11.50, 11.36,
11.84, 12.18, 11.04, 10.90, 11.80, 11.84,
};
StdDeviation sd;
sd.SetValues(arrNumbers, sizeof(arrNumbers) / sizeof(arrNumbers[0]));
double mean = sd.CalculateMean();
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
12/34
double variance = sd.CalculateVariane();
double samplevariance = sd.CalculateSampleVariane();
double sampledevi = sd.GetSampleStandardDeviation();
double devi = sd.GetStandardDeviation();
char buf[1024];
sprintf(buf, "Total Numbers\t\t\t: %10d\n", sizeof(arrNumbers) / sizeof(arrNumbers[0]));
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
13/34
5.) Matrix MultiplicationC++ Program to compute the MATRIXMULTIPLICATIONA program for matrix addition, subtraction and comparison. The class CMatrix is using operator
overloading *, =, ==, >> and
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
14/34
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = 0;
}
}
}
CMatrix(const CMatrix &other)
{
strcpy(m_name, other.m_name);
m_rows = other.m_rows;
m_cols = other.m_cols;
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
}
~CMatrix()
{
for(int i = 0; i < m_rows; i++)
delete [] m_pData[i];
delete [] m_pData;
m_rows = m_cols = 0;
}
void SetName(const char *name) { strcpy(m_name, name); }
const char* GetName() const { return m_name; }
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
15/34
void GetInput()
{
std::cin >> *this;
}
void FillSimulatedInput()
{
static int factor1 = 10, factor2 = 5;
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
16/34
delete [] m_pData;
m_rows = m_cols = 0;
strcpy(m_name, other.m_name);
m_rows = other.m_rows;
m_cols = other.m_cols;
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
return *this;}
CMatrix operator + (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout m_pData[i][j] + other.m_pData[i][j];
}
}
return result;
}
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
17/34
CMatrix operator - (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout m_pData[i][j] - other.m_pData[i][j];
}
}return result;
}
CMatrix operator * (const CMatrix &other)
{
if( this->m_cols != other.m_rows)
{
std::cout m_rows, other.m_cols);
for(int i = 0; i < this->m_rows; i++)
{
for(int j = 0; j < other.m_cols; j++)
{
for(int k = 0; k < this->m_cols; k++)
{
result.m_pData[i][j] += this->m_pData[i][k] * other.m_pData[k][j];
}
}
}
return result;
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
18/34
}
bool operator == (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout m_pData[i][j] != other.m_pData[i][j])
bEqual = false;
}
}
return bEqual;
}
void DisplayMatrix()
{
std::cout > (std::istream &is, CMatrix &m);
friend std::ostream& operator > (std::istream &is, CMatrix &m)
{
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
19/34
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
20/34
b.FillSimulatedInput();
//std::cin >> a;
//std::cin >> b;
CMatrix c = a * b;
c.SetName("C");
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
21/34
Input For Row: 4 Col: 1 = 45
Input For Row: 4 Col: 2 = 50
Input For Row: 4 Col: 3 = 55
Enter Input For Matrix : B Rows: 3 Cols: 2
Input For Row: 1 Col: 1 = 25
Input For Row: 1 Col: 2 = 35
Input For Row: 2 Col: 1 = 40
Input For Row: 2 Col: 2 = 50
Input For Row: 3 Col: 1 = 55
Input For Row: 3 Col: 2 = 65
Matrix : A Rows: 4 Cols: 3
| 15 20 25 |
| 25 30 35 |
| 35 40 45 |
| 45 50 55 |
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
22/34
Matrix : B Rows: 3 Cols: 2
| 25 35 |
| 40 50 |
| 55 65 |
Matrix : C Rows: 4 Cols: 2
| 2550 3150 |
| 3750 4650 |
| 4950 6150 |
| 6150 7650 |
Press any key to continue . . .
6.) Matrix Addition, Subtraction andComparisonC++ Program to compute the MATRIX Addition,
Subtraction and Comparison
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
23/34
C++ program for matrix addition, subtraction andcomparison. The class CMatrix is using operator overloading +, -, =, ==, >> and
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
24/34
}
}
}
CMatrix(const CMatrix &other)
{
strcpy(m_name, other.m_name);
m_rows = other.m_rows;
m_cols = other.m_cols;
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{ for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
}
~CMatrix()
{
for(int i = 0; i < m_rows; i++)
delete [] m_pData[i];
delete [] m_pData;
m_rows = m_cols = 0;
}
void SetName(const char *name) { strcpy(m_name, name); }
const char* GetName() const { return m_name; }
void GetInput()
{
std::cin >> *this;
}
void FillSimulatedInput()
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
25/34
{
static int factor1 = 10, factor2 = 5;
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
26/34
m_pData = new int*[m_rows];
for(int i = 0; i < m_rows; i++)
m_pData[i] = new int[m_cols];
for(int i = 0; i < m_rows; i++)
{
for(int j = 0; j < m_cols; j++)
{
m_pData[i][j] = other.m_pData[i][j];
}
}
return *this;
}
CMatrix operator + (const CMatrix &other)
{ if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
{
std::cout m_pData[i][j] + other.m_pData[i][j];
}
}
return result;
}
CMatrix operator - (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
27/34
this->m_cols != other.m_cols)
{
std::cout m_pData[i][j] - other.m_pData[i][j];
}
}
return result;
}
CMatrix operator * (const CMatrix &other)
{
if( this->m_cols != other.m_rows)
{
std::cout m_rows, other.m_cols);
for(int i = 0; i < this->m_rows; i++)
{
for(int j = 0; j < other.m_cols; j++)
{
for(int k = 0; k < this->m_cols; k++)
{
result.m_pData[i][j] += this->m_pData[i][k] * other.m_pData[k][j];
}
}
}return result;
}
bool operator == (const CMatrix &other)
{
if( this->m_rows != other.m_rows ||
this->m_cols != other.m_cols)
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
28/34
{
std::cout m_pData[i][j] != other.m_pData[i][j])
bEqual = false;
}
}return bEqual;
}
void DisplayMatrix()
{
std::cout > (std::istream &is, CMatrix &m);
friend std::ostream& operator > (std::istream &is, CMatrix &m)
{
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
29/34
is >> m.m_pData[i][j];
}
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
30/34
CMatrix d = c - a; // so d should be equal to b
d.SetName("D");
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
31/34
Input For Row: 2 Col: 1 = 40
Input For Row: 2 Col: 2 = 50
Input For Row: 2 Col: 3 = 60
Input For Row: 3 Col: 1 = 55
Input For Row: 3 Col: 2 = 65
Input For Row: 3 Col: 3 = 75
Input For Row: 4 Col: 1 = 70
Input For Row: 4 Col: 2 = 80
Input For Row: 4 Col: 3 = 90
Matrix : A Rows: 4 Cols: 3
| 15 20 25 |
| 25 30 35 |
| 35 40 45 |
| 45 50 55 |
7.) Prime Numbers
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
32/34
C++ Program to CHECK if the Number is PrimeNumber
Or NOT Prime Number.A prime number is a number which can have only two distinct divisors that is 1 and itself.
Lets try printing a series of prime number by checking whether the number is a primenumber of not.
******************************************************************************
***********
Source Code******************************************************************************
***********
#include
#include
bool IsPrimeNumber(int num)
{
bool bPrime = true;
int factor = num / 2;
for(int i = 2; i
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
33/34
{
std::cout
-
8/9/2019 MELJUN CORTES's - C++ Source Code Algebra Application
34/34
Output :367 373 379 383 389 397
401 409 419 421 431 433
439 443 449 457 461 463
467 479 487 491 499 503
509 521 523 541 547 557
563 569 571 577 587 593
599 601 607 613 617 619
631 641 643 647 653 659
661 673 677 683 691 701
709 719 727 733 739 743
751 757 761 769 773 787
797 809 811 821 823 827
829 839 853 857 859 863
877 881 883 887 907 911
919 929 937 941 947 953
967 971 977 983 991 997
Press any key to continue . . .