How exactly did this happen in this c++ output window?
Well this is a pretty easy program i made while practicing, by mistake i entered "a", as the first element of my matrix and the whole program displayed results(i wasn't able to add other elements it directly displayed the results")
The code which i used:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void read(int a[][10],int r,int c)
{
for(int i=0;i<r; i++)
{
for(int j=0;j<c;j++)
{
cin>>a[i][j];
}
}
}
void display(int a[][10], int x, int y)
{
cout<<endl;
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
cout<<setw(5)<<a[i][j]<<" ";
cout<<endl;
}
}
void displayt(int a[][10],int r,int c)
{
for(int i=0;i<c; i++)
{
for(int j=0;j<r;j++)
cout<<setw(5)<<a[j][i]<<" ";
cout<<endl;
}
}
int main()
{
int m,n;
int a[10][10],b[10][10];
cout<<"Enter the dimensions\n";
cin>>m>>n;
cout<<"Enter the first matrix\n";
read(a,m,n);
cout<<"entered matrix:\n";
display(a,m,n);
cout<<"Transpose:\n";
displayt(a,m,n);
getch();
return 0;
}
Comments
9 years ago
because a was already defined, when you fed it 'a' it might have referenced the values of the existing a to put into a, since it was a valid data type...not sure but what happens if you enter z or x or h?
Answer 9 years ago
no no man. a being a variable and a being given as a character type input are two different things, it has no reation what so ever. as far as entering z x or h is concerned, it gives the 4, 0 and 127 as constant input in their respective places rest of the values change!
Answer 9 years ago
just thought it might be some goofy byref problem, I know I've had programs accidentally grab the wrong chunk of memory before. perhaps a variable overflow?
Answer 9 years ago
the 'wrong chunks' are called garbage values, they are random values initially stored in a variable when it is declared. thus u need to initialize the variable. that i understood. what i didnt understand is why that 4, 0, 127 remain constant and why does the compiler prints the results even if the datatype i've entered is incorrect?