How to input values of the matrix from keyboard in python

P

Pramod

Hi

I want to know the how we input values into the matrix (N*N size)
from keyboard in python,

Here I wrote Matrix programe in C++


This asks values from key board and print on the console N*N matrix ;




Thanks in advance ....


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

Chris Rebert

Hi

  I want to know the how we input values into the matrix (N*N size)
from keyboard in python,

Here I wrote Matrix programe in C++

This  asks values from key board and print on the console N*N matrix ;

Thanks in advance ....


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


from random import random
from sys import exit

while True:
try:
N = int(raw_input("Enter size of the matrix: "))
except ValueError:
print "Invalid input. Try again."
except EOFError:
exit(1)
else:
break

a = [[random() for j in xrange(N)] for i in xrange(N)]
stringified = "\n".join("\t".join(row) for row in a)
print stringified


If you're doing serious work with matrices, look at the NumPy package.

Cheers,
Chris
 
M

MRAB

Pramod said:
Hi

I want to know the how we input values into the matrix (N*N size)
from keyboard in python,

Here I wrote Matrix programe in C++


This asks values from key board and print on the console N*N matrix ;
[snip]

Read from the keyboard using raw_input() (in Python 2, or input() in
Python 3) and convert to float.

Incidentally, your C++ program doesn't allocate the array.
 
C

Chris Rebert

Hi

  I want to know the how we input values into the matrix (N*N size)
from keyboard in python,

Here I wrote Matrix programe in C++

This  asks values from key board and print on the console N*N matrix ;

Thanks in advance ....


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


from random import random
from sys import exit

while True:
   try:
       N = int(raw_input("Enter size of the matrix: "))
   except ValueError:
       print "Invalid input. Try again."
   except EOFError:
       exit(1)
   else:
       break

a = [[random() for j in xrange(N)] for i in xrange(N)]
stringified = "\n".join("\t".join(row) for row in a)
print stringified


Should have prefaced that with an "untested" disclaimer.
Make that:
stringified = "\n".join("\t".join(str(cell) for cell in row) for row in a)

- Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,919
Messages
2,570,037
Members
46,444
Latest member
MadeleineH

Latest Threads

Top