0% found this document useful (0 votes)
8 views7 pages

Untitled Document-10

The document is an assignment on one-dimensional arrays and conditional statements in programming, submitted by Sarah Adil to Prof Mubbara at the University of Central Punjab. It defines a 1D array, provides examples in Python, and discusses common issues and solutions related to conditional statements. Additionally, it includes a dry run example demonstrating how to calculate the sum of elements in a 1D array.

Uploaded by

hashimraja2003
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)
8 views7 pages

Untitled Document-10

The document is an assignment on one-dimensional arrays and conditional statements in programming, submitted by Sarah Adil to Prof Mubbara at the University of Central Punjab. It defines a 1D array, provides examples in Python, and discusses common issues and solutions related to conditional statements. Additionally, it includes a dry run example demonstrating how to calculate the sum of elements in a 1D array.

Uploaded by

hashimraja2003
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/ 7

Assignment # 3

University of Central Punjab Lahore


Submitted To:
Prof Mubbara
Submitted by:
Sarah Adil
Programm:
ADP CS 1ST SEMESTER
Course Title:
Introduction to Computing
Roll no:
03
Submission Date:
31st January Friday, 2025
Title:
1D (one dimensional array)
Definition:
A 1D (one-dimensional) array is a data structure that stores a collection of elements of
the same type, arranged in a single row or a single column. Each element in the array is
accessible through an index, which typically starts from 0.

Example of a 1D Array in Python:

# Defining a 1D array
arr = [10, 20, 30, 40, 50]

# Accessing elements
print(arr[0]) # Output: 10
print(arr[2]) # Output: 30
Conditions:
A conditions system in programming refers to the use of conditional statements to
control the flow of a program based on specified conditions. It allows the program to
make decisions and execute specific code blocks depending on whether a condition is
true or false.

Common Conditional Statements:

1. if Statement: Executes code when a condition is true.

2. if-else Statement: Executes one block of code if the condition is true, and another
block if it is false.

3. if-elif-else Statement: Checks multiple conditions in sequence.

4. Switch Case (in some languages): Alternative for multiple conditions (not available
directly in Python).
# if statement
x = 10
if x > 5:
print("x is greater than 5") # Output: x is greater than 5

# if-else statement
y=3
if y > 5:
print("y is greater than 5")
else:
print("y is less than or equal to 5") # Output: y is less than or equal to 5

# if-elif-else statement
z=7
if z < 5:
print("z is less than 5")
elif z == 7:
print("z is equal to 7") # Output: z is equal to 7
else:
print("z is greater than 5 but not equal to 7")

Problems and solution:

If you're facing issues with a conditions system, here are a few common problems and
solutions you can look into:

1. Incorrect Condition Evaluation

Problem: The condition might not be evaluating as expected, leading to incorrect output.

Example:

x = 10
if x = 10: # Incorrect: assignment instead of comparison
print("x is 10")

Solution: Use the correct comparison operator (== instead of =).

if x == 10:
print("x is 10")

2. Nested Conditions Not Working Properly

Problem: You might be using nested if conditions incorrectly.

Example:

x=5
if x > 0:
if x < 10:
print("x is between 0 and 10") # Indentation error

Solution: Ensure the code inside the nested condition is properly indented.

if x > 0:
if x < 10:
print("x is between 0 and 10")

3. Multiple Conditions

Problem: Using and or or operators incorrectly in multiple conditions.

Example:
x=5
if x > 0 or x < 10: # This always evaluates to True
print("x is positive or less than 10")

Solution: Double-check the logic. In the example above, x > 0 or x < 10 will always be
true because all numbers are either positive or less than 10. Consider restructuring your
condition:

if x > 0 and x < 10:


print("x is between 0 and 10")

4. Missing else or elif When Needed

Problem: Missing else or elif clauses, leading to incomplete decision-making.

Example:

x = 15
if x < 10:
print("x is less than 10")
# No else or elif to handle other conditions

Solution: Add the else or elif to handle other possibilities.

if x < 10:
print("x is less than 10")
else:
print("x is 10 or greater")

5. Confusion Between == and =

Problem: Using = (assignment) instead of == (comparison) in conditions.


Example:
x=5
if x = 10: # Incorrect: assignment instead of comparison
print("x is 10")

Solution: Use == to compare values.

if x == 10:
print("x is 10")

6. Boolean Variables

Problem: Confusion when using Boolean values (True or False) directly in conditions.

Example:
is_raining = True
if is_raining:
print("Take an umbrella")

Solution: If the variable is already a Boolean, you can directly use it in the condition
without comparison. Just check the value.

Dry Run:
A dry run is a manual simulation of a program's execution to track the flow and values of
variables at each step. Let's do a dry run of a simple 1D array operation in Python to
help you understand how it works.

Problem:
We have a 1D array, and we want to find the sum of all the elements in the array.
Code:

arr = [1, 2, 3, 4, 5] # Define the array


sum = 0 # Initialize sum to 0
# Loop through each element of the array and add it to sum
for num in arr:
sum += num

print("Sum of elements:", sum)

Dry Run:
1. Initialization:
arr = [1, 2, 3, 4, 5]
sum = 0

2. First Iteration (num = 1):


sum += 1 → sum = 0 + 1 = 1
Now, sum = 1

3. Second Iteration (num = 2):


sum += 2 → sum = 1 + 2 = 3
Now, sum = 3

4. Third Iteration (num = 3):


sum += 3 → sum = 3 + 3 = 6
Now, sum = 6

5. Fourth Iteration (num = 4):


sum += 4 → sum = 6 + 4 = 10
Now, sum = 10

6. Fifth Iteration (num = 5):


sum += 5 → sum = 10 + 5 = 15
Now, sum = 15

7. End of Loop: The loop has finished iterating through all the elements in the array.

8. Final Output:
print("Sum of elements:", sum) will output:
Sum of elements: 15

Explanation:
In each iteration of the loop, the current element of the array (num) is added to the sum.
After all iterations, the sum holds the total of all elements in the array, which is 15.
This dry run shows how the loop processes each element and how the sum variable is
updated at each step.

You might also like