0% found this document useful (0 votes)
14 views25 pages

Mathematics Group Assignment

Uploaded by

kristiesnow0109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views25 pages

Mathematics Group Assignment

Uploaded by

kristiesnow0109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

KT14503 Mathematics for Computing

Semester 1, Session 2021/2022

Section 3

Group Assignment

Prepared for: Dr. Azali Saudi

Prepared by:

No. Name Matric No.


1 JAMIE LIM MAE WAN BI21110335
2 KRISTIE CHIANG SHEN SHUET BI21110268
3 KEW SIN HAO BI21110100
4 LIN QIN LU BI21110262
5 REENICA CHANG PEI WEI BI21110238
6 WONG SOOK WEI BI21110173
Part 1: Application of Differentiation and Integration (10 Marks)

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
cout << "Please input the expression\n";
cout << "For example: 2t^3 is coefficient = 2, power = 3\n";

double coefficient1, coefficient2, power1, power2;

cout << "Coefficient for expression 1: ";


cin >> coefficient1;

cout << "\nPower for expression 1: ";


cin >> power1;

cout << "\nCoefficient for expression 2: ";


cin >> coefficient2;

cout << "\nPower for expression 2: ";


cin >> power2;

//Prompt the user to choose type of equation


cout << "\nIs this an equation for" << endl;
cout << "1. Displacement\n";
cout << "2. Velocity\n";
cout << "3. Acceleration\n";

double equation, valueT, choice, answer;


cin >> equation;

cout << "\n Value of t = ";


cin >> valueT;

cout << "\n What would you like to find:" <<


endl; cout << "1. Displacement\n";
cout << "2. Velocity\n";
cout << "3. Acceleration\n";
cin >> choice;

switch (int (choice))


{
case 1 : if (equation == 1)
{
//Calculate value T
answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *
pow(valueT, power2));
cout << "\n Displacement when t = " << valueT << " is " << answer;
break
; }
else if (equation == 2)
{
//Integrating the equation
coefficient1 = coefficient1 / (power1 + 1);
power1 += 1;
coefficient2 = coefficient2 / (power2 + 1);
power2 += 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));

cout << "\n Displacement when t = " << valueT << " is " << answer;
break
; }

else if (equation == 3)
{
//First Integration
coefficient1 = coefficient1 / (power1 + 1);
power1 += 1;
coefficient2 = coefficient2 / (power2 + 1);
power2 += 1;

//Second Integration
coefficient1 = coefficient1 / (power1 + 1);
power1 += 1;
coefficient2 = coefficient2 / (power2 + 1);
power2 += 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));
cout << "\n Displacement when t = " << valueT << " is " << answer;
break
; }

case 2 : if (equation == 1)
{
// Differentiate the equation
coefficient1 = coefficient1 * power1;
power1 = power1 - 1;
coefficient2 = coefficient2 * power2;
power2 = power2 - 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));
cout << "\n Velocity when t = " << valueT << " is " << answer; break;
}
else if (equation == 2)
{
//Calculate value T
answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *
pow(valueT, power2));
cout << "\n Velocity when t = " << valueT << " is " << answer; break;
}
else if (equation == 3)
{
//Integrate the equation
coefficient1 = coefficient1 / (power1 + 1);
power1 += 1;
coefficient2 = coefficient2 / (power2 + 1);
power2 += 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));
cout << "\n Velocity when t = " << valueT << " is " << answer; break;
}

case 3 : if (equation == 1)
{
// First Differentiation
coefficient1 = coefficient1 * power1;
power1 = power1 - 1;
coefficient2 = coefficient2 * power2;
power2 = power2 - 1;

// Second Differentiation
coefficient1 = coefficient1 * power1;
power1 = power1 - 1;
coefficient2 = coefficient2 * power2;
power2 = power2 - 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));
cout << "\n Acceleration when t = " << valueT << " is " << answer;
break
; }
else if (equation == 2)
{
// Differentiate the equation
coefficient1 = coefficient1 * power1;
power1 = power1 - 1;
coefficient2 = coefficient2 * power2;
power2 = power2 - 1;

answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *


pow(valueT, power2));
cout << "\n Acceleration when t = " << valueT << " is " << answer;
break
; }
else if (equation == 3)
{
//Calculate value T
answer = (coefficient1 * pow(valueT, power1)) + (coefficient2 *
pow(valueT, power2));
cout << "\n Acceleration when t = " << valueT << " is " << answer;
break
; }
default : break;

}
return 0;
}
Screenshots of test cases for the possible scenario:

Sample Output 1

D(t) = 4t3 – 3t2


V(t) = 12t2 – 6t
A(t) = 24t – 6
When t = 3,
A(3) = 24(3) – 6
= 66
Sample Output 2

V(t) = 6t2 – 4t
6 4
D(t) = t3 - t2
3 2
= 2t3 – 2t2
When t = 5,
D(5) = 2(5)3 – 2(5)2
= 200
Sample Output 3

A(t) = 4t2 – 2t
4 2
V(t) = t3 – t2
3 2
4
= t3 – t2
3
When t = 2,
4
V(2) = (2)3 – (2)2
3
20
=
3
Part 2 Application of Linear Algebra (10 Marks)

Code:
#include <iostream>
#include <cmath>
using namespace std;

int getChoose();
void addition_and_subtraction_of_matrix(int );
void multiply_of_matrix(int );

int main()
{
int pilih, result;
pilih = getChoose();
addition_and_subtraction_of_matrix(pilih);
multiply_of_matrix(pilih);
return 0;
}

int getChoose() //Function to get the choose


{
cout<<"Hello, which calculation would you like to perform?"<<endl;
cout<<"Enter 1 for Matrix Addition or Substraction"<<endl;
cout<<"Enter 2 for Matrix Multiplication"<<endl;
int choose = 0;
cin>>choose;
return choose;
}

void addition_and_subtraction_of_matrix(int choose)


{
if (choose==1) //For add or subtract of matrix
{
//Enter element of first matrix
int r1, c1,r2,c2, a[5][5], b[5][5], sum[5][5], subtract [5][5], i, j;
cout<<"Input Matrix A: "<<endl;
cout<<"How many rows?"<<endl;
cin>> r1;
cout<<"How many columns?"<<endl;
cin>> c1;

//Storing element of first matrix


for (i=0; i<r1; ++i)
{
for(j=0; j<c1; ++j)
{
cout<<"Enter R"<< i+1<<"C" <<j+1<<" ";
cin>> a[i][j];
}
cout<<"\n";
}

//Displaying the first matrix


cout<<"Matrix A: "<<endl;
for(i=0; i<r1; ++i)
{
for(j=0; j<c1; ++j)
{
cout<<a[i][j]<<" ";
if (j == c1 - 1)
{
cout<< endl;
}
}
}

//Enter element of second matrix


cout<<"\nInput Matrix B,"<<endl;
cout<<"How many rows?"<<endl;
cin>> r2;
cout<<"How many columns?"<<endl;
cin>> c2;

//Storing element of second matrix


for (i=0; i<r2; ++i)
{
for(j=0; j<c2; ++j)
{
cout<<"Enter R"<< i+1<<"C" <<j+1<<" ";
cin>> b[i][j];
}
cout<<"\n";
}

//Displaying the second matrix


cout<<"Matrix B: "<<endl;
for(i=0; i<r2; ++i)
{
for(j=0; j<c2; ++j)
{
cout<<b[i][j]<<" ";
if (j == c2 - 1)
{
cout<< endl;
}
}
}

//If the matrix is not compatible


if ((r1 != r2) || (c1 != c2))
{
cout<<"\nIncompatible matrices for addition or subtraction!"<<endl;
cout<<"Number of rows and columns for Matrix A must be the same as number of
rows and columns for Matrix B"<<endl;
}

//If compatible, ask the user to proceed addition or subtraction of


matrix
else
{
cout<<"\nAdd(1) or Minus(2) A and B? ";
int add_or_minus = 0;
cin>> add_or_minus;
//Proceed addition if user enter '1"
if (add_or_minus == 1)
{
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
sum[i][j]= a[i][j] + b[i][j];
}
}
//Display the result of sum matrix
cout<<"Result: "<<endl;
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
cout<<sum[i][j]<<" ";
if(j == c2-1)
{
cout<< endl;
}
}
}
}
if (add_or_minus == 2)
{
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
subtract[i][j]= a[i][j] - b[i][j];
}

}
//Display the result of substract matrix
cout<<"Result: "<<endl;
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
cout<<subtract[i][j]<<" ";
if(j == c2-1)
{
cout<< endl;
}
}
}
}

//If input is not equal to 1&2, user have entered wrong instruction
if (add_or_minus != 1 && add_or_minus != 2)
{
cout<<"You have entered a wrong instruction!"<<endl;
}
}
}
}

void multiply_of_matrix(int choose)


{
if (choose==2) //For multiplication of matrix
{
//Enter element of first matrix
int r1, c1,r2,c2, a[5][5], b[5][5], i, j, k;
double mult[5][5];
cout<<"Input Matrix A: "<<endl;
cout<<"How many rows?"<<endl;
cin>> r1;
cout<<"How many columns?"<<endl;
cin>> c1;

//Storing element of first matrix


for (i=0; i<r1; ++i)
{
for(j=0; j<c1; ++j)
{
cout<<"Enter R"<< i+1<<"C" <<j+1<<" ";
cin>> a[i][j];
}
cout<<"\n";
}

//Displaying the first matrix


cout<<"Matrix A: "<<endl;
for(i=0; i<r1; ++i)
{
for(j=0; j<c1; ++j)
{
cout<<a[i][j]<<" ";
if (j == c1 - 1)
{
cout<< endl;
}
}
}

//Enter element of second matrix


cout<<"\nInput Matrix B,"<<endl;
cout<<"How many rows?"<<endl;
cin>> r2;
cout<<"How many columns?"<<endl;
cin>> c2;

//Storing element of second matrix


for (i=0; i<r2; ++i)
{
for(j=0; j<c2; ++j)
{
cout<<"Enter R"<< i+1<<"C" <<j+1<<" ";
cin>> b[i][j];
}
cout<<"\n";
}

//Displaying the second matrix


cout<<"Matrix B: "<<endl;
for(i=0; i<r2; ++i)
{
for(j=0; j<c2; ++j)
{
cout<<b[i][j]<<" ";
if (j == c2 - 1)
{
cout<< endl;
}
}
}

// If column of first matrix is not equal to row of second matrix,


if (c1 != r2)
{
cout<<"\nIncompatible matrices for multiplication!"<<endl;
cout<<"Number of columns for Matrix A must be the same as number of rows for
Matrix B"<<endl;
}

//If the matrix is compatible


else
{
// Initializing elements of matrix a and b and storing in array mult.
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
}

// Multiplying matrix a and b and storing in array mult.


for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}
}
}
//Display the result of multiplication of two matrix
cout<<"\nResult: "<<endl;
for(i = 0; i < r1; ++i)
{
for(j = 0; j < c2; ++j)
{
if(mult[i][j]>=1000)
{
mult[i][j] = mult[i][j] / 100;
}
cout<<mult[i][j]<<" ";

if(j == c2-1)
{
cout<< endl;
}
}
}
}
}
}
Screenshots of test cases for the possible scenario:

Case:
Given the individual assessment results of 3 students and the
allocation percentage of each assessment for the course, calculate the
total marks of each student for the course.
Student Quiz Assignmen Proje Exam
t ct
A 80 89 100 92
B 75 80 85 72
C 92 85 78 88

Assessment Allocation
Percentage
Quiz 10 %
Assignment 10 %
Project 20 %
Exam 60 %
Test result:
Success case:

Sample Output 1

1 2 1 0 2 2
Matrix A + Matrix B = [ ] + [ ] = [ ]
3 4 0 1 3 5
Sample Output 2

1 2 1 0 0 2
Matrix A - Matrix B = [ ] - [ ] = [ ]
3 4 0 1 3 3
Sample Output 3

1 2 3 1 0 1 2 2 4
Matrix A + Matrix B = [4 5 6] + [0 1 0] = [4 6 6]
7 8 9 1 0 1 8 8 10
Sample Output 4

1 2 3 1 0 1 0 2 2
Matrix A - Matrix B = [3 4 5] - [0 1 0] = [3 3 5]
5 6 7 1 0 1 4 6 6
Sample Output 5

1 2 3 1 0 0 2 2 3
Matrix A + Matrix B = [ ] + [ ] = [ ]
4 5 6 1 1 0 5 6 6
Sample Output 6

1 2 3 1 0 0 0 2 3
Matrix A - Matrix B = [ ] - [ ] = [ ]
4 5 6 1 1 0 3 4 6
Sample Output 7

1 0
1 2 3
Matrix A × Matrix B = [ ] × [0 1]
4 5 6
1 0

(1 + 0 + 3) (0 + 2 + 0)
= [ ]
(4 + 0 + 6) (0 + 5 + 0)

4 2
= [ ]
10 5
Error case:

Sample Output 1

1 2 3
Matrix A + Matrix B = [ ] - [3 2 1] = ERROR!
4 5 6
Sample Output 2

1 2 3 7 8 9
Matrix A × Matrix B = [ ] × [ ] = ERROR!
4 5 6 1 0 0

You might also like