Converted Text
Converted Text
1. Arrays
An array is a collection of elements of the same data type, stored in contiguous memory
locations. Arrays are used to store multiple values in a single variable.
A. Characteristics of Arrays
Homogeneous: All elements of the array must be of the same data type (e.g., integers, floats).
Indexed: Array elements are accessed via indices, starting from 0 (zero-based index).
B. Types of Arrays
1.
One-dimensional Array (1D Array):
2.
Two-dimensional Array (2D Array):
A matrix or table of elements (rows and columns).
3.
Multi-dimensional Array:
Arrays with more than two dimensions, useful for storing complex data.
C. Array Operations
cpp
CopyEdit
for(int i = 0; i < 5; i++) { printf("%d ", arr[i]); }
Array Initialization:
Advantages:
Limitations:
A function is a block of code designed to perform a specific task. Functions help break down
programs into smaller, reusable, and organized pieces.
A. Characteristics of Functions
Return Type: The type of value the function will return (e.g., int, float, void).
B. Types of Functions
1.
Standard Library Functions:
2.
User-defined Functions:
cpp
CopyEdit
return_type function_name(parameters) { // Body of the function return value; // Only if the return
type is not void }
Example:
cpp
CopyEdit
D. Calling a Function
To call a function, use its name followed by the parameters (if any):
cpp
CopyEdit
cpp
CopyEdit
2.
Functions with Arguments and No Return Value:
cpp
CopyEdit
3.
Functions with Arguments and Return Value:
cpp
CopyEdit
Here is an example of using an array inside a function to find the sum of its elements:
cpp
CopyEdit
#include <stdio.h> int sumArray(int arr[], int size) { int sum = 0; for(int i = 0; i < size; i++) { sum +=
arr[i]; } return sum; } int main() { int arr[] = {1, 2, 3, 4, 5}; int result = sumArray(arr, 5); // Call the
function with the array printf("Sum of array elements: %d", result); return 0; }
4. Summary Table:
Concept Definition Example/Usage
Array Collection of elements of the same type int arr[] = {1, 2, 3};
Function Block of code that performs a specific task int add(int a, int b) { return a + b; }
Array Index Index used to access array elements arr[0] (first element)
Function Call Executing a function with arguments add(5, 3)
5. Conclusion
Arrays and functions are fundamental concepts in programming. Arrays allow us to handle
multiple data items, and functions provide modularity and reusability in code.