Arrays PST
Arrays PST
DEPARTMENT OF COMPUTER
APPLICATIONS
PROBLEM SOLVING
TECHNIQUES
ARRA
YS
“The Backbone of Data Organization – Simplifying
Complexity, One Element at a Time!"
LADDER VENDING
STEPS MACHINE
• A vending machine has slots for snacks,
• A ladder has rungs (steps) that are evenly
spaced each
and numbered sequentially. labeled with a number.
• Each rung can hold a specific tool (value). • Each slot contains one type of snack.
The ladder is the array, rungs are the • The vending machine is the array, slots are
DECLARATIO
NSyntax: data_type array_name[size]={value-1, value-2.....
valune-n}
INITIALIZATION
int arr[3] = {10, 20,
30};
2-DIMENSIONAL
ARRAY
A two-dimensional array, also known as a 2D array,
is a collection of data elements arranged in a grid-
like structure with rows and columns. Each element
in the array is referred to as a cell and can be
accessed by its row and column indices/indexes. It
has two subscripts and it is also called as row-major
order
DECLARATIO
Syntax:
N data_type array_name[size 1] [size
2];
INITIALIZATION
1. Replacing Multiple Variables with a Single Name : Arrays allow storing multiple elements under
single variable name
5. Easy Searching and Sorting : Arrays support algorithms like binary search
DECLARING ELEMENTS INDIVIDUALLY WITHOUT USING AN ARRAY CAN MAKE
ACCESSING,
DISPLAYING, OR STORING THEM MORE CHALLENGING FOR THE FOLLOWING
REASONS:
Scalability Issues:
When you declare each element individually, you must create separate
variables
(e.g., var1, var2, var3). This becomes cumbersome and impractical when
dealing
with large datasets, as you would need to manage a large number of variables.
Code Redundancy:
Operations such as accessing, updating, or iterating through these individual
variables require repetitive code. For example, printing 100 variables one by
one would require writing 100 print statements.
Lack of Flexibility:
Arrays allow you to use loops to perform operations on all elements efficiently.
Without arrays, you can't easily loop through variables since they are not stored
in a unified structure.
• Increased Risk of Errors:
Manually handling multiple variables increases the chance of
errors such as typos, missed variables, or incorrect updates.
7. Grouping Related Data : Arrays group related data logically, making it easier to process
and
interpret.
8. Reduced Code Complexity : Arrays enable concise and structured code, reducing redundancy.
1. Storing Student Marks or Attendance
Scenario:
• A teacher wants to record the marks of 50 students in a class.
4. Image Processing
Scenario:
• Editing a photo involves manipulating pixel data, which is stored in a grid-like
structure.