Not knowing what to do with this C++ assignment...

I'm a beginner and I know it's pathetic to post this on the board,

but I'm really desperate. I need to hand this til midnight I have still no

clue what to fill in the blanks. It's about matrices but I didn't learn C++

a lot so I'm stuck. Would you guys help me?

Here's the header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>

class Matrix
{
public
Matrix(int a=0, int b=0, int c=0, int d=0);
~Matrix() {}
void ShowMatrix();
Matrix Transpose();
Matrix operator+(const Matrix& a);
Matrix operator-(const Matrix& a);
Matrix operator*(const Matrix& a);
void operator=(const Matrix& a);

private:
int m[2][2];
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include "Matrix.h"
Matrix::Matrix(int a, int b, int c, int d)
{ blank }

Matrix Matrix::Transpose()
{ blank }

Matrix Matrix::operator+(const Matrix &a)
{ blank }

Matrix Matrix::operator-(const Matrix &a)
{ blank }

Matrix Matrix::operator*(const Matrix &a)
{ blank }

void Matrix::operator=(const Matrix &a)
{ blank }

void Matrix::ShowMatrix()
{ blank }


I hate myself... what's worse is that I have to make a cpp file that has 3x3 matrix.
About "what to do"

It's about matrices but I didn't learn C++

It's clear - simply start learning C++ right now. You need not learn too much for this simple task, so cheer up.

Since it is assignment which proposes that you should learn - you should, so cheer up. Many people struggled through this successfully.

See, here is a tutorial

http://www.cplusplus.com/doc/tutorial/

Though you perhaps will found something more suitable for you.

Here are some simpler tasks by which you can have enough practice:

http://codeabbey.com/


I need to hand this til midnight

It looks you'll need to ask an allowance for week more...
Last edited on
Hi Madosenshi,

there is nothing pathetic about asking for help, its why this forum is here after all (:

The first question is, are you familiar with matrices ? obviously if you haven't covered that this task will not make any sense, if you have covered matrices, most of what is being asked for here should be fairly easy:

To start with the constructor:

all it needs to do is populate the internal array of integers from the values passed in:

1
2
3
4
5
6
7
Matrix::Matrix(int a, int b, int c, int d)
{
  m[0][0]  =a;
  m[0][1]  =b;
  m[1][0]  =c;
  m[1][1]  =d;
}



these:

1
2
3
4
Matrix operator+(const Matrix& a);
Matrix operator-(const Matrix& a);
Matrix operator*(const Matrix& a);
void operator=(const Matrix& a);


are operator overloads; if someone uses your library to define two matrices and wishes to add them together, you as a programmer, need to explain to the compiler what an "add" between two "Matrices" is:

1
2
3
Matrix matrix_a(1, 2, 3, 4);
Matrix matrix_b(4, 3, 2, 1);
Matrix matrix_c = matrix_a+matrix_b;


in this situation the overloaded + operator within matrix_a would receive a constant referance to matrix_b.

thus inside the +operator definition you need to write some code to add all of the elements within the array "m" of the two matrices together, use those values to create a new matrix and then return it, so it can be used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Matrix Matrix::operator+(const Matrix &a)
{ 
   int val_0_0 =m[0][0];
   int val_0_1 =m[0][1];
   int val_0_2 =m[1][0];
   int val_0_3 =m[1][1];

   int val_1_0 =a.m[0][0];
   int val_1_1 =a.m[0][1];
   int val_1_2 =a.m[1][0];
   int val_1_3 =a.m[1][1];


 Matrix return_matrix(val_0_0+val_1_0,
                                  val_0_1+val_1_1,
                                  val_0_2+val_1_2, 
                                  val_0_3+val_1_3  )

}


the remaining functions should be very similar, and a 3v3 matrix class is at least 90% identical to this one.

I hope this helps

Thanks - NGangst
Topic archived. No new replies allowed.