0

Currently, I'm working on creating matrices for cryptography projects. However, I'm having an issue when trying to pass a matrix to a function without having the size for the rows and columns.

Summary of code:

In main, I create an instance of a Cipher class, I'm allowed to get an input file (plaintext) and a key (key is used for enc/decryption).

Then, in Cipher.cpp, I do the blocks for the class functions. For this question, I'll focus on encryption function.

Inside of it, I initialized a char matrix[plaintext.size()][keySize].

Now, because of modularity, I want to pass this matrix by reference to a function that generates the matrix. This is the issue, I cannot pass the matrix with two variables (plaintextSize, keySize), I can only pass it with constant values (e.g char (&matrix)[5][5]).

I've also tried passing like char* matrix, but that obviously doesn't work. This issue leaves me with an encryption function that does two different things: generating the matrix and doing the actual encryption, which is something I don't want.

Any help will be appreciated it.

tadman
  • 208,517
  • 23
  • 234
  • 262
AndF
  • 23
  • 4
  • 2
    Easiest way would be to use a single-dimension vector instead of multi-dimensional matrices. You can always translate access with two dimensions into a single-dimension access based on offset. Than represent this as either `std::vector` or `std::array` and pass this to your function (likely by reference) – SergeyA Mar 29 '21 at 18:50
  • 1
    "something I don't want" -- Why not? https://en.wikipedia.org/wiki/Separation_of_concerns – Woodford Mar 29 '21 at 18:51
  • 1
    "... but that obviously doesn't work" it isnt obvious. Passing a `char*` is fine in principle. Better show your code ([mcve]) instead of describing it. What is obvious to you isnt obvious to others and vice versa – 463035818_is_not_an_ai Mar 29 '21 at 18:54
  • I've heard the term black box used to describe desired function behavior. Basically, seeing the signature and maybe a descriptive comment should make it very clear what the function's purpose is, and you don't have to care *how* it goes about its business. Also the standalone nature of a free function, i.e., not relying on variables outside the scope (globals, etc.). – sweenish Mar 29 '21 at 18:58
  • Thank you @SergeA. What I did was declare the vector (vector> matrix, then I passed it to GenerateMatrix by reference, with size of rows, columns, and the plaintext as additional paramaters. Then inside GenerateMatrix, I resized it (resize()), and then I filled it up with the chars of the plaintext. It works, and now I have two functions that are in charge of only aspect of the program. – AndF Mar 29 '21 at 21:12

1 Answers1

-2

You can do this in one of two basic ways:

  1. you can pass a vector of vectors

  2. you can pass a char * plus the rows and columns and do your own math

    int foo(char *array, int numRows, int numCols) { int row = 4; int col = 3; int index = row * numCols + col; cout << row << ", " << col << " == " << array[index] << endl; }

It's kind of ugly, though.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36