0% found this document useful (0 votes)
11 views6 pages

Array in Programming

The document provides a Python program for managing student grades using lists, including functions to add, display, and calculate the average of grades. It also explains arrays in pseudocode, covering declaration, assignment, accessing elements, and common operations. Additionally, it includes examples of storing and printing names using arrays.

Uploaded by

jaishah0623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Array in Programming

The document provides a Python program for managing student grades using lists, including functions to add, display, and calculate the average of grades. It also explains arrays in pseudocode, covering declaration, assignment, accessing elements, and common operations. Additionally, it includes examples of storing and printing names using arrays.

Uploaded by

jaishah0623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

 If you need to access or manipulate the car names, you can do so using standard list operations.
For example:

 Write a python in array that will store names of the car as Saab,Volvo,BMW

# Define a list to store car names

car_names = ["Saab", "Volvo", "BMW"]

# Print the list

print(car_names)

Here’s an example Python program that uses an array (list) to store grades of students:

# Initialize an empty list to store grades


grades = []

# Function to add a grade to the list


def add_grade(grade):
if 0 <= grade <= 100: # Validate the grade is between 0 and 100
grades.append(grade)
print(f"Grade {grade} added successfully.")
else:
print("Invalid grade. Please enter a grade between 0 and 100.")

# Function to display all grades


def display_grades():

OLWANDE ERICK +254729207653 1


2

if grades:
print("Grades of students:")
for i, grade in enumerate(grades, start=1):
print(f"Student {i}: {grade}")
else:
print("No grades available.")

# Function to calculate and display the average grade


def calculate_average():
if grades:
average = sum(grades) / len(grades)
print(f"The average grade is: {average:.2f}")
else:
print("No grades available to calculate the average.")

# Menu to interact with the program


def menu():
while True:
print("\nGrade Management System")
print("1. Add a Grade")
print("2. Display Grades")
print("3. Calculate Average")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == "1":
try:
grade = float(input("Enter the grade: "))
add_grade(grade)
except ValueError:
print("Please enter a valid number.")
elif choice == "2":
display_grades()
elif choice == "3":
calculate_average()
elif choice == "4":
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

# Run the menu


menu()

Explanation:

1. Grades List: The grades list is used to store student grades.


2. Functions:
o add_grade() adds a grade to the list after validation.
o display_grades() shows all stored grades.
o calculate_average() computes the average of the grades.
3. Menu: A simple text-based menu allows interaction with the program. Users can add
grades, display all grades, calculate the average, or exit.

OLWANDE ERICK +254729207653 2


3

You can extend this program further by adding more features, such as removing grades or
validating user inputs more robustly.

ARRAY IN PSEUDOCODE
Sure! Let's break down arrays in pseudocode step by step with simple explanations and
examples.

What is an Array?

An array is a collection of elements, all stored together in a specific order. Each element in the
array can be accessed by its index (position in the array). Think of it like a list of items stored in
a row.

Declaration of an Array

In pseudocode, you can declare an array like this:

DECLARE arrayName AS ARRAY OF size

For example:

DECLARE numbers AS ARRAY OF 5

This creates an array named numbers with space for 5 elements.

Assigning Values to an Array

You can assign values to specific positions in the array using the index (starting from 0 or 1,
depending on the convention).

numbers[0] ← 10

OLWANDE ERICK +254729207653 3


4

numbers[1] ← 20
numbers[2] ← 30
numbers[3] ← 40
numbers[4] ← 50

Now the array numbers contains these values:

[10, 20, 30, 40, 50]

Accessing Array Elements

You can access elements using their index:

PRINT numbers [2]

This will output 30 because it's at index 2 (third position).

Looping Through an Array

You can use a loop to iterate through an array:

FOR i ← 0 TO 4
PRINT numbers[i]
END FOR

This will print all elements in the array, one at a time:

10
20
30
40
50

Common Operations

Here are a few common operations with arrays:

1. Finding the Length of an Array:


2. length ← LENGTH(numbers)
3. PRINT length

OLWANDE ERICK +254729207653 4


5

This outputs 5 for our example.

4. Updating an Element:
5. numbers[2] ← 35

Now the array becomes [10, 20, 35, 40, 50].

6. Summing Elements:

7. sum ← 0
8. FOR i ← 0 TO LENGTH(numbers) - 1
9. sum ← sum + numbers[i]
10. END FOR
11. PRINT sum

This will output 155 (the sum of all elements).

Example 1: Storing and Printing Names

Here’s a pseudocode example with strings:

DECLARE names AS ARRAY OF 3


names[0] ← "Alice"
names[1] ← "Bob"
names[2] ← "Charlie"

FOR i ← 0 TO 2
PRINT names[i]
END FOR

Output:

Alice
Bob
Charlie

OLWANDE ERICK +254729207653 5


6

Explanation:

1. Declare an Array: We define an array named Cars with a size of 3.


2. Assign Values: We assign the car names to the respective indices (0-based indexing).
3. Iterate (Optional): The loop iterates through the array to print each car name. This step is optional if
just storing is required.

OLWANDE ERICK +254729207653 6

You might also like