78Views4Replies

Author Options:

How exactly did this happen in this c++ output window? Answered

Picture of

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

4 Replies

user
frollard (author)2011-11-27

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?

Select as Best AnswerUndo Best Answer

user
ankur2893 (author)frollard2011-11-28

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!

Select as Best AnswerUndo Best Answer

user
frollard (author)ankur28932011-11-28

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?

Select as Best AnswerUndo Best Answer

user
ankur2893 (author)frollard2011-11-28

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?

Select as Best AnswerUndo Best Answer