I want to display a matrix using class and methods. Im getting "C:\Users\PC\AppData\Local\Temp\cc41EMS5.o:lab6.cpp:(.text+0x3d6): undefined reference to `Matrix::Matrix()'
collect2.exe: error: ld returned 1 exit status" error. I guess its because of printMatrix method(that worked not as a method in main), but I dont know how to fix that.
Here is the code:
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Matrix {
public:
Matrix();
Matrix(int rowSize, int colSize);
void setSize(int rowSize, int colSize);
string printMatrix(int rowSize, int colSize);
void free_data();
void allocate_data();
private:
int rowSize;
int colSize;
double **p;
//vector<vector<int>> matrix(rowSize, vector<int>(colSize));
};
void Matrix::allocate_data() {
p = new double *[rowSize];
for (int i = 0; i < rowSize; i++) {
p[i] = new double[colSize];
}
}
void Matrix::free_data() {
for (int i = 0; i < rowSize; i++) {
delete[] p[i];
}
delete[] p;
}
void Matrix::setSize(int rowSize, int colSize) {
int newSize = 0;
p[rowSize][colSize] = newSize;
}
Matrix::Matrix(int rowSize, int colSize) {
this->rowSize = rowSize;
this->colSize = colSize;
allocate_data();
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
p[i][j] = 0;
}
}
}
string Matrix::printMatrix(int rowSize, int colSize) {
int i, j;
int matrix[i][j];
for (int i = 0; i < rowSize; i++) //it worked in main, but doesn't in class
{
for (int j = 0; j < colSize; j++) {
0 >> matrix[i][j];
}
cout << endl;
}
cout << "Matrix" << endl;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
cout << matrix[i][j] << " ";
}
cout << "\n";
}
cout << endl;
}
int main() {
int rowSize;
int colSize;
cout << "Enter size of rows and columns: ";
cout << "Rows: ";
cin >> rowSize;
cout << "Cols: ";
cin >> colSize;
Matrix m;
m.printMatrix(rowSize, colSize);
return 0;
}
Please send help
You declare two constructors:
Matrix();
Matrix(int rowSize, int colSize);
But you define only one:
Matrix::Matrix(int rowSize, int colSize)
{
this->rowSize=rowSize;
this->colSize=colSize;
allocate_data();
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<colSize; j++)
{
p[i][j]=0;
}
}
}
Furthermore, in `main` you call the constructor that you happened to not define:
> ```
> Matrix m;
> ```
Instead, get rid of the declaration for `Matrix::Matrix()` since you lack a definition, and use the constructor that takes two arguments:
int main()
{
int rowSize;
int colSize;
cout<<"Enter size of rows and columns: ";
cout<<"Rows: ";
cin>>rowSize;
cout<<"Cols: ";
cin>>colSize;
Matrix m(rowSize, colSize);
m.printMatrix(rowSize,colSize);
return 0;
}
## Additional improvements
You don't need to pass the size of the matrix to `Matrix::printMatrix`, since the matrix already has the size as members. By passing those parameters to `printMatrix`, you're making the class more confusing and error-prone to use. If I constructed a 3x3 matrix and tried to call `printMatrix(10,10)` on it, I would certainly invoke undefined behavior and likely crash the program.
Get rid of the parameters (in both definition and declaration) and use the existing `rowSize` and `columnSize` fields to control the loops in that function. The return type also ought to be `void`.
Also, `0 >> matrix[i][j];` makes no sense. The correct syntax is `matrix[i][j] = 0;`.
You define and use a parameterless Matrix constructor (Matrix();) but you do not implement it.
You may either:
Delete the Matrix() definition and use the constructor that you define (Matrix m(rowSize, colSize);)
Implement the parameterless constructor, creating the matrix with some predefined values. You can use a delegating constructor to call the constructor that accepts parameters
Matrix::Matrix() : Matrix(4,4) {
}
I would recommend the first option
Related
How can I take the array size input from the user and pass it to the function. I tried #define inside the function, it doesn't work since the array definition needs the array bound at compile time. I tried global variable too, it says to define a integer constant which is not feasible in my case since I want to get the size from the user. How can I solve this issue?
#include <iostream>
using namespace std;
// reverse the transposed matrix as step 2
void reverseColumns(int arr[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N / 2; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[i][N - j - 1];
arr[i][N - j - 1] = temp;
}
}
}
// take the transpose of matrix as step 1
void transposeMatrix(int arr[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = i; j < N; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
void rotateMatrix(int mat[N][N])
{
transposeMatrix(mat);
reverseColumns(mat);
}
// printing the final result
void displayMatrix(int mat[N][N])
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
cout << mat[i][j] << "\t";
cout << "\n";
}
cout << "\n";
}
int main()
{
int T, N;
cin >> T;
while (T > 0)
{
cin >> N;
int mat[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> mat[i][j];
}
}
int res[N][N];
rotateMatrix(mat);
displayMatrix(mat);
}
return 0;
}
One way to make it work is get the input of rows and cols from user and make a one dimensional array dynamically.
for example:
Let ROWS and COLS be the values you got via cin.
Then the array can be declared as
int* arr = new int[ROWS * COLS];
Instead of writing arr[i][j] you have to write
arr[i * COLS + j]
Also you have to delete the array using
delete[] arr;
You are using C++ so you should take advantages of it. As kiner_shah commented the fast way to fix your code is just by use of std::vector<std::vector<int>>.
This is better solution but stil poor:
#include <iostream>
#include <vector>
using namespace std;
using Matrix = std::vector<std::vector<int>>;
Matrix makeSquereMatrix(size_t N)
{
return {N, std::vector<int>(N)};
}
// reverse the transposed matrix as step 2
void reverseColumns(Matrix& arr)
{
auto N = arr.size();
... // no changes here
}
// take the transpose of matrix as step 1
void transposeMatrix(Matrix& arr)
{
auto N = arr.size();
... // no changes here
}
void rotateMatrix(Matrix& mat)
{
transposeMatrix(mat);
reverseColumns(mat);
}
// printing the final result
void displayMatrix(const Matrix& mat)
{
for (auto& row : mat)
{
for (auto x : row)
cout << x << "\t";
cout << "\n";
}
cout << "\n";
}
void readMatrix(Matrix& m)
{
for (auto& row : m)
{
for (auto& x : row)
{
cin >> x;
}
}
}
int main()
{
int T, N;
cin >> T;
while (T > 0)
{
cin >> N;
auto mat = makeSquereMatrix(N);
readMatrix(mat);
rotateMatrix(mat);
displayMatrix(mat);
--T;
}
return 0;
}
Live demo
Better solution would be introducing a class containing std:::vector with methods performing required actions.
BTW some time ago I've made some matrix code for C. Here is live demo
I was making a cpp program in which it takes two input from the user which determine the size of the 2d array and pass the values to the mat class constructor and dynamically create an array of the user's defined size. But, I don't know why it is not working and showing segmentation fault
#include<iostream>
using namespace std;
class mat{
int **a;
int r, c;
public:
mat(int row, int col){
r = row;
c = col;
for(int i = 0; i < r; i++){
*a = new int[r];
*a++;
}
}
void input(){
for(int i = 0; i < r; i++){
for(int j = 0; i < c; j++){
cin >> a[i][j];
}
}
}
void display(){
for(int i = 0; i < r; i++){
for(int j = 0; i < c; j++){
cout << a[i][j] << "\t";
}
cout << endl;
}
}
};
int main()
{
int r, c;
cout << "enter row :";
cin >> r;
cout << "enter column :";
cin >> c;
mat m(r, c);
m.input();
cout << "array \n";
m.display();
}
I can feel that the issue is with the for loop in the constructor or maybe I am doing it wrong.
The class contains several errors.
The variable a is never initialized. When we try to address the memory pointed to by a we get a segmentation fault. We can initialize it like this a = new int*[r]
We should not change where a point's to, so don't use a++. Otherwise a[i][j] will not refer to the i'th row and the j'th column. We would also want to release the memory at some point.
The inner loop for the columns for(int j = 0; i < c; j++) once entered will never terminate and will eventually produce a segmentation fault. We need to change i < c to j < c.
If we fix these errors, it looks like this:
class mat {
int** a;
int r, c;
public:
mat(int row, int col) {
r = row;
c = col;
a = new int*[r];
for (int i = 0; i < r; i++) {
a[i] = new int[c];
}
}
void input() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> a[i][j];
}
}
}
void display() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
}
};
I wrote simple program to calculate some Matrix, and stuck at this problem. I can't get out my new Matrix.
This is my Matrix.h
#pragma once
using namespace std;
class Matrix
{
private:
int row, col;
float **matrix;
public:
Matrix(int); // square matrix
Matrix(int, int); // matrix with different rows and columns
~Matrix(); //delete
void set(int, int, double); // set value to matrix
double get(int, int); // get value from matrix
void print(); // display matrix
int rows(); // show rows
int cols(); //show columns
Matrix operator*(Matrix); // multiply matrix
};
#include <iostream>
#include <fstream>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(int row) {
if (row <= 0) {
cout << "To small value for ROW or COL";
exit(0);
}
this->row = row;
this->col = row;
this->matrix = new float* [row];
for (int i = 0; i < row; i++)
{
this->matrix[i] = new float[row];
for (int j = 0; j < row; j++)
{
this->matrix[i][j] = 0;
}
}
}
Matrix::Matrix(int row, int col) {
if (row <= 0 || col <= 0) {
cout << "To small value for ROW or COL";
exit(0);
}
this->row = row;
this->col = col;
this->matrix = new float* [row];
for (int i = 0; i < row; i++)
{
this->matrix[i] = new float[col];
for (int j = 0; j < col; j++)
{
this->matrix[i][j] = 0;
}
}
}
Matrix::~Matrix() {
for (int i = 0; i < this->row; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
int Matrix::rows() {
return this->row;
}
int Matrix::cols() {
return this->col;
}
void Matrix::set(int row, int col, double val) {
if (row > this->row || col > this->col || row < 0 || col < 0) {
cout << "There is no value to set.";
exit(0);
}
else {
this->matrix[row - 1][col - 1] = val;
}
}
double Matrix::get(int row, int col) {
if (row > this->row || col > this->col || row < 0 || col < 0) {
cout << "There is no value, please correct ROW or COL.";
exit(0);
}
else {
cout << "Taken value from row " << row << " and col " << col << " = ";
return this->matrix[row - 1][col - 1];
}
}
void Matrix::print() {
for (int i = 0; i < this->row; i++)
{
for (int j = 0; j < this->col; j++)
{
cout << this->matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
Matrix Matrix::operator*(Matrix B) {
Matrix multiplied(B.row, this->col);
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < B.col; j++) {
multiplied.matrix[i][j] = 0;
for (int k = 0; k < this->col; k++) {
multiplied.matrix[i][j] += this->matrix[i][k] * B.matrix[k][j];
}
}
}
multiplied.print(); // this work, and show correct answer in console
return multiplied;
}
#include <iostream>
#include <fstream>
#include "Matrix.h"
using namespace std;
int main()
{
Matrix one(8,7), two(8,7), three(1,1), four(5);
one.set(1, 1, 5);
cout << one.get(1,5) << endl;
one.print();
two.set(1,2,2);
two.print();
Matrix nine = two * one; // but two*one from Matrix.cpp gives me correct Matrix
nine.print(); // this stop the program
}
I'm getting something like this:
makefile:4: recipe for target 'run' failed
make: *** [run] Segmentation fault (core dumped)
I'm using makefile to run this code. Where is the problem?
A comment on your question pinpoints the reason why things are failing - You did not define a copy constructor, so the compiler implicitly defines one for you. The implicit copy constructor copies your matrix pointer into a new Matrix, and then it gets deleted both inside operator*() call and outside where the new matrix is returned. You can fix it by defining copy and move constructors, but I think there's something much better.
First of all, if you find yourself using new and delete manually in your code, it's usually wrong in modern C++. Secondly, you don't need to allocate new arrays for each row - they are all going to be the same size, and you can do one single allocation.
#pragma once
#include <vector>
// never use namespace aliases in header files - this polutes the global namespace of every user.
class Matrix
{
private:
size_t rows; // unsigned integers prevent values less than 0
size_t cols;
std::vector<double> elements;
public:
Matrix(size_t);
Matrix(size_t, size_t);
// destructor no longer needed! vector handles it
void set(size_t, size_t, double);
double get(size_t, size_t) const; // follow const correctness
void print();
size_t rows();
size_t cols();
Matrix operator*(Matrix);
};
Some implementations:
// This is the whole constructor. Vector will zero-initialize all the values
Matrix::Matrix(size_t row, size_t col)
: rows(row)
, cols(col)
, elements(row * col)
{ }
void Matrix::set(size_t row, size_t col, double val) {
elements[row * cols + col] = val;
}
double Matrix::get(size_t row, size_t col) const {
return elements[row * cols + col];
}
I am completely new in C++ and I have to solve a task for college, where I have to make a struct Matrix and fill it with random integers. I marked the line with a "!" where the error appears.
It is the error C2131(Visual C++ Compiler). It says "expression did not evaluate to a constant".
struct Matrix{
int rows;
int columns;
Matrix(int r, int c){
rows = r, columns = c;
}
int produceMatrix(){
int matrix[rows][columns]; "!"
for(int i = 0; i != rows; i++){
for(int j = 0; j != columns; j++){
matrix[i][j] = rand() %10 +1;
}
}
return 0;
}
int showMatrix(){
for(int i = 0; i != rows; i++){
for(int j = 0; j != columns; j++){
cout << matrix[i][j]<< endl;
}
}
}
};
int main()
{
srand(time(0));
Matrix a(3, 4);
}
If you are planning to create your matrix with rows and columns values only known at runtime, you are better off using std::vector<std::vector<int>> to hold your data, as the static array you use needs to know its size at compile time. But if all your sizes are known at compile time and you just want flexibility of creating different matrix sizes, you can use template, for example:
#include <iostream>
#include <ctime>
template <int ROWS, int COLUMNS>
struct Matrix
{
int rows = ROWS;
int columns = COLUMNS;
int matrix[ROWS][COLUMNS] = {};
void produceMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
matrix[i][j] = rand() % 10 + 1;
}
}
}
void showMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
std::cout << matrix[i][j] << "\t";
}
std::cout << std::endl;
}
}
};
int main()
{
srand(time(0));
Matrix<3,4> a;
a.produceMatrix();
a.showMatrix();
}
https://ideone.com/rCLxSn
4 10 5 5
3 8 3 6
2 4 9 10
One thing is that you cannot make variable-length arrays this way.
Another thing is that if you create a variable within a function (like you were doing here with int matrix in produceMatrix()), it is then not visible in another function.
Therefore, the array containing the elements of the matrix should be declared in your struct there, where you have declared rows and columns.
To store the elements of your matrix, you can use one-dimensional array of length equal to rows*columns.
Now, you need some kind of dynamic array to be able to make it of the length not known in the compilation time.
One solution is to use a pointer and define an array with new operator in the constructor. However, if you use new, then you have to use delete at some point to deallocate memory, which here means that the destructor is needed. Another problem would be with copying of your matrices.
Another, simpler solution is to use std::vector, a container provided by c++ standard library. Here's how to do it with std::vector (you need to add #include<vector> to your file):
struct Matrix{
int rows;
int columns;
vector<int> matrix;
Matrix(int r, int c){
rows = r, columns = c;
matrix = vector<int>(c*r);
}
int produceMatrix(){
for(int i = 0; i < matrix.size(); i++){
matrix[i] = rand() %10 +1;
}
return 0;
}
int showMatrix(){
for(int i = 1; i <= matrix.size(); i++){
cout << matrix[i-1];
if(i%columns == 0) cout << endl;
else cout << " ";
}
return 0;
}
};
As many people commented, please go through a good C++ book to learn about arrays, classes, structs etc. As for your code, the following might produce what I think you want:
#include <iostream>
#include <vector>
struct Matrix
{
int rows;
int columns;
std::vector<std::vector<int>> matrix;
Matrix(int r, int c): rows(r), columns(c)
{
matrix.resize(r);
for(int i = 0; i < r; i++)
matrix[i].resize(c);
}
int produceMatrix()
{
for(int i = 0; i != rows; i++)
for(int j = 0; j != columns; j++)
matrix[i][j] = rand() %10 +1;
return 0;
}
int showMatrix()
{
for(int i = 0; i != rows; i++)
{
for(int j = 0; j != columns; j++)
std::cout << matrix[i][j]<<" ";
}
std::cout<<'\n';
}
};
int main()
{
srand(time(0));
Matrix a(3, 4);
a.produceMatrix();
a.showMatrix();
}
Hello guys I need to know what should I pass to qsort function to make this work?
Everything else must stay as it is except arguments of qsort function.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
}
void initMatrix(int **matrix, int n){
for(int i = 0; i<n; i++){
for(int j =0 ; j < n; j++){
matrix[i][j] = rand()%10;
}
}
}
int compar(const void *a, const void *b){
int ia = *((int*)a);
int ib = *((int*)b);
return ia-ib;
}
void deleteMatrix(int **matrix){
delete [] matrix;
}
int main()
{
cout<<"How many rows and cols?"<<endl;
int n;
cin>>n;
int **matrix;
matrix = new int* [n];
for(int j = 0; j < n; j++)
matrix[j] = new int[n];
initMatrix(matrix,n);
printMatrix(matrix,n);
cout<<"Sorted matrix:"<<endl;
qsort(matrix,n*n,sizeof(int),compar); //<-----------
printMatrix(matrix,n);
deleteMatrix(matrix);
return 0;
}
P.S
There must be ** pointer on matrix.
Thanks in advance!
You would probably be better off flattening the matrix to 1 dimension and using a step variable to determine where each row would start. Then sorting is easy.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void printMatrix(int *matrix, int matrixsize, int rowsize) {
for (int i = 0; i < matrixsize; i+= rowsize)
{
for (int j = 0; j < rowsize; j++)
{
cout << matrix[i + j] << " ";
}
cout << endl;
}
}
void initMatrix(int *matrix, int n)
{
for (int i = 0; i < n; i++)
{
matrix[i] = rand() % 10;
}
}
void deleteMatrix(int *matrix) {
delete[] matrix;
}
int main()
{
cout << "How many rows and cols?" << endl;
int row;
cin >> row;
int matrixSize = row*row;
int *matrix = new int[matrixSize];
initMatrix(matrix, matrixSize);
printMatrix(matrix, matrixSize, row);
cout << "Sorted matrix:" << endl;
sort(matrix,matrix + matrixSize); //<-----------
printMatrix(matrix, matrixSize, row);
deleteMatrix(matrix);
return 0;
}