ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 2 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 22 May 2017 My Price 9.00

Write a class called array2x2

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; 
}

Answers

(11)
Status NEW Posted 22 May 2017 04:05 AM My Price 9.00

-----------

Attachments

file 1495425837-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)