Unit 3 Arrays
Unit 3 Arrays
Syntax
goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement
marked as a label. Here, the label is a user-defined identifier that indicates the target
statement.
#include <stdio.h>
int main() {
int i;
int number, average, sum = 0;
jump:
printf("Jump called");
return 0;
}
Unit 3
Arrays
The variables that we have used till now are capable of storing only one value at a time.
Consider a situation when we want to store and display the age of 100 employees.
A one-dimensional array has one subscript, two dimensional array has two subscripts
and on.
The one-dimensional arrays are known as vectors and two-dimensional arrays are known
as matrices.
Properties of Arrays
• Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
• Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
• Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of the data
element.
Advantage of C Array
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit.
One Dimensional Array
Declaration of I-D Array
We can declare an array by specifying its name, the type of its elements, and the size of its
dimensions.
When we declare an array in C, the compiler allocates the memory block of the specified size
to the array name.
data_type array_name[size];
int age[100];
float sal[15];
char grade[20];
Accessing I-D Array Element
The elements of an array can be accessed by specifying the array name followed by subscript
in brackets.
Array Initialization with Declaration
In this method, we initialize the array along with its declaration. We use an initializer list
to initialize multiple elements of the array. An initializer list is the list of values enclosed
within braces { } separated b a comma.
we can skip declaring the size of the array as the compiler can automatically deduce
the size of the array in these cases.
Int a[ ] = {1,3,5,6,7};
Array Initialization after Declaration
int arr[10], i;
for( i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
Displaying values of arr[ 10]
for( i = 0; i < 10; i++)
printf(%d ", arr[i])
Access Array Elements
We can access any element of an array in C using the array subscript operator [ ] and
the index value of the element.
return 0;
}
Update Array Element
We can update the value of an element at the given index i in a similar way to accessing
an element by using the array subscript operator [ ] and assignment operator =.
array_name[i] = new_value;
a[3] = 200;
Array Traversal
#include<stdio.h>
int main(){
int i=0;
int marks[5]={20,30,40,50,60};
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
Calculating the Length of an Array
The length of an array is given by the number of elements stored in it. The general
formula to calculate the length of an array is
where upper_bound is the index of the last element and lower_bound is the index of the
first element in the array
Let Age[5] be an array of integers such that Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1,
Age[4] = 7
Calculating the Address of Array Elements
array name is a symbolic reference to the address of the first byte of the array. When we use
the array name, we are actually referring to the first byte of the array. Since an array stores
all its data elements in consecutive memory locations, storing just the base address, that is
the address of the first element in the array, is sufficient. The address of other data elements
can simply be calculated using the base address. The formula to perform this calculation is
Address of data element, A[k] = BA(A) + w(k – lower_bound)
Here, A is the array, k is the index of the element of which we have to calculate the address,
BA is the base address of the array A, and w is the size of one element in memory, for
example, size of int is 2
average = sum/n;
}
Write a program in C to read n number of values in an array and display them in reverse
order
printf("\n\nThe values store into the
#include <stdio.h>
array in reverse are :\n");
int main()
for(i=n-1;i>=0;i--)
{
{
int i,n,a[10];
printf("%d",a[i]);
}
n=10;
}
printf("Input %d number of elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
Write a program in C to print all unique elements in an array.
Write a program in C to copy the elements of one array into another array.