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

CH 6 deitel how to program

Uploaded by

rabiasultan24277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
916 views

CH 6 deitel how to program

Uploaded by

rabiasultan24277
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CH 6

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

3. Defining and Initializing Arrays

 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

4. Accessing and Manipulating Elements

 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

5. Passing Arrays to Functions

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

This computes the sum of all elements in the array.

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.

8. Character Arrays and Strings

 Strings in C are character arrays ending with a null character ('\0').


 Example:

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

(Note: Use fgets for secure input handling.)

9. Variable-Length Arrays (VLAs)

 VLAs allow the size of an array to be determined at runtime (introduced in C99).


 Example:

c
Copy code
int n;
scanf("%d", &n);
int arr[n]; // Array size is determined by user input

10. Secure Programming with Arrays


 Always ensure that array indices are within bounds to avoid accessing unintended
memory, leading to undefined behavior or vulnerabilities.
 Example of a potential issue:

c
Copy code
int arr[5];
arr[10] = 20; // Out-of-bounds access, undefined behavior

11. Applications and Case Studies

1. Bar Graph Representation:


o Use arrays to store data points and represent them as a bar graph.
o Example:

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?

Initialization and Access

8. Write the syntax to define an array.


9. How do you initialize an array in C? Provide an example.
10. What happens if an array is partially initialized?
11. How are individual elements of an array accessed?
12. How can you use a loop to iterate through all elements of an array?

Passing Arrays

13. How are arrays passed to functions in C?


14. What is the difference between passing an array and passing a variable to a function?

Multi-Dimensional Arrays

15. Write the syntax to define a two-dimensional array.


16. Explain the memory layout of a 2D array in C.
17. How do you initialize a 2D array during declaration?

Operations on Arrays

18. Write a program to compute the sum of all elements in an array.


19. How would you sort an array in ascending order? Provide the algorithm name.
20. What is the difference between linear search and binary search?
21. Write the steps for performing a binary search on an array.

Strings and Character Arrays

22. How are strings stored in C?


23. What is the purpose of the scanf function when working with character arrays?
24. Why is fgets preferred over scanf for reading strings?

You might also like