Notes
Notes
An array is a data structure that is a collection of variables of one type that are accessed
through a common name. Each element of an array is given a number by which we can
access that element which is called an index. It solves the problem of storing a large
number of values and manipulating them.
Elements are all of the same data type, such as an integer , float, character
Characteristics of an array:
1. An array is always stored in consecutive memory location.
2. It can store multiple value of similar type, which can be referred with single name.
3. The pointer points to the first location of memory block, which is allocated to the array name.
4. An array can either be an integer, character, or float data type that can be initialised
Advantage of C Array:
1. In an array, accessing an element is very easy by using the index number.
2. The search process can be applied to an array easily.
3. 2D Array is used to represent matrices.
4. For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.
Types of Array:
• One dimensional array:
One-dimensional array in C as a single row to store the elements. All the elements are stored at contiguous
memory locations.
Syntax:
data_type array_name[size];
int arr[5];
Initialization :
int nums[5] = {0, 1, 2, 3, 4};
int marks[]={10,2,0,23,4};
Here the size of array marks is initialized to 5.
• Array Accessing:
• <arr_name>[index];
printf("%d", nums[0]);
Two dimensional :
A 2D array is also known as a matrix (a table of rows and columns).
To create a 2D array of integers, take a look at the following example:
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
The first dimension represents the number of rows [2], while the second dimension represents the
number of columns [3]. The values are placed in row-order, and can be visualized like this:
Note:
The rule of initialization of multidimensional arrays is that last subscript varies most frequently and the first subscript varies
least rapidly.
Operations on array- Insertion:
For example consider an array n[10] having four elements:
n[0] = 1, n[1] = 2, n[2] = 3 and n[3] = 4
And suppose you want to insert a new value 60 at first position of array. i.e. n[0] = 60, so we have to move
elements one step below so after insertion
n[1] = 1 which was n[0] initially, n[2] = 2, n[3] = 3 and n[4] = 4.
#include <stdio.h> printf("Please enter the value\n");
int main() scanf("%d", &value);
{
int array[50], position, c, n, value; for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
printf("Enter number of elements in the array\n");
scanf("%d", &n); array[position-1] = value;
printf("Please enter the location where you want to insert an new element\n"); return 0;
scanf("%d", &position); }
Operations on array deletion:
}
Array applications - Finding maximum and minimum
mx = arr1[0];
#include <stdio.h> mn = arr1[0];
void main()
{ for(i=1; i<n; i++)
{
int arr1[100]; if(arr1[i]>mx)
int i, mx, mn, n; {
printf("\n\nFind maximum and minimum element in an mx = arr1[i];
array :\n"); }
printf("--------------------------------------------------\n");
printf("Input the number of elements to be stored in the if(arr1[i]<mn)
array :"); {
scanf("%d",&n); mn = arr1[i];
}
printf("Input %d elements in the array :\n",n);
}
for(i=0;i<n;i++) printf("Maximum element is : %d\n", mx);
{ printf("Minimum element is : %d\n\n", mn);
}
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
Counting occurrences of particular element in an array
#include <stdio.h> printf("Enter number to find Occurrence: ");
scanf("%d", &num);
#define MAX 100
Output:
//read array elements Input array elements:
printf("Enter array elements:\n"); 10, 10, 20, 30, 10
Element to find occurrence: 10
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1); Output:
scanf("%d", &arr[i]); Occurrence of 10 is: 30
}
C String Functions
There are many important string functions defined in "string.h" library.
C gets() function
The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in
a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings.
It returns the string entered by the user.
Reading string using gets()
#include<stdio.h>
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
C puts() function
• The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is
previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of
characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to
the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the
string plus 1.
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name); //reads string from user
printf("Your name is: ");
puts(name); //displays string
return 0;
}