0% found this document useful (0 votes)
32 views

Notes

Arrays allow storing and manipulating multiple values of the same type, with each element accessed via an index. Elements are stored contiguously in memory and arrays can be one, two, or multidimensional. Arrays are useful for storing tabular data, performing searches efficiently, and representing matrices through 2D arrays.

Uploaded by

Yagnam Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Notes

Arrays allow storing and manipulating multiple values of the same type, with each element accessed via an index. Elements are stored contiguously in memory and arrays can be one, two, or multidimensional. Arrays are useful for storing tabular data, performing searches efficiently, and representing matrices through 2D arrays.

Uploaded by

Yagnam Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Arrays :

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:

outputs all elements in the matrix array:


Access the Elements of a 2D Array Example
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} }; int i, j;


for (i = 0; i < 2; i++)
printf("%d", matrix[0][2]); // Outputs 2 {
for (j = 0; j < 3; j++)
{
printf("%d\n", matrix[i][j]);
}
}
Multidimensional Array

More than 2-dimensional arrays are treated as multidimensional


arrays.
Example:
int a[2][3][4];
Here a represents two 2-dimensional arrays and each of these 2-d
arrays contains 3 rows and 4 columns.
Initialization:
int a[2][4][3]={ { {1,2,3}, {4,5}, {6,7,8}, {9} },
{ {10,11}, {12,13,14},{15,16}, {17,18,19} } }

The values of elements after this initialization are as:

a[0][0][0]:1 a[0][0][1]:2 a[0][0][2]:3 a[0][1][0]:4 a[0][1][1]:5 a[0][1][2]:0


a[0][2][0]:6 a[0][2][1]:7 a[0][2][2]:8
a[0][3][0]:9 a[0][3][1]:0 a[0][3][2]:0
a[1][0][0]:10 a[1][0][1]:11 a[1][0][2]:0 a[1][1][0]:12 a[1][1][1]:13 a[1][1][2]:14 a[1][2][0]:15 a[1][2][1]:16 a[1][2][2]:0
a[1][3][0]:17 a[1][3][1]:18 a[1][3][2]:19

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("Enter %d elements\n", n); printf("Resultant array is\n");

for (c = 0; c < n; c++) for (c = 0; c <= n; c++)


scanf("%d", &array[c]); printf("%d\n", array[c]);

printf("Please enter the location where you want to insert an new element\n"); return 0;
scanf("%d", &position); }
Operations on array deletion:

if array is containing five elements and you want to delete element at


position •six which is not possible.
if ( position >= n+1 )
#include <stdio.h> printf("Deletion not possible.\n");

int main() else


{ {
int array[100], position, c, n; for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Enter number of elements in array\n");
scanf("%d", &n); printf("Resultant array is\n");

printf("Enter %d elements\n", n); for( c = 0 ; c < n - 1 ; c++ )


printf("%d\n", array[c]);
for ( c = 0 ; c < n ; c++ ) }
scanf("%d", &array[c]); return 0;
}
printf("Enter the location where you wish to delete element\n");
Searching:
1. Read the array size and store that value into the variable n.
2. Read the entered elements and store those elements into an array a[] using
scanf statement and the for loop. scanf(“%d”,&a[i]) indicates scanf statement reads the
entered element and assigned that element to a[i].
3. Read the key which we want to search in an array.
4. Compare the key with each element of the array as a[i]==key and print element
found, if the key matches with any element of the array otherwise print element not
found.
Searching for(i=0; i<n; i++)
{
#include<stdio.h> if(a[i]==element)
int main() {
{ printf("%d found at position %d", element, i+1);
int a[100], n, element, pos=0; }
int i; else
printf("%d not found.", element);
printf("Enter array size [1-100]: ");
scanf("%d", &n); }

printf("Enter array elements: "); return 0;


for(i=0; i<n; i++)scanf("%d", &a[i]); }

printf("Enter element to search: ");


scanf("%d",&element);
Sorting
Create an array of fixed size (maximum capacity), lets say 10.
2. Take n, a variable which stores the number of elements of the array, less than maximum capacity of array.
3. Iterate via for loop to take array elements as input, and print them.
4. The array elements are in unsorted fashion, to sort them, make a nested loop.
5. In the nested loop, the each element will be compared to all the elements below it.
6. In case the element is greater than the element present below it, then they are interchanged
7. After executing the nested loop, we will obtain an array in ascending order arranged elements.

#include <stdio.h> if (number[i] > number[j])


void main() {
{
a = number[i];
int i, j, a, n, number[30]; number[i] = number[j];
printf("Enter the value of N \n"); number[j] = a;
scanf("%d", &n);
}
printf("Enter the numbers \n");
for (i = 0; i < n; ++i) }
scanf("%d", &number[i]);
for (i = 0; i < n; ++i) }
{
printf("The numbers arranged in ascending order are given below \n");
for (j = i + 1; j < n; ++j) for (i = 0; i < n; ++i)
{ printf("%d\n", number[i]);

}
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

//count occurance of num


int main() count = 0;
{ for (i = 0; i < n; i++) {
if (arr[i] == num)
int arr[MAX], n, i;
count++;
int num, count; }
printf("Occurrence of %d is: %d\n", num, count);
printf("Enter total number of elements: "); return 0;
}
scanf("%d", &n);

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.

No. Function Description


1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to destination string.
3) strcat(first_string, second_string) concats or joins first string with second string. The result
of the string is stored in first string.
4) strcmp(first_string, second_string) compares the first string with second string. If both
strings are same, it returns 0.
5) strrev(string) returns reverse string.
6) strlwr(string) returns string characters in lowercase.
7) strupr(string) returns string characters in uppercase.
C gets() and puts() functions
The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings.

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;
}

You might also like