L05 - Arrays
L05 - Arrays
Arrays
🌐 etechprowess.com
Arrays
• An array is defined as the collection of similar type of data items. are
• They stored at contiguous memory locations.
• Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc.
• Syntax for arrays
• data_type array_name [array_size];
Types of Arrays
• There are two types of arrays:
• Single dimension Array
• Multi-dimensional Array
• Single Dimensional Arrays:
• Single dimensional array or
1-D array is the simplest form of
arrays.
• This type of array consists of
elements of similar types and these
elements can be accessed through
their indices.
Types of Arrays
• Multi-dimensional Arrays:
• The most common type of multi-dimensional array that is used is a 2-D array.
• Although, the number of dimensions can be more than 2 depending upon the
compiler of the user’s system.
• These arrays consist of elements that are array themselves.
Why do we use Arrays
• Arrays can be used to store large amount of data without declaring
multiple variables.
• Example: Read the data (let say 1000 numbers) from a file, then print out the data
in reversed order.
• Arrays help us to group relevant data items together in a single composite
data structure.
• Example: In a given data of exam conducted by a school, using arrays it is easy to
count how many students got each score from 0 to 100.
• Arrays stores the data which can be accessed in a random order.
• Example: Read a file containing weather data, store each month’s stats as an
element in an array, and then examine it to find overall weather statistics
Accessing Arrays
• Arrays can store the collection of derived data types, such as pointers,
structure, etc.
• Each array element can be randomly accessed by using its index number
• Example: if you want to store 100 integers, you can create an array for it.
• int data[100];
• It's important to note that the size
and type of an array cannot be
changed once it is declared.
• Suppose you declared an array :
• float mark[5];
Declare & Initialize an Array
• The simplest way to initialize an array is by using the index of each
element.
• Each element of an array can be initialized by using the index.
• Example:
• int marks[5]; //declaration of an array
marks[0]=80; //initialization of each array element
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
• Or it can be declared & initialized together
• int marks[5] = { 80, 60, 70, 85, 75 };
Example : Array
• Program to use an array variable
• #include <stdio.h>
int main()
{
int marks[]= {45,56,45,7,23,76}; // declaration & initialization
int age[3]= {5,8}; // declaration & initialization
age[3]=56; // initialization
//printf("%d", marks); // not applicable - so indexing is required
printf("%d\n",marks[0]);
printf("%d",marks[1]);
// find number of elements in an arrays
printf("%ld\n\n", sizeof(marks)/sizeof(int));
}
Example : Arrays and Loops
• Program to read & write data in an • //access salary for 5 employees
array for (int i=0; i <= 5; i++) //if i<=5, it access
• #include <stdio.h> next memory location
int main() {
{ printf("At %x, data stored is
float salary[5]; %.2f\n", &salary[i], salary[i]);
printf("Enter salary of 5 employees"); }
//Note: to read, access & print all
//store array elements elements in an array, always use loops
for (int i=0; i < 5; i++)
{
scanf("%f", &salary[i]); return 0;
} }
Array of Strings
• String is a sequence of characters that are treated as a single data item and terminated by a
null character '\0’.
• A string is actually a one-dimensional array of character.
• Strings are often used to create meaningful and readable programs.
• Declaring a string is as simple as declaring a one-dimensional array.
• Example :
• char string_name[size];
eTECH Prowess
Email:[email protected]
http://etechprowess.com