I have to write a program to allow a user to input the number of rows and the number of columns of a matrix. The program will then construct the transpose of the matrix and display the transpose to the user, it is also supposed to display the trace and determinant of the matrix. I am having a problem with the output for the transpose, whenever the matrix is not a perfect square I get random numbers.
This is my code:
int main()
{
int matrix[10][10];
int row, column, i, j;
int temp = 0;
//size of matrix
cout << "Enter number of rows: ";
cin >> row;
cout << "Enter number of columns: ";
cin >> column;
// read the matrix values ( original matrix )
cout << "Enter the elements: \n";
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
cin >> matrix[i][j];
cout << "Matrix: \n";
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
cout << matrix[i][j] << ' ';
cout << endl;
}
// transpose the matrix values ( the matrix transposed )
cout << "Transpose of a Matrix: \n";
for (j = 0; j < row; j++)
{
for (i = 0; i < column; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}
// trace of a matrix
int trace = 0;
if (row == column)
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
trace += matrix[i][i];
cout << "Trace of a Matrix: \n";
cout << trace << endl;
}
else
{
cout << "The trace will not be computed \n"
<< "The given matrix is not a perfect square" << endl;
}
// determinant of Matrix
int det = 0;
if (row == 2 && column == 2)
{
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
for (i = 0; i < row; i++)
cout << "Determinant of the Matrix: \n";
det = ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
cout << det << endl;
}
}
else
{
cout << "The determinant will not be computed \n"
<< "The given matrix is not a square" << endl;
}
return 0;
}
If I were to enter 3 rows and 2 columns and random elements my output looks like this:
Enter number of rows: 3
Enter number of columns : 2
Enter the elements:
1 5 6 -4 2 3
Matrix:
1 5
6 -4
2 3
Transpose of a Matrix:
1 6
5 -4
-858993460 -858993460
The trace will not be computed
The given matrix is not a perfect square
The determinant will not be computed
The given matrix is not a square
Is there something wrong in my code that causes -858993460 and -858993460 to come up?
Try this when printing the transpose (note the upper bounds for the loops have been swapped compared to your code) :
for (j = 0; j < column; j++)
{
for (i = 0; i < row; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}
The reason you got "random elements", is because your code was reading and printing the contents of memory that hadn't been initialized yet.
Eg. in your example output, the first value -858993460 was obtained from matrix[2][0] (with j == 2 and i == 0), but that slot hadn't been filled with anything (the original matrix only occupies 0 <= i <= 2 and 0 <= j <= 1). Similarly, the second value -858993460 was obtained from matrix[2][1].
You are accessing parts of the memory, that have not been written to while reading in the matrix elements.
Instead of changing what i and j represent when printing the matrix, just change the order of the for loops from the order in that you were reading the elements.
cout << "Transpose of a Matrix: \n";
for (j = 0; j < column; j++)
{
for (i = 0; i < row; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}
Related
the problem arises when I use a different number of rows and columns, for example, 2 by 3 otherwise, it is running okay. the sum of column outputs garbage values
I can't seem to understand where the bug is.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int a[10][10];
int i,row,column, j, s = 0, sum = 0;
cout<<"Enter Number of rows: ";
cin>>row;
cout<<"Enter Number of columns: ";
cin>>column;
cout<< "Enter elements Matrix \n";
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
cin >> a[i][j];
cout << "Matrix Entered By you is \n";
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < row; i++)
{
for (j = 0; j <column; j++)
s = s + a[i][j];
cout << "Sum of Row " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
}
You are not iterating correctly to get your columns sum, column and row are switched up. change to:
for (i = 0; i < column; i++) // <-----
{
for (j = 0; j < row; j++) // <-----
s = s + a[j][i];
cout << "Sum of Column " << i + 1 << " is: " << s;
s = 0;
cout << endl;
}
Consider a 3x4 matrix:
1 2 3 4
1 2 3 4
1 2 3 4
Your current loop would access it in the following manner, invoking undefined behavior.
[1] [2] [3] 4
[1] [2] [3] 4
[1] [2] [3] 4
[?] [?] [?]
enter image description here
Hello everyone, I am trying to print a reverse heart shape in c++. While user will input a integer, and the program will display the reverse heart according to the value.
in the photo, the input value are 1,3,4
Now my code can only show the triangle but still missing the two small triangle, so hoping can get some guide here.
int size,space;
cout << "Input size: ";
cin >> size;
int rows = (size * 2)+1;
for (int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
cout << " ";
}
while (k != 2 * i - 1)
{
cout << "* ";
++k;
}
cout << endl;
}
{
for (int i = size; i >= 1; --i)
{
for (int space = 0; space < size - i; ++space)
cout << " ";
for (int j = i; j <= 2 * i - 1; ++j)
cout << "* ";
for (int j = 0; j < i - 1; ++j)
cout << "* ";
cout << endl;
}
}
I try to print a Inverted first, but I fail to print two at the same time.
I have created a 2d array, so lets say as I did in the code below the array elements shows like this:
3 4
8 2
I want to compare the" ROWS" and get the minimum number, like what I do with 3 & 4, I get 3 is the min number and with the second row between 8 & 2, I get 2 is the min number.
My problem is that I can't get it to compare the columns as in 3 & 8 to get the max out between them, and 4 & 2.
In the code it just outputs the max number in the rows it still comparing the "ROWS"
P.S sorry for the long explanation. I just had to clarify it .
int main()
{
int d;
int max;
//array dimention
cout << "Enter the array dimention :- " << endl;
cin >> d;
//max_min for the final maximum numbers
int max_num[d];
//the 2d array
int arr[d][d];
// array input
cout << "please enter the array elements :- " << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cin >> arr[i][j];
}
}
//array output
cout << "the array elements you enterd :-" << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//the max numbers
for (int i = 0; i < d; i++) {
max = arr[0][i];
for (int j = 0; j < d; j++) {
if (max < arr[j][i]) {
max = arr[j][i];
}
}
max_num[i] = max;
}
cout << "the maximum elements in array :-" << endl;
for (int i = 0; i < d; i++) {
cout << max_num[i] << endl;
}
}
Im trying to create a program that creates two 2d dynamic arrays and multiply them and give an output and calculate the time taken for the multiplication process based on the input size n. This code works when it comes to outputs lesser than 7 rows and 7 cols but gives an error when the number goes above 8.
using namespace std;
int m1c , m1r , m2c , m2r , i , j , k , l;
int** arr1 = new int*[m1c];
int** arr2 = new int*[m2c];
int** multArr = new int*[m1c];
int main(){
cout << "Enter the Number of rows for matrix 1 :";
cin >> m1c;
cout << "Enter the Number of columns for matrix 1 :";
cin >> m1r;
cout << "Enter the Number of rows for matrix 2 :";
cin >> m2c;
cout << "Enter the Number of columns for matrix 2 :";
cin >> m2r;
for (i = 0; i < m1r; i++) {
arr1[i] = new int[m1c];
multArr[i] = new int[m1c];
}
for (i = 0; i < m2r; i++) {
arr2[i] = new int[m2c];
}
if (m1r != m2c) {
cout << "Number of rows in the first matrix must be equal to the numbr of columns in the second matrix ";
return -1;
}
for (i = 0; i < m1r; i++) {
for (j = 0; j < m2c; j++) {
arr1[i][j] = rand() % 100;
}
}
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
arr2[i][j] = rand() % 100;
}
}
//Displaying the two arrays
for (i = 0; i < m1r; i++) {
for (j = 0; j < m1c; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
delete[] arr1;
delete[] arr2;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looks like an error initializing arr1. Your using m2c as the column count. You probably meant m1c.
Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}