This question already has answers here:
adding a newline to file in C++
(4 answers)
Most efficient way to output a newline
(7 answers)
Closed 5 months ago.
so I'm starting to write a program that multiplies two square matrices using dynamic 2D arrays. I'm just learning how dynamic arrays work, so I'm testing to make sure everything is storing properly.
When I run my code, it outputs the two matrices on a single line each, rather than like a matrix with rows and columns. How do I fix this?
#include <iomanip>
#include <iostream>
#include <array>
using namespace std;
int main()
{
int **C, n, m; //pointer, rows, columns for matrix 1;
int **D, p, q; //pointer, rows, columns for matrix 2;
cout << "Enter the dimensions of your matrices: ";
cin >> n >> m;
p = n;
q = m;
cout << endl;
C = new int *[n];
D = new int *[p];
for (int x=0 ; x < n; x++)
{
C[x] = new int [m];
}
for (int x=0 ; x < p; x++)
{
D[x] = new int [q];
}
cout << "Enter the values of your first matrix: ";
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cin >> C[I][K];
}
cout << "Enter the values of your second matrix: ";
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cin >> D[L][Z];
}
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
}
cout << endl;
}
Just add one more statement
cout << endl;
in your for loops like
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
cout << endl;
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
cout << endl;
}
cout << endl;
Related
I am writing a matrix class where I need to perform some matrix calculations in the main program. I am not sure why the program is ending abruptly when user chooses a matrix of size more than 2x2 matrix. The std::cin works fine until two rows but program ends after the loop reaches third row. Only part of the code is shown below which is related to my question.
#include<iostream>
#include <vector>
#include <cassert>
using std::vector;
using namespace std;
class Matrix {
private:
int rows;
int cols;
int **vtr;
public:
Matrix(int m = 2, int n = 2)
{
rows = m;
cols = n;
vtr = new int*[m];
for (int i = 0; i < m; i++)
{
vtr[i] = new int [n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
vtr[i][j] = 0;
}
}
}
void read()
{
cout << "Enter the number of rows and columns of Matrix separated by a space: ";
cin >> rows >> cols;
Matrix a(rows, cols);
a.write();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "(" << i << "," << j << ") : ";
cin >>vtr[i][j];
}
}
}
void write()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << vtr[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
};
int main()
{
Matrix A, B, C;
int row, column ;
cout << "For Matrix A" << endl;
A.read();
cout << "For Matrix B " << endl;
B.read();
cout << "For Matrix C" << endl;
C.read();
}
Since the 2D array, vtr is created when declaring the Matrix object, you can move the vtr creation after reading the console input like below.
Matrix(int m = 2, int n = 2)
{
/*rows = m;
cols = n;
vtr = new int*[m];
for (int i = 0; i < m; i++)
{
vtr[i] = new int [n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
vtr[i][j] = 0;
}
}*/
}
void read()
{
cout << "Enter the number of rows and columns of Matrix separated by a space: ";
cin >> rows >> cols;
vtr = new int*[rows];
for (int i = 0; i < rows; i++)
{
vtr[i] = new int [cols];
}
//Matrix a(rows, cols);
//write();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "(" << i << "," << j << ") : ";
cin >>vtr[i][j];
}
}
write(); //Prints the array
}
The three matrixs will be construct when you define Matrix A, B, C. So the matrix size is 2x2. When you call function cin to assign value to some position is not in 2x2 will not work.
I'm writing a program to find the determinant of a matrix n x n, using Laplace expansion.
Briefly, the program creates a two-dimensional array based on a user request. The users choose the size of the two-dimensional array and fills it in themselves. Next comes the computation of the matrix using Laplace.
The problem is that I can't use the resulting array values in the determinant function. I'm completely new to C ++, so any help would be welcome. The code shown below. Thanks
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
void fin(int**, int, int);
void fout(int**, int, int);
int main() {
int **board, n;
double alpha, beta;
cout << "Enter the number of rows and columns: ";
cin >> n;
cout << endl;
board = new int* [n];
for(int row = 0; row < n; row++)
board[row] = new int[n];
fin(board,n,n);
cout << endl;
fout(board,n,n);
cout << endl;
cout << "Determinant of the matrix is " << determinant(board, n);
cout << endl;
return 0;
}
void fin(int **p, int R, int C)
{
for(int row = 0; row < R; row++)
{
cout << "Enter " << C + 1 << " numbers for row number " << R + 1 << ": ";
for(int col = 0; col < C; col++)
cin >> p[row][col];
cout << endl;
}
}
void fout(int **p, int R, int C)
{
for(int row = 0; row < R; row++)
{
for(int col = 0; col < C; col++)
cout << setw(5) << p[row][col];
cout << endl;
}
}
int determinant( int **result, int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((result[0][0] * result[1][1]) - (result[1][0] * result[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = result[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * result[0][x] * determinant( submatrix, n - 1 ));
}
}
return det;
}
I have a problem with my homework that asks me to have the compiler print out a matrix in which all the diagonals are outputted as zero. I also have to pass it to a function. However, I have no idea how to do this..
Here is my code:
#include <iostream>
using namespace std;
int diagonals();
int main()
{
//problem 1
int matrix[3][3];
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3 ; j++)
{
cout << "Row " << i << " column " << j<< ": ";
cin >> matrix[i][j];
}
}
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "\nReverse of the matrix:" << endl;
for (int j = 1; j <= 3; j++)
{
for (int i = 1; i <= 3; i++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}//end of problem 1
//problem 2
cout << "Diagonals changed to 0:\n" << endl;
}
your matrix declaration says int matrix[3][3]; that it has three 1-D array & in each 1-D array you can store three elements. And in C/C++ array index starts from zero.
Problematic statement is for (int i = 1; i <= 3; i++) as you are skipping matrix[0][0] and trying to store into matrix[3][3] which doesn't exist which in turn causes undefined behavior.
So firstly start iterating loop from 0 to number of rows & column respectively.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 ; j++) {
cout << "Row " << i << " column " << j<< ": ";
cin >> matrix[i][j];
}
}
Coming to task you mentioned, print out a matrix in which all the diagonals are outputted as zero. ? write one condition so that if row value & col value are equal then assign it to zero otherwise scan from user. Here is the sample code
int main(void) {
int matrix[3][3] = { 0 }; /* initialize it */
int row = sizeof(matrix)/sizeof(matrix[0]); /* find no of rows */
int col = sizeof(matrix[0])/sizeof(matrix[0][0]);/* find no of columns */
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if( i == j)
matrix[i][j] = 0;/* when i and j are equal means thats diagonal and assign it to zero */
else /* if its not diagonal then scan from user */
std::cin>>matrix[i][j];
}
}
return 0;
}
Secondly, I also have to pass it to a function. for this learn how to pass 2d array to a function. Here is the sample example.
void diagonal(int (*mat)[3],int row, int col) { /* mat is pointer to an array */
std::cout<<"printing matrix "<<std::endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
std::cout<<mat[i][j]<<"\t";
}
std::cout<<std::endl;
}
}
And call diagonal() like below from main() function as
diagonal(matrix,row,col); /* pass matrix, no of rows, no of columns */
Ok so I have this code to have users input numbers into an array but would like to make it into a function. I am new to C++ and am unsure how to go about it. Can anyone give me some hints or tips? I would greatly appreciate it. The code is as follows:
main()
{
int m;
int n;
int i;
int j;
int A[100][100];
m = get_input ();
n = get_input2 ();
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (i = 0 ; i < m ; i++)
for (j = 0 ; j < n ; j++)
cin >> A[i][j];
return;
}
void initialize(int x[][], int m, int n){
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (int i = 0 ; i < m ; i++)
for (int j = 0 ; j < n ; j++)
cin >> x[i][j];
}
main()
{
int A[100][100],m,n;
// m = get_input ();// you must have that method or cin>>m
//n = get_input2 (); //you must have that method or cin>>n
cout<<"Enter no of rows";
cin>>m;
cout<<"Enter no of columns";
cin>>n;
initialize (A,m,n);
return;
}
On the way to understand working of nested for loop i wrote a program that takes a input and display a pyramid upto that input value like this:
1
22
333
4444
It's displaying just the height of pyramid but its not displaying written part in the second for loop.
Here is the code(after modification but required result not yet)
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max;
for (int j = 0 ; j <= max ; j++)
{
cout << j ;
}
cout << endl ;
max++ ;
}
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int num ;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max = i +1; //change 1
for (int j = 0 ; j < max ; j++)
{
cout << max; //change 2
}
cout << endl ;
//max++ ; //change 3
}
system("PAUSE") ;
return 0;
}
You should initialize max to 0.
int max = 0;
Additionally there are two more bugs.
int max ;
should be declared before the for loop for i. (Otherwise max is defined to be 0 always)
In the inner loop print i, not j.
First of all, please try to have a proper structure in your code:
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl;
cin >> num;
for(int i = 0; i < num; i++)
{
int max;
for(int j = 0; j <= max; j++)
{
cout << j;
}
cout << endl;
max++;
}
system("PAUSE");
return 0;
}
And your mistake:
Change int max; to int max = 0;
You cannot add 1 to a non existing value.
As has been stated in other answers, your max counter isn't initialized. Additionally, you don't really need it, as you already have i doing the same task:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
{
cout << i;
}
cout << endl;
}
Unless you actually want to print something like 0 01 012 0123, this is the code you're looking for:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
cout << i;
cout << endl;
}
max is not set to an initial value.
Its declared insides 1st loop and then used in the 2nd loop.