Himanshu Rastogi
Himanshu Rastogi

Reputation: 63

How to input element in 2d array

Like below C++ Code, how could I use Python to input elements in 2d array? Please, help in writing the same program in Python3.

int main()
{
 int s = 3;
 int a[s][s];
 cout<<"Enter 9 Element in Square Matrix";
 for(int i =0;i<s;i++)
 {
  for(int j =0; j<s;j++)
  {
   cin>>a[i][j];
  }
 }
 cout<<"You Entered";
 for(int i =0;i<s;i++)
 {
  for(int j =0; j<s;j++)
  {
   cout<<a[i][j]<<"\t";
  }
 cout<<endl;
 }
return 0;
}
Output:
Enter 9 Elements in Square Matrix
1
2
3
4
5
6
7
8
9
You Entered: 
1 2 3
4 5 6
7 8 9

If there is a mistake in the program, please don't try to correct it. Thank you.

Upvotes: 4

Views: 8606

Answers (4)

Shreya Saha
Shreya Saha

Reputation: 31

Say you want to create a 3*3 matrix:

Initialize the matrix as follows:

matrix = [x[:] for x in [[0] * 0] * 0]

Then take the matrix elements as input from the user:

    for i in range(0,3):
        row_list = []
        for j in range(0,3):
            row_list.append(int(input()))
        matrix.append(row_list)

Upvotes: 0

Martin Castro Alvarez
Martin Castro Alvarez

Reputation: 493

If you are not familiar with python, you should create a file called, for example, matrix.py and then add the following content:

matrix_size = 3
matrix = []

print("Enter {} Elements in Square Matrix:".format(matrix_size))
for i in range(0, matrix_size):
    row = []
    for j in range(0, matrix_size):
        row.append(input())
    matrix.append(row)

print("You entered:")
for i in range(0, matrix_size):
    print(" ".join(matrix[i]))

After saving the file, you can execute this file this way:

python3 matrix.py

Here is a sample output:

[martin@M7 tmp]$ python3 matrix.py
Enter 3 Elements in Square Matrix:
1
2
3
1
2
3
7
5
4
You entered:
1 2 3
1 2 3
7 5 4

Upvotes: 1

Snochacz
Snochacz

Reputation: 725

s = 3
a = [x[:] for x in [[0] * s] * s]

print("Enter 9 Element in Square Matrix")

for i in range(0, s):
    for j in range(0, s):
        a[i][j] = input()

print("You Entered")

for i in range(0, s):
    line = ''
    for j in range(0, s):
        line += a[i][j] + ' '
    print(line)

Upvotes: 1

Kaushik NP
Kaushik NP

Reputation: 6781

Am gonna use list to store the 2D array here. There are many other structures you can use for storing a 2D array, but for basic needs, this will suffice.

n=int(input("Enter N for N x N matrix : "))         #3 here
l=[]                                                #use list for storing 2D array

#get the user input and store it in list (here IN : 1 to 9)
for i in range(n): 
  row_list=[]                                      #temporary list to store the row
  for j in range(n): 
     row_list.append(int(input()))                 #add the input to row list
  l.append(row_list)                               #add the row to the list

print(l)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#Display the 2D array
for i in range(n):
  for j in range(n):
    print(l[i][j], end=" ")
  print()                                           #new line

'''
1 2 3 
4 5 6 
7 8 9 
'''

Upvotes: 4

Related Questions