Definition: An array is a collection of data items of the same type, accessed with a common name.
Data Structure: Holds a finite sequential collection of homogeneous data.
Collection: An array acts as a container for a collection of data.
Finite: The number of data items in an array is determined before use and is always finite.
Sequential: Data in the array is stored sequentially in memory.
Homogeneous: All data items in an array must share the same data type.
List of customers and their phone numbers.
Table of daily rainfall data.
List of employees in an organization.
Test scores of a class of students.
Syntax: type arrayName[arraySize];
Examples:
int group[10];
float height[50];
char name[15];
After declaring an array, its elements must be initialized.
Static Initialization: Initialize during declaration.
Dynamic Initialization: Initialize elements during program execution.
Defined using curly braces with values separated by commas.
Example: int marks[5] = {90, 86, 89, 76, 91};
Assign values to an array element during execution using the following syntax:
Syntax: array_name[index] = some_value;
Example: scanf("%d", &arr[i]);
Advantages:
Less lines of code for multiple elements.
Random access using array index.
Easy access and traversal of elements.
Simplified sorting.
Disadvantages:
Costly insertion and deletion due to memory management.
Fixed size defined at declaration; not dynamic like linked lists.
Syntax: datatype array_name[size];
Example: Declaration and access examples provided.
Specify array name followed by an index inside square brackets. Index starts at 0.
Example:
int a[5];
Elements: a[0], a[1], ... a[4]
A simplest form of multidimensional array, defined as an array of arrays.
Organized as matrices (rows and columns) for bulk data processing.
Syntax: datatype array_name[rows][columns];
Example: Can be declared with the type and size of rows and columns.
Two methods: conventional and better method using nested braces.
Example: int x[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
Definition: A matrix is a grid-like storage format that organizes data in rows and columns.
Basic operations include addition, subtraction, and multiplication along with user-defined orders.
Start Program.
Enter rows and columns of matrices.
Input elements for both matrices.
Perform multiplication and store in a third matrix.
Print final matrix.
Definition: Sequence of characters treated as a single data item, enclosed in double quotes. Example: "You are the best."
Strings are declared as character arrays.
Syntax: char string-name[size];
Strings are null-terminated; anything after is ignored.
Example: char str[20] = "Initial value";
Functions included in string.h
:
strlen()
: Returns length, excluding null character.
strcpy()
: Copies source string to destination.
strcmp()
: Compares two strings.
strcat()
: Concatenates two strings.
strrev()
: Reverses the string.
strlwr()
: Converts string to lowercase.
strupr()
: Converts string to uppercase.
strstr()
: Returns pointer to a substring.
Demonstrates the use of several string functions in C like length, reverse, concatenation, and comparison.
Understanding arrays and strings is fundamental in programming with C, enabling structured data storage and manipulation.