0% found this document useful (0 votes)
3 views

c Programming

Uploaded by

panthers82638
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

c Programming

Uploaded by

panthers82638
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ARRAYS

IN C
Presented By : Raj Verma
1
INTRODUCTION TO ARRAYS
What is an Array?
• A data structure that stores multiple elements of
the same type in a contiguous memory block.
• Each element can be accessed using an index.
Why Arrays Are Important:
• Simplifies handling large amounts of related data.
• Crucial for efficient data organization and
manipulation.
int number[5];
Example:
C PROGRAMMING
2
KEY CHARACTERISTICS OF ARRAYS
Contiguous Memory Allocation: All elements are
stored in
adjacent memory locations.
Fixed Size: Declared size cannot be changed during
program execution.
Index-Based Access:
Elements number[2]
are accessed via their index (starting from
0).
Example: accesses the third
element. C PROGRAMMING
Homogeneous Elements: All elements must be of the
3
DECLARING AND INITIALIZING ARRAYS

Declaration Initialization
Syntax Methods
At the time of declaration
data_type
array_name[size]; int marks[5] = {10, 20, 30, 40, 50};
Example: int marks[5]; Assigning values later
marks[0] = 10;

C PROGRAMMING
4
ACCESSING ARRAY ELEMENTS

Accessing Modifying
•Elements
Use the array name Elements
marks[2] = 75;
and index to retrieve // Updates the third element
values. to Note:
75. Index starts at 0
• Example: marks[2]);
printf("%d",
and goes up to size - 1.
// Outputs the third
element

C PROGRAMMING
5
TYPES OF ARRAYS IN C

One-Dimensional Arrays Two-Dimensional


• Arrays
C PROGRAMMING

• Store data in a single row or Store data in a grid format


line. (rows and columns).
• Example: • Example:
int numbers[5];
int matrix[3][3];

Multi-Dimensional Arrays
Used for higher-dimensional
data (rarely needed).
6
LOOPING THROUGH ARRAYS
Why Loops Are Useful:
1
C PROGRAMMING

• Automatically access or modify all elements.

for(int i = 0; i < 5; i++)


Example with for Loop:
2
{ printf("%d ", marks[i]);
}
Tip: Nested loops are required for multi-dimensional arrays.
3
7
APPLICATIONS OF ARRAYS
Common Use Cases:
• Storing and processing lists (e.g., student
scores, product prices).
• Sorting and searching algorithms (e.g.,
bubble sort, binary search).
• Representing matrices in mathematical
operations.
• Storing strings (character arrays).
• Managing pixel data in graphics
programming.
C PROGRAMMING
8
ADVANTAGES AND LIMITATIONS OF
•Advantages: ARRAYS
• Simplified code for managing large
datasets.
• Easy to iterate through elements.
• Memory-efficient for fixed-size data.
• Limitations:
• Fixed size: Cannot dynamically grow or
shrink.
• No built-in error handling for out-of-bound
access.
C PROGRAMMING
• Only one data type allowed per array.
9
COMMON ERRORS WITH ARRAYS
Out-of-Bounds Access:
Accessing an index beyond the declared
size causes
undefined behavior.
int arr[3];
• Example:arr[5] = 10; // Error!

Uninitialized Arrays:
Arrays not explicitly initialized may contain
garbage
C PROGRAMMING
values.
Incorrect Indexing: Forgetting the 0-based
10
CONCLUSION
Key Takeaways:
• Arrays are essential for managing
collections of homogeneous data.
• Provide efficient storage and access
mechanisms.
• Useful for algorithms, matrix operations,
and string manipulations.
Next Steps:
• Explore dynamic arrays and pointers to
arrays.
• Practice using arrays in real-world
C PROGRAMMING
scenarios for deeper understanding.
THANK
FOR YOUR
ATTENTION

YOU !

You might also like