Enter values into a 2d array and print - c++

I am trying to write a C++ program that asks for inputs for a 3 by 3 array and then prints them. I am trying to print it but am not sure why it is just giving me 0 for each val
#include <iostream>
using namespace std;
const int row = 3;
const int col = 3;
void printMatrix(int array[row][col])
{
int i, j;
cout << endl << "Matrix " << endl;
for(i = 0; i < row; i++)
{
cout << endl;
for(j = 0; j < col; j++)
{
cout << array[row][col] << "\t";
}
}
cout << "\n";
}
int main()
{
int i, j, array[row][col];
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << "Enter a value for Row " << i + 1 << " Col " << j + 1 << ": "
cin >> array[i][j];
}
}
printMatrix(array);
}
Console Output:
Enter a value for Row 1 Col 1: 1
Enter a value for Row 1 Col 2: 2
Enter a value for Row 1 Col 3: 3
Enter a value for Row 2 Col 1: 1
Enter a value for Row 2 Col 2: 2
Enter a value for Row 2 Col 3: 3
Enter a value for Row 3 Col 1: 1
Enter a value for Row 3 Col 2: 2
Enter a value for Row 3 Col 3: 3
Output Matrix:
0 0 0
0 0 0
0 0 0

the problem is here
cout << array[row][col] << "\t";
try
cout << array[i][j] << "\t";

You just need to change the printMatrix() function.
Change this function as below:
cout << array[i][j] << "\t";
And put semicolon (;) at the end of the cout statement in the main function.

Related

C++ matrix problem : incorrect output for transpose matrix

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;
}

Sum of rows and column in a Matrix

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
[?] [?] [?]

Output a triangle of numbers in C++

My task is to ask the user for an int, then output a "number triangle" like the one below (in this case, the int is equal to 5).
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
However, the code which I have written for this task outputs this:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
For reference, here is my code:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Size: " << std::endl;
cin >> size;
for(int i = size; i >= 0; i--)
{
for(int j = 0; j <= i; j++)
{
if (j < i){
cout << j << " ";
}
else{
cout << j << " ";}
}
cout << endl;
}
return 0;
}
Can somebody tell me what to change in my program to make it output the correct triangle? Thanks in advance.
You should print the spaces at the beginning of the line instead of at the end.
for(int i = size; i >= 0; --i){
for(int j = 0; j < size-i; ++j){cout << " ";} // spaces at the beginning
for(int j = 0; j <= i; ++j){
cout << j << " ";
}
cout << endl;
}

I want to display all entries of 2D array in c++

I want to create a program that take size of rows and columns of 2D array from user, and then take all entries of array from user. And Finally display all the entries from array[0][0] to array[size][size].
My code is:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int tom[rows][columns];
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}
Output is:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
12
13
14
12
13
14
It should give output:
1
2
3
12
13
14
What is the problem?
Please help.
You can't dynamically create an array like this. This shouldn't even compile. And even if it did, you are creating the array before letting the user input the dimension. For the proper Approach use std::vector:
#include <iostream>
#include <vector>
int main()
{
int rows, columns;
std::cout << "Size of array rows: ";
std::cin >> rows;
std::cout << "Size of array columns: ";
std::cin >> columns;
std::vector<std::vector<int>> tom(rows, std::vector<int>(columns));
for (int count1 = 0; count1 < rows; count1++)
{
for (int count2 = 0; count2 < columns; count2++)
{
std::cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
std::cin >> tom[count1][count2];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
std::cout << tom[i][j] << std::endl;
}
}
return 0;
}
output:
Size of array rows: 2
Size of array columns: 3
Enter entry of row 0 and column 0: 1
Enter entry of row 0 and column 1: 2
Enter entry of row 0 and column 2: 3
Enter entry of row 1 and column 0: 12
Enter entry of row 1 and column 1: 13
Enter entry of row 1 and column 2: 14
1
2
3
12
13
14
please don't use using namespace std; - read here why.
You initialized tom before actually getting the number of rows/columns.
#include <iostream>
int main()
{
int rows, columns;
std::cout << "Rows: ";
std::cin >> rows;
std::cout << "Columns: ";
std::cin >> columns;
int arr[rows][columns];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << "Enter the value for [" << i << "][" << j << "] : ";
std::cin >> arr[i][j];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You cannot declare an array like this:
int tom[rows][columns];
since values of rows and columns are not known yet.
Instead you should initialize this array dynamically after asking for values.
Here is your code fixed:
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
int **tom;
cout << "Size of array rows: ";
cin >> rows;
cout << "Size of array columns: ";
cin >> columns;
tom = new int*[rows];
for (int i = 0; i < rows; i++) {
tom[i] = new int[columns];
}
for(int count1 = 0; count1 < rows; count1++)
{
for(int count2 = 0; count2 < columns; count2++)
{
cout << "Enter entry of row " << count1 << " and column " << count2 << ": ";
cin >> tom[count1][count2];
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
cout << tom[i][j] << endl;
}
}
return 0;
}

Need 3 separate outputs

#include <iostream>
using namespace std;
int main (){
int row,
column = 0,
colCount = 3,
rowCount = 3;
//for loop
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
cout << column << " " << row;
if(row < (rowCount - 1)){
cout << ", ";
}
}
cout << endl;
}
//while loop
while(column < colCount){
while(row < rowCount){
cout << column << " "<< row;
if(row < (rowCount - 1)){
cout << ", ";
}
row++;
}
cout << endl;
column += 1;
row = 0;
}
//do while loop
do{
do{
cout << column << " "<< row;
if(row < (rowCount - 1)){
cout << ", ";
}
row++;
}while(row < rowCount);
cout << endl;
column +=1;
row = 0;
}while(column < colCount);
}
When commenting out 2/3 loops, each one will produce the wanted output.
All together it seems to run on top of each other and adds extra output.
Current output:
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
3 3
Wanted output:
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
How do I get an output out of each loop?
You can leave the for loop as it is:
for (; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
std::cout << column << " " << row;
if(row < (rowCount - 1))
std::cout << ", ";
}
std::cout << std::endl;
}
Now column and row gets to 3 in the for-loop above, which makes it the while loop never going to be executed. So, you need to make both of them to 0.
And, the third do-while loop always executes before any condition checks, thats why you get 3 3
Anyways, here is a solution for your problem.
#include <iostream>
int main ()
{
int row, column = 0, colCount = 3, rowCount = 3;
//for loop
for (column; column < colCount; column++){
for(row = 0; row <= (rowCount - 1); row++){
std::cout << column << " " << row;
if(row < (rowCount - 1))
std::cout << ", ";
}
std::cout << std::endl;
}
std::cout<<std::endl;
//while loop
column = 0;
row = 0;
while(column < rowCount){
while(row < rowCount){
std::cout << column << " "<< row;
if(row < (rowCount - 1))
std::cout << ", ";
row++;
}
std::cout << std::endl;
column += 1;
row = 0;
}
//do while loop
std::cout<<std::endl;
column = 0;
row = 0;
do{
do{
std::cout << column << " "<< row;
if(row < (rowCount - 1))
std::cout << ", ";
row++;
}while(row < rowCount);
std::cout << std::endl;
column +=1;
row = 0;
}while(column < colCount);
return 0;
}
1) You should initialize the column at the beginning of each loop to 0.
2) Within the while and do loops, initialize the row to 0 before entering the inner-loop
The output 3 3 at the end is due to a single entrance in the do-while loop.