This short presentation goes over the basics of arrays in C, including how to declare, initialize, and use both 1D and 2D arrays. It’s put together in a simple way, based on what I understood while learning the topic myself.
ARRAYS IN C
Presentationby Niharika Patidar
One Dimensional & Two Dimensional Arrays
Declaration & Initialization Of Arrays
Course: Bachelor of Computer Application
Subject: Logic Development Programme
Assigned By: Prof. Atik Sama
2.
INTRODUCTION
Today, we’re goingto talk about arrays in C. We’ll cover the basics
of one-dimensional (1D) and two-dimensional (2D) arrays, and I’ll
show you how to declare and initialize them. Arrays are essential
for organizing and managing data, so understanding them will
really help you as you dive into programming. Let’s get started!
3.
WHAT ARE ARRAYS?
Arraysare a data structure that stores
a fixed-size collection of elements of
the same type. They help organize and
manage data efficiently.
Main Purpose:
Organize Data: Arrays allow you to
group related values together,
making it easy to access and
manipulate them.
5.
TYPES OF ARRAYS
Alinear collection of elements
accessed by a single index.
A grid-like collection of
elements accessed by two
indices, typically representing
rows and columns.
1-DIMENSIONAL
ARRAYS
2-DIMENSIONAL
ARRAYS
6.
Definition: A linearcollection of elements accessed by a single index.
Syntax:
data_type array_name[size];
Example:
int numbers[5] = {1, 2, 3, 4, 5};
Important Notes:
Array indices start at 0 (the first element is accessed using index 0).
The size must be defined at the time of declaration.
1-D ARRAYS
8.
Definition: A grid-likecollection of elements accessed by two
indices, typically representing rows and columns.
2-D ARRAYS
int matrix[3][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Syntax:
data_type array_name[rows][columns];
Example:
10.
DECLARATION
Declaring an arrayinvolves specifying its data type and size, which
defines how many elements it can hold.
int numbers[5]; // Declares an array that can hold 5 integers
Example:
data_type array_name[size];
Syntax:
12.
INITIALIZATION
Initialization is theprocess of assigning values to an array when it
is declared.
When an array is declared or memory is allocated, its elements may
contain undefined or "garbage" values. Therefore, it’s important to
initialize the array with meaningful values.
When It’s Done:
It occurs at the time of declaration to set the initial values of the array.
int scores[4] = {85, 90, 78, 92};
Example: