The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
I need help with building the code and i need it to actually work. I tried getting help before but the code does not workÂ
this needs to have three files a header file(.h) a main file(.cpp) and a testing file(testing.cpp) all in the same project this is on visual studio 2012 Â i also added the code that i had help with it does not workÂ
Â
Write a class called array2x2 which includes the following members: Private members:
o A two dimensional array to store the data: double data[2][2];
Public members:
o A default constructor which initializes data to having entries all 0. [i.e., data[i][j]=0.0 for all i and j: data=0 0 0 0]
o A function that displays the data in the following format: If data=23 −4 6 7, then display {{23, -4}; {6,7}}
o A function that given a 2x2 array A, sets the entries of data to that of A.
o A function that given a double x and indexes i and j, sets the element i,j of data to x ( data[i][j]=x). Make sure that i, j are within index bounds. [E.g. setElement(1,1, -10) will result in , data=0 0 0 −10. setElement(1,2,45) will do nothing.]
o A function that given the indexes i and j returns the value data[i] [j]. If the indexes are out of bounds, reduce/increase them to the closest legal index bound. [for example, if i=-2 and j=2 convert them to i=0 and j=1 and return data[0][1].]
o A function that returns the maximum value of the data.
o A function that searches for a value in data. If value is found, return 1 (true) and the indexes of the position found. If it is not found, return 0 (false) and -1, -1 for the indexes. (Hint: pass indexes as reference parameters)
Write a class called matrix2x2 derived from array2x2 via public inheritance. In addition, this news class should have the following member functions:
Public members:
o A constructor with one default parameter set to 0. If the value of the parameter is b, then data should be initialized to: data= b 0 0 b [i.e., data[0][0]=b, data[1][1]=b, the other entries 0].
o A function display (overwriting the display function of array2x2), that displays the data as a b c d
o A function that returns the determinant.
[if data=a b c d then the determinant is defined as the number (a*b-c*d).]
o A function that multiplies the matrix by a number x.
[if data=a b c d then the function returns a matrix2x2 with data= a*xb*xc*'s*x]
o A function that sums two matrices. I.e. matrix2x2 sumMatrix(matrix2x2 M) [If data=a b c d and M.data=e f g h then return a matrix2x2 with data=a+eb+fc+gd+h]
In the main function (outside the class), create a function that subtracts two matrices. Hint: use the last two public functions implemented inside the class
For all the above write a main program that tests all the functions. When a function makes changes to the array/matrix, display before and after effects by printing the array/matrix using display function implemented.
Â
#include <iostream>
using namespace std;
// array2x2 class with all member varaibles and fucntions
class array2x2
{
public:
// Constructor
array2x2(void)
{
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
data[i][j]=0;
}
}
}
//Display Method
void display(){
cout<<"{{"<<data[0][0]<<","<<data[0][1]<<"};{"<<data[1][0]<<","<<data[1][1]<<"}}"<<endl;
}
// Set Values
void setEntries(double array[2][2]){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
data[i][j]=array[i][j];
}
}
}
// Set element individually
void setElement(int i, int j, double x){
if(i<2 && j<2)
{
data[i][j] = x;
}
}
// Get the data value individually
double getData(int i, int j){
double value;
if(((i<2)&&(i>0))&& ((j<2)&&(j>0))){
value = data[i][j];
}
else if((i<0 || i>1) && ((j<2)&&(j>0))){
i = i<0? 0: 1;
value = data[i][j];
}
else if((j<0 || j>1) && ((i<2)&&(i>0))){
j = j<0? 0: 1;
value = data[i][j];
}
}
// Get the max value
double getmax(){
double greaterNumber = data[0][0];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
if(data[i][j]>greaterNumber)
{
greaterNumber = data[i][j];
}
}
}
return greaterNumber ;
}
// Search for a value
int searchData(double dataValue){
int isFound = 0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
if(data[i][j] == dataValue)
{
isFound = 1;
cout<<"The index is: "<<i<<", "<<j<<endl;
break;
}
}
}
if(isFound == 0)
{
cout<<"The index is: -1, -1"<<endl;
}
return isFound;
}
private:
double data[2][2];
};
class matrix2x2: public array2x2
{
public:
Double matArray[2][2];
matrix2x2(int b=0){
array2x2::setElement(0,0,b);
array2x2::setElement(1,1,b);
}
void display(){
cout<<<<array2x2::getData(0,0)<<" "<<array2x2::getData(0,1)<<" "<<array2x2::getData(1,0)<<" "<<array2x2::getData(1,1)<<endl;
}
double determinant(){
double det = (array2x2::getData(0,0) * array2x2::getData(0,1)) - (array2x2::getData(1,0) * array2x2::getData(1,1));
return det;
}
void mulMatrix(int x){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
double dataVal = array2x2::getData(i,j);
array2x2::setElement(i,j dataVal*x);
}
}
}Â
void sumMatrix(matrix2x2 M){
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
double dataVal = array2x2::getData(i,j);
array2x2::setElement(i,j,dataVal+M.getData(i,j));
}
}
}
};
int main(void)
{
matrix2x2 mat(45.0);
cout<<"Initial Matrix"<<endl;
mat.display();
mat.determinant();
cout<<"After Determinant"<<endl;
mat.display();
cout<<"After Multiplication"<<endl;
mat.mulMatrix(3);
mat.display();
cout<<"After Summation:"<<endl;
matrix2x2 mat2(23.9);
mat.sumMatrix(mat2);
mat.display();
return 0;Â
}
-----------