c++ : "no matching function for call to" error with function - c++

I have a function "mod_kadane" defined in class "MAX_SUB_MAT" which returns an object.
#define dx 101
class MAX_SUB_MAT
{
public:
int curr_sum, max_sum, left, right, up, down;
MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
MAX_SUB_MAT objx;
curr_sum = objx.max_sum = INT_MIN;
int sub_mat[row];
for(int L = 0; L < row; L++)
{
memset(sub_mat, 0, sizeof sub_mat);
for(int R = L; R < column; R++)
{
for(int i = 0; i < row; i++)
{
sub_mat[i] += i;//mat[i][R];
}
MAX_SUB_ARR obj;
obj = obj.kadane(sub_mat, row);
curr_sum = obj.maxima;
if(curr_sum > objx.max_sum)
{
objx.max_sum = curr_sum;
objx.left = L;
objx.right = R;
objx.up = obj.left;
objx.down = obj.right;
}
}
}
return objx;
}
};
But when I'm trying to call it from "main":
cin >> row >> column;
int mat[row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin >> mat[i][j];
}
}
MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);
then an error is shown:
error: no matching function for call to 'MAX_SUB_MAT::mod_kadane(int [row][column], int&, int&)'|
But if I remove the 2d array "mat" from both sides then the code works fine.
Here is my full code:
#include<bits/stdc++.h>
#define dx 101
using namespace std;
class MAX_SUB_ARR
{
public:
int max_curr, maxima, left, right;
MAX_SUB_ARR kadane(int arr[], int n)
{
MAX_SUB_ARR obj;
obj.maxima = max_curr = INT_MIN;
//cout << obj.maxima << endl;
/*for(int i = 0; i < n; i++)
cout << arr[i] << endl;*/
for(int i = 0; i < n; i++)
{
if(max_curr < 0)
{
max_curr = arr[i];
obj.left = i;
}
else
{
max_curr += arr[i];
}
//maxima = max(maxima, max_curr);
if(max_curr > obj.maxima)
{
obj.maxima = max_curr;
obj.right = i;
//cout << obj.maxima << endl;
}
}
return obj;
}
};
class MAX_SUB_MAT
{
public:
int curr_sum, max_sum, left, right, up, down;
/* MAX_SUB_MAT(int r, int c)
{
row = r;
column = c;
}*/
MAX_SUB_MAT mod_kadane(int mat[dx][dx], int row, int column)
{
MAX_SUB_MAT objx;
curr_sum = objx.max_sum = INT_MIN;
int sub_mat[row];
for(int L = 0; L < row; L++)
{
memset(sub_mat, 0, sizeof sub_mat);
for(int R = L; R < column; R++)
{
for(int i = 0; i < row; i++)
{
sub_mat[i] += i;//mat[i][R];
}
MAX_SUB_ARR obj;
obj = obj.kadane(sub_mat, row);
curr_sum = obj.maxima;
if(curr_sum > objx.max_sum)
{
objx.max_sum = curr_sum;
objx.left = L;
objx.right = R;
objx.up = obj.left;
objx.down = obj.right;
}
}
}
return objx;
}
};
int main()
{
int row, column;
printf("Number of rows you want to insert?:\n");
cin >> row;
printf("Number of columns you want to insert?:\n");
cin >> column;
int mat[row][column];
if(row && column)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin >> mat[i][j];
}
}
//int curr_sum, max_sum, left, right, up, down;
//MAX_SUB_MAT objx = new MAX_SUB_MAT(row, column);
MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);
for(int i = objx.up; i <= objx.down; i++)
{
for(int j = objx.left; j <= objx.right; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
return 0;
}

It is not true that in C or C++ we cannot declare a function with two dimensional array parameter with both sizes specified.
See: http://c-faq.com/aryptr/pass2dary.html
The problem has been correctly explained by #AlanStokes in his comments!

Related

Sequence Alignment problem using Pthreads

I am trying to implement Sequence alignment problem (Needleman-Wunsch algorithm) using p-Threads. I am confused how to map the concept of multi threading in this particular serial problem. The code of serial computation of sequence alignment is enclosed below.(Just Matrix calculation part)
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
class Matrix
{
private:
int x;
int y;
int** mat;
string gene1;
string gene2;
int match_penalty;
int mismatch_penalty;
int gap_penalty;
int minimum_penalty;
public:
Matrix(int gene1Len, int gene2Len)
{
x = gene2Len + 1; //gene2 length
y = gene1Len + 1; //gene 1 length;
mat = new int* [x];
for (int i = 0; i < x; i++)
mat[i] = new int[y];
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
mat[i][j] = 0;
}
}
//Default Penalties
match_penalty = 1;
mismatch_penalty = 3;
gap_penalty = 2;
minimum_penalty=0;
}
void Print_Matrix()
{
cout<<"\n";
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
cout << mat[i][j] << "\t";
}
cout << "\n";
}
}
void setGenes(string gene1, string gene2)
{
this->gene1 = gene1;
this->gene2 = gene2;
}
void setPenalty(int mismatch, int gp,int match)
{
mismatch_penalty = mismatch;
gap_penalty = gp;
match_penalty=match;
}
void setMatrix()
{
//1st row and 1st Column values
for (int i = 0; i < x; i++)
{
mat[i][0] = i * gap_penalty;
}
for (int i = 0; i < y; i++)
{
mat[0][i] = i * gap_penalty;
}
// Other matrix values
for (int i = 1; i < x; i++)
{
for (int j = 1; j < y; j++)
{
if (gene1[j - 1] == gene2[i - 1]) //Similar gene values (A==A ||T==T)
{
mat[i][j] = mat[i - 1][j - 1]+match_penalty;
}
else
{
mat[i][j] = max({ mat[i - 1][j - 1] + mismatch_penalty , mat[i - 1][j] + gap_penalty, mat[i][j - 1] + gap_penalty });
}
}
}
}
};
int main()
{
string gene1 = "ACCA";
string gene2 = "CCA";
int matchPenalty;
int misMatchPenalty ;
int gapPenalty ;
cout<<"Enter the value of Match Penalty" << endl;
cin >> matchPenalty;
cout<<"Enter the value of misMatchPenalty Penalty" << endl;
cin >> misMatchPenalty;
cout<<"Enter the value of gapPenalty Penalty" << endl;
cin >> gapPenalty;
Matrix dp(gene1.length(), gene2.length());
dp.setGenes(gene1, gene2);
dp.setPenalty(misMatchPenalty, gapPenalty,matchPenalty);
dp.setMatrix();
dp.Print_Matrix();
}
How can I implement the above problem in P-threads? So far, I have used two threads to calculate matrix values of 1st column and 1st row simultaneously.But I have no idea how to compute all values of matrix in parallel. Kindly see my source code:
#include<string>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<pthread.h>
using namespace std;
//Global variables --shared by all threads
int matchPenalty;
int misMatchPenalty;
int gapPenalty;
struct gene_struct {
string gene1;
string gene2;
int rowSize;
int colSize;
int **mat;
};
void* set_matrix_row(void *args)
{
struct gene_struct *shared_block = (struct gene_struct *) args;
for (int i = 0; i < shared_block->rowSize; i++)
{
shared_block->mat[i][0] = i * gapPenalty;
}
return NULL;
}
void *set_matrix_column(void *args)
{
struct gene_struct *shared_block = (struct gene_struct *) args;
for (int i = 0; i < shared_block->colSize; i++)
{
shared_block->mat[0][i] = i * gapPenalty;
}
return NULL;
}
void set_Matrix_Diagnol(struct gene_struct shared_block)
{
//How Should I calculate rest of the matrix values using Pthreads?
}
void Print_Matrix(struct gene_struct shared_block)
{
cout<<"\n";
for (int i = 0; i < shared_block.rowSize; i++)
{
for (int j = 0; j < shared_block.colSize; j++)
{
cout << shared_block.mat[i][j] << "\t";
}
cout << "\n";
}
}
int main()
{
pthread_t ptid1, ptid2;
string gene1, gene2;
struct gene_struct shared_block;
cout << "Enter First Gene : ";
cin >> gene1;
cout << "Enter Second Gene : ";
cin >> gene2;
cout<<"Enter the value of Match Penalty" << endl;
cin >> matchPenalty;
cout<<"Enter the value of misMatchPenalty Penalty" << endl;
cin >> misMatchPenalty;
cout<<"Enter the value of gapPenalty Penalty" << endl;
cin >> gapPenalty;
shared_block.gene1 = gene1;
shared_block.gene2 = gene2;
shared_block.rowSize = gene2.length()+1;
shared_block.colSize = gene1.length()+1;
shared_block.mat = new int* [shared_block.rowSize]; //col = gene2+1
for (int i = 0; i < shared_block.rowSize; i++)
{
shared_block.mat[i] = new int[shared_block.colSize];
}
pthread_create(&ptid1, NULL, &set_matrix_row, (void *)&shared_block);
pthread_create(&ptid2, NULL ,&set_matrix_column, (void *)&shared_block);
pthread_join(ptid1,NULL);
pthread_join(ptid2,NULL);
Print_Matrix(shared_block);
}

access violation writing location c++ for Gaussian Elimination

I am working on a code for gaussian reduction, but for some reason I am getting the error:
Exception thrown at 0x00A9F6DF in Excersise-7-3.exe: 0xC0000005: Access violation writing location 0xFDFDFDFD.
at S[h][0] = i; (line 147)
I cant seem to resolve this. What should I do? How can I prevent this from happening in the future?
#include "stdafx.h"
#include "Gauss.h"
#include <fstream>
Gauss::Gauss()
{
x.resize(v.size());
vector<double>::iterator it;
for (it = x.begin(); it != x.end(); it++)
{
v[*it] = 0;
}
S = new double*[m];
for (int i = 0; i < m; i++)
{
S[i] = new double[3];
for (int j = 0; j < 3; j++)
{
S[i][j] = 0;
}
}
}
Gauss::~Gauss()
{
}
bool Gauss::read_vector(string filename)
{
ifstream in;
in.open(filename);
if (in.fail())
{
cout << "File could not be opened." << endl;
return false;
}
in >> size;
for (int i = 0; i < size; i++)
{
int y;
in >> y;
v.push_back(y);
}
in.close();
return true;
}
bool Gauss::read_matrix(string filename)
{
ifstream in;
in.open(filename);
if (in.fail())
{
cout << "File could not be opened." << endl;
return false;
}
in >> m;
in >> n;
A = new double *[m];
for (int i = 0; i < m; i++)
{
A[i] = new double[n];
for (int k = 0; k < n; k++)
{
in >> A[i][k];
}
}
in.close();
return true;
}
void Gauss::print_vector()
{
for (int i = 0; i < size; i++)
cout << v[i] << endl;
}
void Gauss::print_matrix()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}
void Gauss::elimination(int &i, int &k)
for (int j = 0; j < m - 1; j++)
{
if(j != i)
{
double fraction = A[j][k] / A[i][k];
for (int l = 0; l < n; l++)
{
A[j][l] = A[j][l] - fraction*A[i][l];
v[j] = v[j] - fraction*v[i];
}
}
}
}
void Gauss::row_swap(int &i, int &k)
{
double sub;
for (int j = 0; j < n; j++)
{
sub = A[i][j];
A[i][j] = A[k][j];
A[k][j] = sub;
}
{
sub = v[i];
v[i] = v[k];
v[k] = sub;
}
}
void Gauss::reduction()
{
int h = 0;
int i = 0;
int k = 0;
while (i != m || k != n)
{
if (A[i][k] != 0)
{
elimination(i, k);
S[h][0] = i;
S[h][1] = k;
S[h][2] = A[i][k];
h++;
cout << h;
}
else
{
for (int j = i + 1; j < m; j++)
{
if (A[j][k] > 0)
{
row_swap(i, k);
elimination(i, k);
S[h][0] = i;
S[h][1] = k;
S[h][2] = A[i][k];
h++;
}
else
{
k++;
}
}
}
i++;
k++;
}
}
void Gauss::solution()
{
for (int j = 0; j < m; j++)
{
x[S[j][1]] = v[S[j][0]] / S[j][2];
}
}

Kosaraju's Algorithm for spoj's BOTTOM

I am trying to solve http://www.spoj.com/problems/BOTTOM/
Here are the steps I am following:
1) Find the strongly connected components using Kosaraju's algorithm. 2) Consider a strongly connected component. Consider an edge u. Now consider all edges from u to some vertice v. If v lies in some other SCC, eliminate the whole strongly conected component. Else include all the elements in the solution.
However, I am constantly getting WA. Please help.
Here is my code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <iterator>
#include <queue>
using namespace std;
int k = 0;
int V, E;
bool fix[5001];
bool fix2[5001];
int compNum[5001];
void dfs(int v, vector< vector<int> >&G, bool *fix, vector <int> &out) {
fix[v] = true;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (!fix[u]) {
fix[u] = true;
dfs(u, G, fix, out);
}
}
out.push_back(v);
}
void dfs2(int v, vector< vector<int> >&G, bool *fix2, vector < vector<int> > &components) {
fix2[v] = true;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (!fix2[u]) {
fix2[u] = true;
dfs2(u, G, fix2, components);
}
}
components[k].push_back(v);
compNum[v] = k;
}
int main() {
int a, b;
while (true) {
cin >> V; if (V == 0) break; cin >> E;
vector< vector<int> >G(V + 1);
vector< vector<int> >G2(V + 1);
vector<int>out;
vector < vector<int> >components(V + 1);
for (int i = 0; i < E; i++) {
cin >> a >> b;
G[a].push_back(b);
G2[b].push_back(a);
}
for (int i = 1; i <= V; i++) {
if (!fix[i])
dfs(i, G, fix, out);
}
reverse(out.begin(), out.end());
for (int i = 0; i < out.size(); i++){
if (!fix2[out[i]]) {
dfs2(out[i], G2, fix2, components);
k++;
}
}
vector<int>gamotana;
for (int i = 0; i < components.size(); i++) {
for (int j = 0; j < components[i].size(); j++) {
bool check = true;
for (int z = 0; z < G[components[i][j]].size(); z++)
{
if (compNum[G[components[i][j]][z]] != i)
{
check = false; goto next123;
}
}
if (check)
gamotana.push_back(components[i][j]);
}
next123:;
}
sort(gamotana.begin(), gamotana.end());
for (int i = 0; i < gamotana.size(); i++)
cout << gamotana[i] << " ";
for (int i = 0; i < 5001; i++) {
fix[i] = false;
fix2[i] = false;
compNum[i] = -1;
}
k = 0;
cout << endl;
}
return 0;
}
In your algorithm description you say you eliminate the entire connected component if some edge leads to a different component.
However, in your code you appear to add all vertices j in component i to your solution until you find an edge leading out. In other words, even if a component is not a sink you may still incorrectly report some of the vertices as being sinks.
I imagine you should do something more like this:
for (int i = 0; i < components.size(); i++) {
for (int j = 0; j < components[i].size(); j++) {
for (int z = 0; z < G[components[i][j]].size(); z++)
{
if (compNum[G[components[i][j]][z]] != i)
{
goto next123;
}
}
}
for (int j = 0; j < components[i].size(); j++)
gamotana.push_back(components[i][j]);
next123:;
}
Of course, there may be more issues. I would recommend you try constructing and testing some small examples first, and perhaps testing against a brute force solver to identify failing cases.
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<int>* edges, stack<int>& finishedVertices, bool* visited, int n, int start){
visited[start] = true;
for(int i = 0 ; i < edges[start].size() ; i++){
int node = edges[start][i];
if(!visited[node]){
dfs(edges, finishedVertices, visited, n, node);
}
}
finishedVertices.push(start);
}
void dfs_reverse(vector<int>* edgesT, bool* visited, unordered_map<int,vector<int>>& SCC, int node, int k){
SCC[k].push_back(node);
visited[node] = true;
for(int i = 0 ; i < edgesT[node].size() ; i++){
int new_node = edgesT[node][i];
if(!visited[new_node]){
dfs_reverse(edgesT, visited, SCC, new_node, k);
}
}
}
void getSCC(vector<int>* edges, vector<int>* edgesT, int n){
bool* visited = new bool[n];
for(int i = 0 ; i < n ; i++){
visited[i] = false;
}
stack<int> finishedVertices;
for(int i = 0 ; i < n ; i++){
if(!visited[i]){
dfs(edges, finishedVertices, visited, n, i);
}
}
unordered_map<int,vector<int>> SCC;
int k = 0;
for(int i = 0 ; i < n ; i++){
visited[i] = false;
}
while(!finishedVertices.empty()){
int node = finishedVertices.top();
finishedVertices.pop();
if(!visited[node]){
dfs_reverse(edgesT, visited, SCC, node, k);
k++;
}
}
int flag = 1;
vector<int> ans;
vector<int> bottom;
for(int i = 0 ; i < k ; i++){
for(int j = 0 ; j < SCC[i].size(); j++){
ans.push_back(SCC[i][j]);
}
for(int m = 0 ; m < ans.size() ; m++){
int node = ans[m];
for(int j = 0 ; j < edges[node].size() ; j++){
int new_node = edges[node][j];
vector<int> :: iterator it;
it = find(ans.begin(), ans.end(), new_node);
if(it == ans.end()){
flag = 0;
break;
}
}
if(flag == 0)
break;
}
if(flag == 1){
for(int j = 0 ; j < ans.size() ; j++)
bottom.push_back(ans[j]);
}
flag = 1;
ans.clear();
}
sort(bottom.begin(), bottom.end());
for(int i = 0 ; i < bottom.size() ; i++)
cout << bottom[i] + 1 << " ";
cout << endl;
}
int main(){
while(true){
int n;
cin >> n;
if(n == 0)
break;
vector<int>* edges = new vector<int>[n];
vector<int>* edgesT = new vector<int>[n];
int e;
cin >> e;
for(int i = 0 ; i < e ; i++){
int x, y;
cin >> x >> y;
edges[x-1].push_back(y-1);
edgesT[y-1].push_back(x-1);
}
getSCC(edges, edgesT, n);
delete [] edges;
delete [] edgesT;
}
return 0;
}

2D (Maximum From Each Column)

After trying a lot of times and thinking again and again.. It feels like a frustrated person.
I'm here to get some suggestions from you guys..
Actually I'm trying to find the maximum of each ROW and COLUMN.
So by using divide and conquer technique, I write two separate functions one for finding max of each row and store it to row vector that i used as an argument. Respectively for columns.
void maxValuesR(int a[][cols], int rv[], int row)
void maxValuesC(int a[][cols], int cv[], int row)
PROBLEM: The code is not even compile, I just don't understnd the errors..
Please Help..
I Really Need Your Help Here!
The code is as follow:
#include <iostream>
using namespace std;
const int rows = 2; // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << "Enter Element: ";
cin >> arr[i][j];
}
}
}
void printData(int a[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
void print_single(int a[], int row)
{
for(int i = 0; i < row; i++)
{
cout << a[i] << "\t";
}
}
void maxValuesR(int a[][cols], int rv[], int row)
{ int foxi;
for(int i = 0; i < row; i++)
{
foxi = a[i][0];
for(int j = 0; j < cols; j++)
{
if(foxi < a[i][j])
{
foxi = a[i][j];
}
}
rv[i] = foxi;
}
}
void maxValuesC(int a[][cols], int cv[], int row)
{
int maxi;
for(int i = 0; i < cols; i++)
{
maxi = a[0][i];
for(int j = 0; j < row; j++)
{
if(maxi > a[j][i])
{
maxi = a[j][[i];// show error here => expected a '{' introducing lambda body
}
}
cv[i] = maxi;
}
}
int main()
{
int rowVector[rows];
int colVector[cols];
int a[rows][cols];
cout << "Fill Array_1. " << endl;
getData(a, rows);
cout << "Array_1." << "\n\n";
printData(a, rows); cout << endl;
maxValuesR(a, rowVector, rows);
print_single(rowVector, rows);
cout << "\n\n";
maxValuesC(a, colVector, rows);
print_single(colVector, rows);
return 0;
}
To clarify the compile errors:
You forgot the include (or didn't write it here?) #include <iostream>
You didn't specify the namespace for cin, cout and endl (they are in std namespace)
You had a superfluous "[" in the statement a[j][[i], by all likelihood you wanted to write a[j][i]
The compiling code looks like this:
#include <iostream>
const int rows = 2; // Declared As Global Variable
const int cols = 3; // Declared As Global Variable
void getData(int arr[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
std::cout << "Enter Element: ";
std::cin >> arr[i][j];
}
}
}
void printData(int a[][cols], int rows)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
std::cout << a[i][j] << "\t";
}
std::cout << std::endl;
}
}
void print_single(int a[], int row)
{
for(int i = 0; i < row; i++)
{
std::cout << a[i] << "\t";
}
}
void maxValuesR(int a[][cols], int rv[], int row)
{ int foxi;
for(int i = 0; i < row; i++)
{
foxi = a[i][0];
for(int j = 0; j < cols; j++)
{
if(foxi < a[i][j])
{
foxi = a[i][j];
}
}
rv[i] = foxi;
}
}
void maxValuesC(int a[][cols], int cv[], int row)
{
int maxi;
for(int i = 0; i < cols; i++)
{
maxi = a[0][i];
for(int j = 0; j < row; j++)
{
if(maxi > a[j][i])
{
maxi = a[j][i];
}
}
cv[i] = maxi;
}
}
int main()
{
int rowVector[rows];
int colVector[cols];
int a[rows][cols];
std::cout << "Fill Array_1. " << std::endl;
getData(a, rows);
std::cout << "Array_1." << "\n\n";
printData(a, rows);
std::cout << std::endl;
maxValuesR(a, rowVector, rows);
print_single(rowVector, rows);
std::cout << "\n\n";
maxValuesC(a, colVector, rows);
print_single(colVector, rows);
return 0;
}
Can't tell whether it produces the output you want, though, because you didn't specify any example input (let alone the corresponding expected output)...

Cannot read functor class in any way

Alright, I'm implementing a dynamic 2-dimensional matrix class. For a basis, this is what I have so far:
template <typename Type>
class dyMatrix {
private:
Type *mat;
int lines, columns;
int length;
void assimilate (const dyMatrix<Type>& that) {
lines = that.lines;
columns = that.columns;
length = that.length;
}
public:
dyMatrix (int height, int width)
: lines(height), columns(width), mat(0)
{
length = lines * columns;
mat = new Type[length];
};
// ---
int getLines() {
return lines;
};
int getColumns() {
return columns;
};
int getLength() {
return length;
}
// ---
Type& operator() (int i, int j) {
return mat[j * columns + i];
};
Type& operator() (int i) {
return mat[i];
};
// ---
dyMatrix (const dyMatrix<Type>& that) {
this->assimilate(that);
// ---
mat = new Type[length];
for (int i = 0; i < length; i++) {
mat[i] = that.mat[i];
}
};
dyMatrix& operator= (const dyMatrix<Type>& that) {
Type *local_mat = new Type[that.length];
for (int i = 0; i < that.length; i++) {
local_mat[i] = that.mat[i];
}
// ---
this->assimilate(that);
delete[] mat;
mat = local_mat;
// ----
return *this;
};
~dyMatrix() {
delete mat;
};
};
My problem is that I've been having some specific problems when trying to read input into it. For example, I have the following main():
int main() {
int lanes, cols;
cin >> lanes >> cols;
dyMatrix<int> map(lanes, cols);
/* CONTINUE READING */
cout << endl;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cout << map(i, j) << ' ';
}
cout << endl;
}
}
Where it is a commented section, if I put the following lines of code:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> map(i, j);
}
}
or:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = aux;
}
}
or even:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
cin >> aux;
map(i, j) = i + j; // !!!
}
}
...It won't work. For some reason, however, the following will:
int aux;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
// cin >> aux;
map(i, j) = i + j; // Still the same thing as before, logically
}
}
I do not understand. What is happening here?
edit: Sorry, forgot to include my debugging procedure.
Input:
3 5
1 2 3 4 5
1 2 3 4 5
The first line has the dimensions of the matrix. The second and third lines are values written into it; After pressing Enter at the end of the third line, however, the program crashes.
It doesn't even work with this input:
3 5
1
2
3
4
5
1
2
3
4
5
It crashes right after the second 5, again.
Your indices appear the wrong way around:
Type& operator() (int i, int j) {
return mat[j * columns + i];
};
Here, i is clearly the column and j is the row. However, in your loops the arguments appear in the opposite order:
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < cols; j++) {
map(i, j) = ...; // row then column
}
}
Also, the destructor should be using delete[] and not delete.