2D Array: Display Circle - Python Programming Exercise

In this exercise, you will develop a Python program that creates a 70x20 two-dimensional array of characters, "draws" a circle with a radius of 8 inside it, and displays it on screen. This exercise is perfect for practicing array manipulation, trigonometric functions, and loops in Python. By implementing a program that calculates the points of a circle using trigonometric functions and displays them in a two-dimensional array, you will gain hands-on experience in handling arrays and mathematical functions in Python. This exercise not only reinforces your understanding of arrays and trigonometry but also helps you develop efficient coding practices for managing and displaying data.

 Category

Arrays, Lists, and Strings

 Exercise

2D Array: Display Circle

 Objective

Develop a Python program that creates a 70x20 two-dimensional array of characters, "draws" a circle with a radius of 8 inside it, and displays it on screen.

Hint: The points in the circle can be obtained using:
x = xCenter + r * cos(angle)
y = yCenter + r * sin(angle)

"sin" and "cos" expect the angle to be measured in radians, instead of degrees. To convert from one unit to the other, remember that 360 degrees = 2π radians (or 180 degrees = π radians): float radians = (angle * Math.PI / 180.0);

You might draw 72 points (as there are 360 degrees in a circle, they would be spaced 5 degrees from each other).

Hint: In Python, cosine is math.cos, sine is math.sin, and π is math.pi.

 Example Python Exercise

 Copy Python Code
import math

# Define the dimensions of the array
rows = 20
cols = 70

# Create a 70x20 two-dimensional array initialized with spaces
array = [[' ' for _ in range(cols)] for _ in range(rows)]

# Circle parameters
radius = 8
x_center = 35  # Horizontal center of the circle
y_center = 10  # Vertical center of the circle

# Draw the circle by calculating points
for angle in range(0, 360):  # We use steps of 1 degree for more precision
    radians = math.radians(angle)  # Convert angle to radians
    x = int(x_center + radius * math.cos(radians))  # Calculate x position
    y = int(y_center + radius * math.sin(radians))  # Calculate y position
    
    # Make sure the point is within the bounds of the array
    if 0 <= x < cols and 0 <= y < rows:
        array[y][x] = 'O'  # Mark the point with 'O'

# Display the array (circle)
for row in array:
    print(''.join(row))

 Output

                                                                                
                                                                                
                                                                                
                                                                                
                        OOOOOOOOOOOOOOOOOOOOOOOO                                 
                   O                                O                            
               O                                       O                         
           O                                               O                     
         O                                                   O                   
      O                                                         O                
     O                                                           O               
    O                                                             O             
     O                                                           O               
      O                                                         O                
         O                                                   O                   
           O                                               O                     
               O                                       O                         
                   O                                O                            
                        OOOOOOOOOOOOOOOOOOOOOOOO                                 
                                                                                
                                                                                
                                                                                

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Software Applications

    In this exercise, you will develop a Python program that can store up to 1,000 records of software applications. For each application, you must keep the following dat...

  •  Exercise tasks

    In this exercise, you will develop a Python program that can store up to 2,000 "to-do tasks". For each task, it must keep the following data: Date (a set of 3 ...

  •  Home Finances

    In this exercise, you will develop a Python program that can store up to 10,000 records of expenses and incomes to create a small household accounting system. For eac...

  •  Array Reversal

    This Python program prompts the user to input 5 numbers and stores them in an array (or list in Python). After all the numbers are entered, the program displays them ...

  •  Array Lookup

    his Python program asks the user to input a specific number of floating-point numbers and stores them in a list. The program then repeatedly prompts the user to enter...

  •  Even Number Array

    This Python program prompts the user to enter 10 integer numbers and displays only the even numbers from the list. It utilizes a for loop to iterate through th...