PDA

View Full Version : I would able to program in c++ I would be able to program in every language.



sana.pari
04-24-2011, 05:28 PM
RESPECTED SIR,your way of teaching is very simple and unique I learnt many things from yours way of teaching

sir i have coded this problem but the result is not good. I want to tranpose the matrix but the

matrix is not transposed please tell me the mistakes and give me the correct code

I would be grateful to you. Sir can you tell me that which language is good for learning

and if I would able to program in c++ I would be able to program in every language.

#include<iostream.h>
#include<conio.h>
const int maxrows=3;
const int maxcols=3;
void readmatrix(int a[][maxcols]);
void displaymatrix(int a[][maxcols]);
void transposematrix(int a[][maxcols]);
main()
{
int a[maxrows][maxcols];
readmatrix(a);
cout<<endl;
cout<<"DISPLAY MATRIX ";
cout<<endl;
displaymatrix(a);
cout<<endl;
cout<<"TRANPOSE THE MATRIX ";
cout<<endl;
displaymatrix(a);
cout<<getch();
}
void readmatrix(int a[maxrows][maxcols])
{
int rows;
int cols;
for (rows=0;rows<maxrows;rows++)
{
for (cols=0;cols<maxcols;cols++)
{
cout<<"PLEASE ENTER THE NUMBER";
cout<<rows;

cout<<cols;
cin>>a[rows][cols];
}
cout<<'\t';
cout<<endl;
}
}

void displaymatrix(int a[][maxcols])
{
int rows;
int cols;
for (rows=0;rows<maxcols;rows++)
{
for (cols=0;cols<maxcols;cols++)
{



cout<<a[rows][cols];
cout<<'\t';
}
cout<<endl;
}
}
void transposematrix(int a[maxrows][maxcols])
{
int rows;
int cols;
int temp;
for (rows=0;rows<maxrows;rows++)
{
for (cols=rows;cols<maxcols;cols++)
{
temp=a[rows][cols];
a[rows][cols]=a[cols][rows];
a[cols][rows]=temp;
}
}
}

Support
04-24-2011, 05:29 PM
Dear Student,

Below is the corrected modified code of your program.

#include<iostream.h>
#include<conio.h>

const int maxrows=3;
const int maxcols=3;

void readmatrix(int a[][maxcols]);
void displaymatrix(int a[][maxcols]);
void transposematrix(int a[][maxcols]);

main()
{
int a[maxrows][maxcols];

readmatrix(a);

cout<<endl;
cout<<"DISPLAY MATRIX:\n\n";


displaymatrix(a);

cout<<"\nTRANPOSE THE MATRIX ";
cout<<endl;

transposematrix(a);

displaymatrix(a);

cout<<getch();
}


void readmatrix(int a[maxrows][maxcols])
{
int rows;
int cols;

for (rows=0;rows<maxrows;rows++)
{
for (cols=0;cols<maxcols;cols++)
{

cout<<"PLEASE ENTER THE NUMBER FOR ROW "<<rows << " AND COLUMN " <<cols;
cout<<":\t";
cin>>a[rows][cols];

}
}

}

void displaymatrix(int a[][maxcols])
{
int rows;
int cols;

for (rows=0;rows<maxcols;rows++)
{
for (cols=0;cols<maxcols;cols++)
{
cout<<a[rows][cols];
cout<<"\t";
}

cout<<endl;
}
}


void transposematrix(int a[maxrows][maxcols])
{
int rows;
int cols;
int temp;


for (rows=0;rows<maxrows;rows++)
{
for (cols=rows;cols<maxcols;cols++)
{
temp=a[rows][cols];
a[rows][cols]=a[cols][rows];
a[cols][rows]=temp;
}
}

// displaymatrix(a);
}


The problem with you program was that you were not calling transpose function in your main function and also there were some issue with your cout statements.