C Programming
Arrays
"Defined path for a Stable career in Core Technologies“
hello@etechprowess.com
INDEX
❑ Arrays ❑ Arrays & Functions
❑ Types of Arrays ❑ Arrays & Pointers
❑Single-Dimensional Array
❑Multi-Dimensional Array
❑ Why do we use arrays
❑ Accessing Arrays
❑ Declare & Initialize Arrays
❑ Arrays of Strings
❑ String Handling Functions
🌐 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];
• Note : C language does not support strings as a data type
Example - Strings
• Program to use a string variable
• #include <stdio.h>
#include<string.h>
int main() {
char name[10]; int index = 0;
printf("Enter your name : ");
scanf("%s", name);
printf("Your name is %s\n",name); // '\0' - null character , has to be written in single quote
while(name[index] != '\0'){
printf("At position %d, data is %c\n", index, name[index]);
index++;
}
printf("Length of name is %d\n", strlen(name)); // find number of character in name array
printf("Size of name array is %ld\n\n", sizeof(name)/sizeof(char)); // find total size of
character array
return 0;
}
String Handling Functions:
• C language supports a large number of string handling functions that can
be used to carry out many of the string manipulations.
• These functions are packaged in the string.h library.
• Following are the most commonly used string handling functions.
Arrays and Functions
• In C, Arrays can be passed as an argument to any function.
• Both one-dimensional and multi-dimensional array can be passed to
functions as an argument.
• Individual element of an array can be passed to function using call by
value.
• Original array elements remain unchanged, as the actual element is never
passed to function.
• Therefore function body cannot modify original value in this case.
Arrays & Functions
• Program to pass a single element of an array to function
• #include<stdio.h>
void display(int a)
{
printf("%d", a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only
return 0;
}
Example – Arrays & Functions
• Program to pass a 1-D array to a • float average(float m[]){
function int i;
float avg, sum=0.0;
• #include<stdio.h> for(i=0;i<6;++i){
float average(float m[]); sum+=m[i];
int main(){ }
float avg, marks[]={63.4, 55, 52.6, 73, avg =(sum/6);
40.5, 48}; return avg;
avg=average(marks); }
/* Only name of array is passed as an
argument. */
printf("Average age=%.2f",avg);
return 0;
}
Example – Arrays & Functions
• Program to pass 2D arrays as • absolute_value(matrix);
arguments to function /* passing multi-dimensional array to
function */
• #include<stdio.h> for(i=0;i<2;++i){
void absolute_value(int matrix[2][2]);
int matrix[2][2]; for(j=0;j<2;++j){
int main(){ printf("%d\t", matrix[i][j]);
int i, j; }
int *abs_value; printf("\n");
printf("Enter 4 numbers:\n"); }
for(i=0;i<2;++i) return 0;
{ }
for(j=0;j<2;++j) void absolute_value(int m[2][2]){
{ int i, j;
scanf("%d", &matrix[i][j]); for(i=0;i<2;++i)
} for(j=0;j<2;++j)
}
if(m[i][j]<0){
matrix[i][j] = m[i][j] * -1;
}
}
Arrays & Pointers
• Pointers can be used to pass on arrays, strings, functions, and variables as
arguments of a function.
• Passing on arrays by pointers saves lot of memory.
Only the address of array is passed instead of all the elements of an array.
• Otherwise, it would mean passing on copies of all the elements and thus
taking lot of memory space.
Arrays & Pointers
• Program to illustrate working of arrays & Pointers
• #include <stdio.h>
int main() {
int roll_no[5] = {11, 12, 13, 14, 15};
int* ptr;
// ptr is assigned the address of the third element
ptr = &roll_no[2];
printf("*ptr = %d \n", *roll_no);
printf("*(ptr+1) = %d \n", *(roll_no+1));
printf("*(ptr-1) = %d", *(roll_no-1));
return 0;
}
Arrays & Pointers
• Program to save memory by passing a • float average(float *ptr ){
reference instead of an array int i;
• #include<stdio.h> float avg=0, sum=0.0;
float average(float m[]); for(i=0;i<6;++i){
int main(){ printf("%x\t",ptr);
float *p; sum+=*ptr;
float avg, marks[]={63.4, 55, 52.6, 73, ptr++;
40.5, 48};
p=&marks[0];
printf("In main: %x\n", &marks[2]); }
avg=average(p); avg =(sum/6);
// Only name of array is passed as an argument. return avg;
printf("\nAverage marks=%.2f",avg); }
return 0;
}
THANK YOU
eTECH Prowess
Phone:+91 7676651416, 080 4890 919
Email:hello@etechprowess.com
http://etechprowess.com