Answer the question
In order to leave comments, you need to log in
How to write function overloading for vectors and matrices?
Good day!
There is a task that requires to implement a matrix class and a vector class. In my case, the matrix is the child of the vector.
The next thing to implement is the overload of functions that multiply a matrix by a matrix; vectors to matrix; matrix per vector; number per matrix, and so on. To be honest, I have no idea how to implement it.
Wrote the function ARRAYxARRAY which simply multiplies the elements of a row by the elements of a column. BUT how to make objects like MATRIX and VECTOR to use with this function. And then how else to overload the function for other operations (vector * matrix / matrix * vector).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Vector {
protected int row, column;
protected int[,] ARRAY;
protected int ROW {
get { return row; }
set { row = value; }
}
protected int COLUMN {
get { return column; }
set { column = value; }
}
public Vector() {
}
public Vector(int row, int column) {
this.row = ROW;
this.column = COLUMN;
ARRAY = new int[this.COLUMN, this.ROW];
}
public void EnterVector() {
Console.WriteLine("Choose type of vector:" +
"\nvector-row = [1]" +
"\nvector-col = [2]");
int variant = int.Parse(Console.ReadLine());
if (variant == 1) {
COLUMN = 1;
Console.Write("Enter the numbers of vector elements: ");
ROW = int.Parse(Console.ReadLine());
}
else if (variant == 2) {
ROW = 1;
Console.Write("Enter the numbers of vectror elements: ");
COLUMN = int.Parse(Console.ReadLine());
}
ARRAY = new int[COLUMN, ROW];
for (int col = 0; col < COLUMN; col++) {
for (int row = 0; row < ROW; row++) {
Console.Write("Enter the element of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
ARRAY[col, row] = int.Parse(Console.ReadLine());
}
}
}
public void Display() {
for (int col = 0; col < COLUMN; col++) {
Console.WriteLine("\n");
for (int row = 0; row < ROW; row++) {
Console.Write("{0}\t", ARRAY[col, row]);
}
}
Console.WriteLine();
}
public void ARRAYxARRAY(int[,] firstMatrix, int[,] secondMatrix, int[,] mult, int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
mult[i, j] = 0;
}
}
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
for (int k = 0; k < columnFirst; k++) {
mult[i, j] += firstMatrix[i, k];
}
}
}
}
~Vector() {
Console.WriteLine("Vector has been denied.");
}
}
class Matrix : Vector {
public Matrix() {
}
public Matrix(int row, int column) {
this.row = ROW;
this.column = COLUMN;
ARRAY = new int[this.COLUMN, this.ROW];
}
public void EnterMatrix() {
Console.Write("Enter the numbers of matrix columns: ");
COLUMN = int.Parse(Console.ReadLine());
Console.Write("Enter the numbers of matrix rows: ");
ROW = int.Parse(Console.ReadLine());
ARRAY = new int[COLUMN, ROW];
for (int col = 0; col < COLUMN; col++) {
for (int row = 0; row < ROW; row++) {
Console.Write("Enter the element of matrix cell[" + (col + 1) + ":" + (row + 1) + "]: ");
ARRAY[col, row] = int.Parse(Console.ReadLine());
}
}
}
~Matrix() {
Console.WriteLine("Matrix has been denied.");
}
}
class Program
{
static void Main()
{
Vector VECTOR = new Vector();
VECTOR.EnterVector();
Console.Write("The vector is: ");
VECTOR.Display();
Console.WriteLine("");
Matrix MATRIX = new Matrix();
MATRIX.EnterMatrix();
Console.Write("\nThe matrix is: ");
MATRIX.Display();
//////////////////////////////
}
}
Answer the question
In order to leave comments, you need to log in
You have an error. A vector is a special case of a matrix. You do an overload of c# operators for the matrix class (every Malay May programmer can do this) according to almost school formulas, ala Cij=Aij+Bij.
Then, if suddenly, for some magical reason, Putin personally asked you to add the Vector keyword to the namespace, inherit the vector from the matrix. Accordingly, all functions are automatically inherited, and no need to be smart.
I have a hard time understanding what you are trying to do.
Even less understand how you are trying to do it.
However, it looks like you need to familiarize yourself with operator overloading.
Here are a couple of articles on this topic:
https://metanit.com/sharp/tutorial/3.36.php
https://professorweb.ru/my/csharp/charp_theory/lev...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question