CH 6 deitel how to program
CH 6 deitel how to program
1. Introduction to Arrays
Definition: Arrays store multiple elements of the same type in a contiguous memory
block. Each element can be accessed via an index.
Use Cases:
o Store lists of numbers, characters, or objects (e.g., test scores, names, or
coordinates).
o Ideal for repetitive tasks like summing a series of values or searching for an
element.
Arrays solve the limitation of using multiple independent variables for related data.
Key Concept: All elements in an array are of the same data type, enabling consistent
operations on the elements.
2. Basic Concepts
Indexing:
o Arrays in C are zero-indexed.
o If arr is an array of size 5, valid indices are 0, 1, 2, 3, 4. Accessing arr[5]
results in undefined behavior.
Memory Layout:
o Arrays are stored contiguously in memory, which makes accessing elements
efficient but requires pre-allocated memory.
o Example:
c
Copy code
int arr[3] = {10, 20, 30};
// Memory layout:
// arr[0]: 10, arr[1]: 20, arr[2]: 30
Defining:
o Syntax:
c
Copy code
int arr[5]; // An array of size 5
o Arrays must have a fixed size during declaration (unless using variable-length
arrays in C99 or later).
Initialization:
o Initialization at declaration ensures all elements are assigned meaningful values.
o Examples:
c
Copy code
int arr[5] = {1, 2, 3}; // Remaining elements default to 0
float arr[] = {3.5, 4.5, 5.5}; // Compiler infers size as 3
Accessing:
o Use the array name and index to access elements.
o Example:
c
Copy code
int arr[5] = {10, 20, 30, 40, 50};
printf("%d", arr[2]); // Outputs 30
Manipulating:
o Assign new values or update existing ones.
o Example:
c
Copy code
arr[1] = arr[1] * 2; // arr[1] becomes 40
Arrays are always passed by reference (memory address is passed, not a copy of the
data).
Changes made to the array inside the function persist outside.
Example:
c
Copy code
void doubleValues(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
When doubleValues is called, the original array will have its values doubled.
6. Multi-Dimensional Arrays
Multi-dimensional arrays extend the concept to tables or matrices (2D), or even 3D grids.
2D Arrays:
o Declaration:
c
Copy code
int matrix[3][4]; // A 3x4 table
o Accessing elements:
c
Copy code
matrix[1][2] = 7; // Sets row 1, column 2 to 7
Higher Dimensions:
o You can define arrays with more dimensions, though they become harder to
manage:
c
Copy code
int cube[2][3][4]; // A 2x3x4 3D array
7. Common Operations
1. Summing Elements:
c
Copy code
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
2. Sorting:
o Sorting algorithms like Bubble Sort or Selection Sort are applied to organize array
elements.
o Example (Bubble Sort):
c
Copy code
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
3. Searching:
o Linear Search:
Iterate over the array until the target is found.
o Binary Search:
Requires a sorted array; repeatedly divides the search space in half.
c
Copy code
char name[20] = "John Doe";
printf("%s", name); // Outputs "John Doe"
Input:
c
Copy code
char input[50];
scanf("%s", input); // Reads a string
c
Copy code
int n;
scanf("%d", &n);
int arr[n]; // Array size is determined by user input
c
Copy code
int arr[5];
arr[10] = 20; // Out-of-bounds access, undefined behavior
c
Copy code
for (int i = 0; i < size; i++) {
printf("%d: ", arr[i]);
for (int j = 0; j < arr[i]; j++) {
printf("*");
}
printf("\n");
}
2. Data Analysis:
o Compute statistical measures like mean, median, and mode from an array.
This detailed explanation provides an enhanced understanding of arrays and their practical
applications. Practice these concepts through examples and exercises to solidify your knowledge.
questions
Definitions and Concepts
1. What is an array in C?
2. Define a one-dimensional array and give an example.
3. What is a multi-dimensional array? Provide a use case.
4. Explain the difference between an array and a variable.
5. What are character arrays, and how are they used for strings in C?
6. What is the significance of the '\0' character in strings?
7. Define variable-length arrays (VLAs). When are they used?
Passing Arrays
Multi-Dimensional Arrays
Operations on Arrays