0% found this document useful (0 votes)
15 views17 pages

Arrays PST

Uploaded by

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

Arrays PST

Uploaded by

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

DR NSAM

FIRST GRADE COLLEGE

DEPARTMENT OF COMPUTER
APPLICATIONS
PROBLEM SOLVING
TECHNIQUES

ARRA
YS
“The Backbone of Data Organization – Simplifying
Complexity, One Element at a Time!"

"Have you ever struggled to organize your collection of songs, books, or


videos?"
"Imagine a tool that helps you organize things systematically.
That's what arrays do in programming!"
WHAT IS
ARRAY?
An array is a collection of elements
of the same type stored in
contiguous memory locations.
An array is like a series of boxes
(elements) arranged next to each
other in memory (contiguous
locations). Each box stores a value,
and all values must be of the same
type (e.g., integers, floats). This
setup allows quick access to any
box using its position (index),
making it efficient for managing and
LET US
UNDERSTAND IT
WITH
TRAI
Compare an
EVERYDAY
N array to a train: each
LIFE
compartment EXAMPLES
stores one value, and
all compartments are numbered
(indexed).
The train represents the array as a
whole, which is the collection of
elements.
• Just like a train is a sequence of
connected compartments, an
array is a sequence of memory
locations.
• All compartments (elements) in
the train are connected in a
LIBRARY
BOOKSHELVES PARKING LOT

• Think of a library with a long • A parking lot with numbered slots


bookshelf containing compartments. represents an array. Each slot
• Each compartment is labeled with a (element) holds one car (value).
number and holds exactly one book. • The slot number is used to locate or
• To find a book, you refer to the change the car parked there.
compartment number (index). • The parking lot is the array, slots are
• The bookshelf is the array, the elements, and the slot numbers
compartments are the elements, and are indices.
the compartment numbers are the • All slots are contiguous and fixed in
indices. number, just like an array.
• Just like you can replace one book
with another, you can modify an array
element.
SEATS IN CALENDAR
CINEMA
• A row of seats in a cinema is numbered • A monthly calendar has 30 or 31 numbered
sequentially.
days (compartments).
• Each seat may be occupied (1) or empty
• Each day may hold an event, task, or holiday
(0), representing whether someone is
(value).
sitting there. • The month is the array, days are the elements,
• The cinema row is the array, seats are the
and the events/tasks are the values.
elements, and the values indicate whether
the seat is occupied.

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

elements the elements,


TYPES OF
1-DIMENSIONAL ARRAY ARRAY
A One-Dimensional Array is the simplest form
of an Array in which the elements are stored
linearly and can be accessed individually by
specifying the index value of each element
stored in the array. It has only one subscript

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

Example: int a[3] [3]= {1,2,3,4,5,6,7,8,9};


int a[3] [3]= { {1,2,3}, {4,5,6},
MULTI DIMENSIONAL ARRAY

A multi-dimensional array is also known as a


matrix:

• A multi-dimensional array is an array with


more than one dimension or level. Also
called as a 3D array is an array of arrays of
arrays, adding another dimension.
• Multi-dimensional arrays are used to store
and manipulate data in multiple dimensions
or axes. They are commonly used in fields
such as mathematics, statistics, and
computer science to represent and process
structured data, such as images, videos,
CHARACTERISTICS OF ARRAYS: USES AND IMPORTANCE OF
ARRAYS

• Fixed Size: Size is determined when • Simplifies data management.


declared. • Enhances performance in bulk data processing.
• Homogeneous Elements: All elements in • Used in algorithms, data structures, and
the array are of the same data type. memory-efficient programming.
• Indexed: Elements can be accessed using • Data Management: Simplifies handling multiple
their position (index), starting from 0. values.
• Efficient Storage: Data is stored • Algorithm Design: Essential for searching, sorting,
sequentially in memory. and matrix operations.
• Gaming: Stores player scores, levels, or inventory
items.
• Data Structures: Forms the foundation for
advanced structures like stacks, queues, and
graphs.
WHAT DOES ARRAY HELP US OVERCOME IN C-
PROGRAMMING?

1. Replacing Multiple Variables with a Single Name : Arrays allow storing multiple elements under
single variable name

2. Simplifying Iterative Operations : Arrays allow you to loop through elements,


making
operations concise and efficient.

3. Efficient Memory Management : Arrays allocate contiguous memory for all


elements,
optimising memory usage and access
speed.

4. Dynamic Data Handling : Arrays can dynamically hold as many


elements as
needed combined with pointers and
dynamic
memory allocation.

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.

• Difficult Data Management:


Arrays provide a structured way to manage related data.
For example, you can sort, search, or manipulate elements
in an array easily using built-in methods. Without an array,
you would need to write custom logic for each task,
complicating your code.

• Memory Management Challenges:


Arrays use contiguous memory locations, which can optimize
memory usage. Declaring individual variables may lead to
fragmented memory allocation and inefficient use of memory
resources.

By using arrays, you overcome these challenges and achieve


better code readability, maintainability, and efficiency.
6.Random Access to Data : Arrays allow direct access to any element using its index,
enabling faster
operations.

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.

How an Array Helps:


• Instead of creating separate variables for each student, a single array can store all the
marks
• This makes it easy to calculate averages, find the highest or lowest marks, and more.

2. Managing a Library Database


Scenario:
• A library needs to keep track of book IDs, titles, or availability status.

How an Array Helps:


• Arrays can store:
⚬ Book IDs: int bookIDs[1000];
⚬ Book titles: char bookTitles[1000][50];
⚬ Availability: bool isAvailable[1000];
3. E-Commerce Inventory Management
Scenario:
• An online store tracks product IDs, prices, and stock quantities.

How an Array Helps:


• Store product details efficiently
int productIDs[5000];
float productPrices[5000];
int stockQuantities[5000];

4. Image Processing
Scenario:
• Editing a photo involves manipulating pixel data, which is stored in a grid-like
structure.

How an Array Helps:


• Use a 2D or 3D array to represent pixels:
⚬ For grayscale: int pixels[height][width];
⚬ For RGB: int pixels[height][width][3]; // [R, G, B]
5. Traffic Management
Scenario:
• A system monitors the number of vehicles at different intersections in a
city

How an Array Helps:


• Use arrays to track vehicle counts for each intersection:
int vehicleCount[10]; // 10 intersections
ANY DOUBTS
OR
QUESTIONS??

You might also like