FPL Unit 4 AAB
FPL Unit 4 AAB
Arrays
CO4: To design a solution using Arrays, Character and
String Arrays
One-Dimensional Arrays
Definition :
A one-dimensional array in C is a sequence of elements of the same
type stored in contiguous memory locations. You can think of it as a
list of items where each item is accessed using a single index.
Declaration :
To declare a one-dimensional array in C, you specify the type of the
elements and the number of elements (size) the array will hold:
type name[size];
-Example :
int arr[5]; // Declares an array named 'arr' that can hold 5 integers
In this example, `int` is the type of elements, `arr` is the name of the
array, and `5` is the number of elements.
Arrays
Initialization :
Arrays in C can be initialized at the time of declaration or later. Here are the
common methods:
- At Declaration:
You can initialize an array with specific values when you declare it:
int arr[5] = {1, 2, 3, 4, 5}; // Initializes the array with the values 1 through 5
If you provide fewer initializers than the size, the remaining elements are
set to 0:
int arr[5] = {1, 2}; // Initializes arr[0] to 1, arr[1] to 2, and the rest to 0
If you omit the size, the compiler calculates it based on the number of
initializers:
int arr[5];
for(int i = 0; i < 5; i++)
{
arr[i] = i * 2; // Initializes each element with twice its
index
}
Two-Dimensional Arrays
Definition :
A two-dimensional array in C is essentially an array of arrays. It can be
visualized as a matrix or table with rows and columns. Each element
is accessed using two indices: one for the row and one for the
column.
Declaration :
To declare a two-dimensional array, specify the type of the elements,
the number of rows, and the number of columns:
type name[rows][columns];
Example:
int matrix[3][4]; // Declares a 2D array with 3 rows and 4 columns
-At Declaration:
You can initialize a 2D array with specific values directly:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
}; // Initializes the array with the specified values
If fewer values are provided, the remaining
elements are set to 0:
int matrix[3][4] = {
{1, 2},
{3, 4},
{5} // Initializes the first two rows with specified
values and the last row with the remaining elements
set to 0
};
Default Initialization:
If the array is declared but not explicitly initialized, all elements are
set to 0:
Post-Declaration Initialization:
You can initialize elements individually or via a loop:
int matrix[3][4];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
matrix[i][j] = i + j; // Initializes each element with the sum of its
indices
}
}
Summary
One-Dimensional Arrays:
Declaration: `type name[size];` (e.g., `int arr[5];`)
Initialization: At declaration (e.g., `int arr[5] = {1, 2, 3, 4,
5};`) or later using loops.
Two-Dimensional Arrays:
Declaration : `type name[rows][columns];` (e.g., `int
matrix[3][4];`)
Initialization : At declaration (e.g., `int matrix[3][4] =
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};`), default to 0, or
using loops.
Character Arrays and Strings
Declaration :
To declare a string in C, you use a character array. The size of the
array should be sufficient to hold all characters including the null
terminator.
Example :
char str[50]; // Declares a character array with space for 50
characters
Initialization :
Strings can be initialized in several ways:
At Declaration:
Using `scanf`:
char str[50];
scanf("%s", str); // Reads a string from the terminal until a space or newline is
encountered
Using `fgets` :
char str[50];
fgets(str, sizeof(str), stdin); // Reads a string including spaces, up to a newline
or the buffer size
Example:
Example:
#include <string.h>
Example:
#include <string.h>
Return Values:
- `0`: Strings are equal.
- Negative value: `str1` is less than `str2`.
- Positive value: `str1` is greater than `str2`.
6. Introduction to String Handling Functions
The C Standard Library provides several functions to handle strings, declared in
`<string.h>`:
`strlen`: Computes the length of a string (excluding the null terminator).
#include <string.h>
char str[] = "Hello";
size_t len = strlen(str); // len is 5
int main() {
// Declare matrices and dimensions
int A[2][3] = {{1, 2, 3}, {4, 5, 6}};
int B[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int C[2][2] = {0}; // Resultant matrix initialized to zero
int rowsA = 2, colsA = 3, rowsB = 3, colsB = 2;