Passing matrix to a function - c++

I am getting an error when calling the 'printMat' function. My requirement is to create a matrix after accepting the number of rows and columns, and then take input into the matrix, then call the printMat by sending the matrix and print the elements. The error is as follows:
error: parameter 'a' includes pointer to array of unknown bo
und 'int []'
#include<iostream>
using namespace std;
int row,col;
void printMat(int* a[])
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<<a[i][j]<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat(mat);
return 0;
}

int* a[]
is an array of pointer, but you are passing a pointer to an array:
int (*a)[]

The reason it doesn't work is that arrays are just a nightmare. In C++, we use vectors instead.
#include<iostream>
#include<vector>
using namespace std;
void printMat(vector<vector<int>> mat)
{
for(vector<int> one_row : mat)
{
for(int one_cell_in_this_row : one_row)
{
cout << one_cell_in_this_row << ' ';
}
cout << endl;
}
}
int main()
{
int row,col;
cin>>row;
cin>>col;
vector< vector<int> > mat( row , vector<int>(col,0) );
// ^ ^
// initialize the vector ~~~~~/ |
// with 'row' items, each |
// of which is a vector |
// of 'col' integers. ~~~~~~~~~~~~~~~~~~~~~~~~~/
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
int current_entry;
cin>>current_entry;
mat.at(i).at(j) = current_entry;
}
}
printMat(mat);
return 0;
}

You can solve using pointer arithmetic. See following code.
#include<iostream>
using namespace std;
int row,col;
void printMat(int *a)
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<< *((a+i*col) + j)<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat((int *)mat);
return 0;
}
Other possible solutions are explained in this link

Related

How can I reverse a each column of a matrix

I want to change the a matrix
A=[[1,2],[3,4]] to A'=[[3,4],[1,2]]
How can I write a C++ program to make this change
Here is my attempt. but it fails
Here I am declaring a global variable g=0
and at each iteration I am trying to reverse each row
#include<bits/stdc++.h>
using namespace std;
int g=0;
void rotaterow(vector<vector<int>>&matrix, int a, int j)
{
int n = matrix.size();
int k=n-1;
for (int i = 0; i < n/2; i++)
{
int s=matrix[a][g];
int b=matrix[k][g];
swap(matrix[a][i], matrix[k--][j]);
}
g++;// taking g as a global variable
//matrix[a][n - 1] = temp;
}
int main()
{
int n;
cin>>n;
vector<vector<int>>matrix(n, vector<int>(n));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>matrix[i][j];
}
}
for(int j=0;j<n;j++)
rotaterow(matrix,0,j);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<matrix[i][j]<<" ";
cout<<endl;
}
}
Since you didn't mention any specific problem with your program, you can use std::swap as shown below:
#include <vector>
#include <iostream>
int main()
{
std::vector<std::vector<int>> vec = {{1,2,3}, {4,5,6}};
std::cout<<"before swap: "<<std::endl;
for(std::vector<std::vector<int>>::size_type row = 0; row < vec.size(); ++row)
{
for(std::vector<int>::size_type col = 0; col < vec.at(row).size(); ++col)
{
std::cout<<vec.at(row).at(col)<<" ";
}
std::cout<<std::endl;
}
//swap the 0th and 1st column
std::swap(vec.at(0), vec.at(1));
std::cout<<"after swap: "<<std::endl;
for(std::vector<std::vector<int>>::size_type row = 0; row < vec.size(); ++row)
{
for(std::vector<int>::size_type col = 0; col < vec.at(row).size(); ++col)
{
std::cout<<vec.at(row).at(col)<<" ";
}
std::cout<<std::endl;
}
}
The output of the above program can be seen here.

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

2D array value are passed like array and 0 do not get printed

I'm working on this code and I don't understand why reading a matrix from the file doesen't output the correct matrix but instead it just shoves all the values != 0 into a single array and doesen't print 0. Thanks.
#include <iostream>
#include <fstream>
#include <array>
using namespace std;
#define M 4
#define N 4
int** inputArray();
void printArray(int** matrix);
int main()
{
int **matrix;
matrix = inputArray();
printArray(matrix);
return 0;
}
void printArray(int** matrix)
{
for (int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
}
int** inputArray()
{
int** matrix=new int*[N];
int value;
ifstream matrice("matrix.txt");
if (matrice.is_open())
{
do
{
for (int i=0; i<N; ++i)
{
matrix[i]=new int[M];
for(int j=0; j<M; j++)
{
matrice >> matrix[i][j];
//cout << matrix[i][j];
}
}
}
while (matrice>>value);
matrice.close();
return matrix;
}
else cout << "Unable to open file";
}
The matrix I tried using is:
3418
0163
0023
0001
The program should print out exactly the matrix I'm reading from the file.
Your input processing way is wrong ! As each element in a row is not separated by space you should take input as string or character.
For example:
#include <iostream>
#include <fstream>
#include <array>
using namespace std;
#define M 4
#define N 4
int** inputArray();
void printArray(int** matrix);
int main()
{
int **matrix;
matrix = inputArray();
printArray(matrix);
return 0;
}
void printArray(int** matrix)
{
for (int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
}
int** inputArray()
{
int** matrix=new int*[N];
int value;
ifstream matrice("matrix.txt");
if (matrice.is_open())
{
for (int i=0; i<N; ++i)
{
string x;
matrice>>x;
matrix[i]=new int[M];
for(int j=0; j<M; j++)
{
matrix[i][j]=x[j]-'0';
//cout << matrix[i][j];
}
}
matrice.close();
return matrix;
}
else cout << "Unable to open file";
}
I used string here. matrice>>x will take input until it get white space or newline.So matrice>>x will input a whole row as string in x. After taking input I iterated through the string and converted each character to number using x[j]-'0' and added each element on its cell.

How to send 2 dimensional Matrix with unknown size to a function

I'm trying to figure out how to pass a 2 dimensional Matrix to a function
//function to print Matrix
void refl(int n, int board[][])
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
int board[n][n]; //creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
refl(n , board);
}
return 0;
}
It says that "n" and "board" isn't declared in the function while they are .
Try to use C++ std::vector or good old C malloc+free.
C++
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void reflxx(int n, vector<vector<int>>& board)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
vector<vector<int>> board(n, vector<int>(n));
//creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
}
reflxx(n , board);
return 0;
}
See http://www.cplusplus.com/reference/vector/vector/resize/ for more example.

2D Vector of Doubles sorting/editing issue

I am working on a 2D vector of doubles matrix solver and my matrices keep turning out incorrectly.
The intended matrix output at "Level 3" is {(top, left to right)3,4 over (bottom, left to right)1,2} but my code keeps outputting {2,1 over 3,4}. I keep trying to invert the values but whenever I do so I keep getting an out of bounds exception.
vector<double> gauss(mat B) {
int n = B.A.size();
for (int i=0; i<n-1; i++) {
// Search for maximum in this column
double maxEl = abs(B.getSlot(i,i));
int maxRow = i;
for (int k=i+1; k<n; k++) {
if (abs(B.getSlot(k,i)) > maxEl) {
maxEl = abs(B.getSlot(k,i));
maxRow = k;
}
}
// Swap maximum row with current row
for (int k=i; k<n+1;k++) {
double tmp = B.getSlot(k,maxRow);
B.editSlot(k,maxRow,B.getSlot(k,i));
B.editSlot(k,i,tmp);
}
cout << "\n\nlevel 3: \n";
B.display();
}
B.display();
// Solve equation Ax=b for an upper triangular matrix A
vector<double> x(n);
for (int i=n-1; i>=0; i--) {
x[i] = B.getSlot(i,n)/B.getSlot(i,i);
for (int k=i-1;k>=0; k--) {
B.editSlot(k,n,B.getSlot(k,n)-B.getSlot(k,i)*x[i]);
}
}
return x;
}
int main() {
int n = *Size of array is properly retrieved*
mat my_mat(n,n);
// Read input data
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
double myDub;
inFile >> myDub;
my_mat.editSlot(i,j,myDub);
}
}
// Calculate solution
vector<double> x(n);
x = gauss(my_mat);
}
And my class code is as follows
class mat
{
public:
mat(int x,int y){
int row = x;
int col = y;
n=row;
A = vector<vector<double>>(row,vector<double>(row,0));
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout<<setw(6)<<A[i][j];
}
cout<<endl;
}
}
void editSlot(int x, int y, double val){A[y][x] = val;}
double getSlot(int x, int y){return A[y][x];}
void display()
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout<<setw(6)<<A[j][i];
}
cout<<endl;
}
cout<<endl;
}
vector<vector<double>> A;
private:
int n;
};
Try to change your input loop:
// Read input data
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double myDub;
inFile >> myDub;
my_mat.editSlot(j, i, myDub); // < - j, i swapped.
}
}
Can you provide sample input that you use and what output you expect?