142k views
1 vote
9.3 Code Practice

Write a program that creates a 4 x 5 grid called numbers. The elements in your array should all be random numbers between -30 and 30, inclusive. Then, print the array as a grid

Please help!!!

User Slallum
by
5.2k points

1 Answer

1 vote

Final answer:

The question involves writing a Python program to create a 4 x 5 grid of random numbers between -30 and 30 and to print it out. Using the random module's randint function, the grid is filled with random numbers and then printed.

Step-by-step explanation:

To create a 4 x 5 grid with random numbers between -30 and 30, inclusively, you can use the Python programming language. The Python code below generates the desired grid and prints it.

import random
numbers = [[random.randint(-30, 30) for _ in range(5)] for _ in range(4)]
for row in numbers:
print(' '.join(str(n).rjust(3) for n in row))

This program first imports the random module which includes functions for generating random numbers. It then creates a list of lists (2D array) to represent the grid. The random.randint() function is used to fill the grid with random numbers. Finally, the grid is printed with each number properly spaced.

User Jiggunjer
by
4.8k points