Unit – 4
Arrays
Array is defined as a collection of elements of same data type. Hence it is also called as homogeneous variable. Total number of elements in array defines size of array. Position of element in an array defines index or subscript of particular element. Array index always starts with zero.
- One Dimensional Array:
The array which is used to represent and store data in a linear form is called as 'single or one dimensional array.'
Syntax:
data-typearray_name[size];
Example 2:
int a[3] = {2, 3, 5};
charch[10] = "Data" ;
float x[3] = {2.5, 3.50,489.98} ;
Total Size of an array(in Bytes) can be calculated as follows:
total size(in bytes) = length of array * size of data type
In above example, a is an array of type integer which has storage size of 3 elements. One integer variable occupies 2 bytes. Hence the total size would be 3 * 2 = 6 bytes.
Memory Allocation :
P1: Program to display all elements of 1-D array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
intn,i;
printf("\n Enter the size of array :");
scanf("%d",&n);
printf("size of array is %d",n);
printf("\n Enter Values to store in array one by one by pressing ENTER :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n The Values in array are..");
for(i=0;i<n;i++)
{
printf("\n %d",a[i]);
}
getch();
}
P2: Program to copy 1-D array to another 1-D array
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[100], b[100];// a is original array and b is copied array
printf("Enter the number of elements in array a\n");
scanf("%d", &n);
printf("Enter the array elements in a\n");
for (i= 0; i< n ; i++)
scanf("%d", &a[i]);
/*
Copying elements from array a into array b */
for (i =0;i<n;i++)
b[i] = a[i];
/*
Printing copied array b
.
*/
printf("Elements of array b are\n");
for (i = 0; i< n; i++)
printf("%d\n", b[i]);
getch();
}
Output
P3:Program to find reverse of 1-D array
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, j, a[100], b[100];// a is original array and b is reversed array
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (i= 0; i< n ; i++)
scanf("%d", &a[i]);
/*
* Copying elements into array b starting from end of array a
*/
for (i = n - 1, j = 0; i>= 0; i--, j++)
b[j] = a[i];
/*
Printing reversed array b
.
*/
printf("Reverse array is\n");
for (i = 0; i< n; i++)
printf("%d\n", a[i]);
getch();
}
Output:
2. Multi Dimensional Array
1-D array has 1 row and multiple columns but multidimensional array has of multiple rows and multiple columns.
Two Dimensional array:
Itis the simplest multidimensional array. They are also called as matrix.
Declaration:
data-typearray_name[no.of rows][no. of columns];
Total no of elements= No.of rows * No. of Columns
Example 3: inta[2][3]
This example illustrates two dimensional array a of 2 rows and 3 columns.
Total no of elements: 2*3=6
Total no of bytes occupied: 6( totalno.of elements )*2(Size of one element)=12
Initialization and Storage Representation:
In C, two dimensional arrays can be initialized in different number of ways. Specification of no. of columns in 2 dimensional array is mandatory while no of rows is optional.
int a[2][3]={{1,3,0}, // Elements of 1st row
{-1,5,9}}; // Elements of 2nd row
OR
int a[][3]={{1,3,0}, // Elements of 1st row
{-1,5,9}}; // Elements of 2nd row
OR
int a[2][3]={1,3,0,-1,5,9};
Fig. 3 gives general storage representation of 2 dimensional array
Eg 4. int a[2][3]={1,3,0,-1,5,9}
Accessing Two-Dimensional Array Elements:
An element in 2-dimensional array is accessed by using the subscripts i.e. row index and column index of the array. For example:
intval= A[1][2];
The above statement will access element from the 2nd row and third column of the array A i.e. element 9.
If the size of array is n then index ranges from 0 to n-1. There can be array of integer, floating point or character values. Character array is called as String.
Eg.1 A=
0 1 2 3 4
|
This example illustrates integer array A of size 5.
- Array index ranges from 0 to 4.
- Elements of array are {10, 20, 40, 5, 25}.
Arrays can be categorized in following types
- One dimensional array
- Multi Dimensional array
These are described below.
The abstract model of string is implemented by defining a character array data type and we need to include header file ‘string.h’, to hold all the relevant operations of string. The string header file enables user to view complete string and perform various operation on the string. E.g. we can call ‘strlen’ function to find length of string, ‘strcat’ to concatenate two string, ‘strcpy’ to copy one string to another, ‘strrev’ to reverse the string. The definitions of all these functions are stored in the header file ‘string.h’. So string functions with the user provided data becomes the new data type in ‘C’.
Fig. 3.1 : String data type
To use the functions related to strings, user only need to include ‘string.h’ header file. All the definitions details of these functions are keep hidden from the user, user can directly use these function without knowing all the implementation details. This is known as abstraction or hiding.
String Manipulation in C Language
Strings are also called as the array of character.
- A special character usually known as null character (\0) is used to indicate the end of the string.
- To read and write the string in C program %s access specifier is required.
- C language provides several built in functions for the manipulation of the string
- To use the built in functions for string, user need to include string.h header file.
- Syntax for declaring string is
Char string name[size of string];
Char is the data type, user can give any name to the string by following all the rules of defining name of variable. Size of the string is the number of alphabet user wants to store in string.
Example:
Sr. No. | Instructions | Description |
#include<stdio.h> | Header file included | |
2. | #include<conio.h> | Header file included |
3. | void main() | Execution of program begins |
4. | { | String is declare with name str and size of storing 10 alphbates |
5. | char str[10]; | |
6. | clrscr(); | Clear the output of previous screen |
7. | printf("enter a string"); | Print “enter a string” |
8. | scanf("%s",&str); | Entered string is stored at address of str |
9. | printf("\n user entered string is %s",str); | Print: user entered string is (string entered by user) |
10. | getch(); | Used to hold the output screen |
11. | } | Indicates end of scope of main function |
Following are the list of string manipulation function
Sr. No. | String Function | Purpose |
strcat | use to concatenate (append) one string to another | |
2. | Strcmp | use to compare one string with another. (Note: The comparison is case sensitive) |
3. | strchr
| use to locate the first occurrence of a particular character in a given string |
4. | Strcpy | use to copy one string to another. |
5. | Strlen | use to find the length of a string in bytes, |
Reference
- Brian W. Kernighan And Dennis M. Ritchie, The C Programming Language, Prentice Hall Of India
- YashwantKanetkar, Let Us C, Bpb Publication