+ 14
Why is this showing 0?
https://code.sololearn.com/czOjzX0LK14Y/?ref=app Why this showing 2 3 1 8 9 1 0 0 0 I can't got that why it this showing 0 0 0 Also if I put 5 or 6 on the place of 3 then it showing very weird output
25 Respostas
+ 23
Coder
Coder matrix is maded by matrix[n][m] transition where n is rows count and m is column count. In your question as you have declared 3 by 3 matrix and computing is perform only two rows as you have written only two rows and 3 columns.
Why 0 is coming over 3 row all column as you have not used matrix 3rd row and that 3rd row is empty so in C++ empty thing return witj 0s. If you change the size of matrix to matrix[4][4] with same for loop than you will see 3rd and 4th rows filled with 0s
So read only till that value which you have declared.
and you delcared additionl rows than the matrix size like [3][3] matrix with for loop till 6 or 5 rows and columns than due to reading the memory which is not declared in the matrix rows and colum it will return you some garbage values.
#include <iostream>
using namespace std;
int main()
{
int x[3][3]= {{2, 3, 4}, {8, 9, 1}};
for(int i =0;i<2;i++){
for(int j=0;j<3;j++){
cout << x[i][j]<<" ";
}
cout <<"\n";
}
return 0;
}
+ 19
Coder you see, it's giving you zeros and garbage values.
It won't give you error but print zero or garbage value.
I am not sure how that works but this is pretty much basic idea.
+ 17
Coder You have 2 rows and 3 colone you should have in value of i=2 not 3
+ 14
Theodora Amponfi you should learn and challenged every day
+ 13
🔫 Rick Grimes bhaiaya ok then
#include <iostream>
using namespace std;
int main()
{
int x[3][3]= {{2, 3, 4}, {8, 9, 1}};
for(int i =0;i<6;i++){
for(int j=0;j<6;j++){
cout << x[i][j]<<" ";
}
cout <<"\n";
}
return 0;
}
Why I got a very weird output?
if I just replace 6 with 3
it not giving any eror why?
+ 11
Because those values don't exist.
It's depending on compiler that either it will show garbage value or 0 if you try to print something that is not initialized.
+ 11
🔫 Rick Grimes bhaiaya thanks it is just a garbage value 😁
Thank for the clarification Bhiaya 😁
+ 7
yeah Rico Edi Nabawi is right ...
you must understand about memory and allocation/initialization for getting it .
+ 7
Array size is declared so memory is allocated and no values are initialized to the x[2][0], x[2][1], and x[2][2] hence it's value is 0.
+ 7
You declared 3 arrays consisting of 3 values.But you defined only two arrays with 3 values in each.Using the for loop you tried to iterate over the third array.Actually there is no third array as you don't defined.But when you declare an array without defining it leads to store some zeros in the undefined declared array.So when you tried to literate over the undefined third array,it outputs the zeros that stored in it.
+ 4
Yeah because you has array with size of 3*3 so your 3rd column and rows are zero by default
But when you put value more than 5 you will get garbage value because you didn't provide size more than 3
+ 3
Theodora Amponfi do not use others questions to chat in comment about something not related to question,if you have a specific question search for similar ones or post a new one in Q&A ,
+ 3
Raphael Nworie your comment is not relevant to the discussion and is considered spam. Please remove.
Thanks and happy coding
+ 3
Himanshu Khatri your comments are irrelevant to the discussion and considered spam, please remove.
Thanks and happy coding.
+ 3
You have just 2 row and 3 columns not 3 row change 3 to 2 and it will work
+ 2
Pls am in level 3 and i don't know how to continue
+ 1
Coder you initial 2D array in 3 row and 3 column but you declare 2 row and 3 column, this is not problem. The problem is output formats. In output format you have to declare 2 row 3 column then it will make correct output... More details in google or sololearn...
You cant change for loop value before you change array initial value and input more value...
You have to understand..
Inputs==Outputs
Input 3==Output 3
Input 5==Output 1,2,3,4,5
Input 4!=Output 5 and more
I think you understand...
example
#include<iostearm>
using namespace std;
int main(){
int x[3][3]={{1,2,3},{3,2,1}}
//fast for loop for row.
for(int i=0;i<2;i++){
//2nd for loop for column.
for(int j=0;i<3;j++){
cout<<x[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
+ 1
You jst need to do one thing i.e you have to add a new row in the array
That was showing you 0 0 0 jst because you haven't given the third row
Kindly check the code below:-
#include <iostream>
using namespace std;
int main()
{
int x[3][3]= {{2, 3, 4}, {8, 9, 1},{5,6,7}};
for(int i =0;i<3;i++){
for(int j=0;j<3;j++){
cout << x[i][j]<<" ";
}
cout <<"\n";
}
return 0;
}
+ 1
Coder in simpler way when we creatr a array and initialise in starting with some numbere and some number remain blank then those number goes to array and blank position write 0 on array as we some example;
If we create an array
Int a[3][3];
Then retrieve data then value is get garbage value
If we initialise
1. Int a[3][3]={{1,2,3},{456},};
this will see third row is set all zero see another example
in this
2.Int a[3][3]={};//initialise array with 0
When retrieve data all value is set 0
But why because of empty space or block is like a null or nothing and nothing and null is represent 0 in computer or mathematics
I hope u get the point
+ 1
alshami ali
تعليقك غير ذي صلة بالمناقشة. بريد مؤذي. يرجى الإزالة.