How to give matrix input from keyboard or file in python

Pramod pramo4d at gmail.com
Wed Aug 11 21:27:32 CEST 2010






I want to write the following programm in python .


Main objective is input from keyoard to enter elements into the
matrix .


Thanks in advance



#include<iostream>
#include<math.h>
#include<cstdlib>
using namespace std;
int main()
{
	double **a;
	int i,j,n;
	cout<<"Enter size of the matrix\n";
	cin>>n;
	a=new double* [n];
	for(i=0;i<n;i++)
		a[i]=new double[n+1];
	for(i=0;i<n;i++){
		for(j=0;j<n;j++)
			cin>>a[i][j];
		cout<<endl;
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++)
			cout<<a[i][j]<<"\t";
		cout<<endl;
	}
	delete [] a;
}



thanku Chris for your program. after  running yourprogram following
errors are coming ,can expalined in detailes please .

Enter size of the matrix: 2
Traceback (most recent call last):
  File "two.py", line 16, in <module>
    stringified = "\n".join("\t".join(row) for row in a)
  File "two.py", line 16, in <genexpr>
    stringified = "\n".join("\t".join(row) for row in a)
TypeError: sequence item 0: expected string, float found

Thanks in Advance







More information about the Python-list mailing list