UNIT - III
ARRAY : An array is a collection of data that holds fixed number of
values of same type. For example: if you want to store marks of 100 students,
you can create an array for it.
float marks[100];
OR
An array is a collection of one or more values of the same type. Each value is called an element
of the array. The elements of the array share the same variable name but each element has its
own unique index number (also known as a subscript). An array can be of any type, For
example: int, float, char etc. If an array is of type int then it's elements must be of type int
only.
The size and type of arrays cannot be changed after its declaration.
Arrays are of two types:
1. One-dimensional arrays
2. Two -dimensional arrays
declare an array
data_type array_name[array_size];
For example,
float mark[5];
Elements of an Array
You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.
Few key notes:
Arrays have 0 as the first index not 1. In this example, mark[0]
If the size of an array is n, to access the last element, (n-1) index is used. In this example,
mark[4]
Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d,
address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.
initialize an array in C programming
It's possible to initialize an array during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
Another method to initialize array during declaration:
int mark[] = {19, 10, 8, 17, 9};
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
Example: Arrays
// Program to find the average of n (n < 10) numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
1. One dimensional array in C:
Syntax : data-type arr_name[array_size];
Using Arrays
Elements of an array are accessed by specifying the index ( offset ) of the desired element
within square [ ] brackets after the array name.
Array subscripts must be of integer type. ( int, long int, char, etc. )
VERY IMPORTANT: Array indices start at zero in C, and go to one less than the size
of the array. For example, a five element array will have indices zero through four. This
is because the index in C is actually an offset from the beginning of the array. ( The first
element is at the beginning of the array, and hence has zero offset. )
Landmine: The most common mistake when working with arrays in C is forgetting that
indices start at zero and stop one less than the array size.
Arrays are commonly used in conjunction with loops, in order to perform the same
calculations on all ( or some part ) of the data items in the array.
Sample Programs Using 1-D Arrays
#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
printf("value of arr[%d] is %d \n", i, arr[i]);
}
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
Program Using 2-D Arrays:
Two dimensional array is nothing but array of array.
syntax : data_type array_name[num_of_rows][num_of_column];
#include<stdio.h>
int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}
Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40
Strings:
String is a group of characters is called as a String. Strings are defined as an array of characters.
The difference between a character array and a string is the string is terminated with a special
character ‘\0’.
Declaration of strings: Declaring a string is as simple as declaring a one dimensional array.
Below is the basic syntax for declaring a string.
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store. Please keep in mind that there
is an extra terminating character which is the Null character (‘\0’) used to indicate termination of
string which differs strings from normal character arrays.
Initializing a String: A string can be initialized in different ways. We will explain this with the
help of an example. Below is an example to declare a string with name as str and initialize it with
“GeeksforGeeks”.
1. char str[] = "GeeksforGeeks";
2. char str[50] = "GeeksforGeeks";
3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
Below is the memory representation of a string “Geeks”.
Let us now look at a sample program to get a clear understanding of declaring and initializing a
string in C and also how to print a string.
// C program to illustrate strings
#include<stdio.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s",str);
return 0;
}
Output:
Geeks