ITP Week 5
ITP Week 5
Introduction
Arrays are a collection of fixed-sized elements of the same data type stored
contiguously in memory.
Key Points
• Fixed-sized sequence of elements • Elements stored contiguously in memory •
Easy storage, access, and management of data • Efficient searching of elements
• Basis for matrix operations
The provided text is a script for a tutorial on arrays in C programming language.
The tutorial explains how to initialize and access arrays, as well as perform
operations on them.
Here’s a summary of the main points covered in the tutorial:
1. Initializing Arrays: The tutorial discusses two ways to initialize arrays:
• Using curly braces {} after the data type declaration.
• By explicitly specifying the size of the array when declaring it.
2. Accessing Array Elements: The tutorial explains how to access elements
of an array using their index (position in the array).
3. Example Program: An example program is provided that takes 10 integer
inputs from the user and computes their sum.
Some key takeaways from the tutorial include:
• Arrays are declared with a data type, followed by square brackets []
containing the size or number of elements.
1
• Elements of an array can be accessed using their index (0-based).
• Array indices start at 0 and end at n-1 for an n-element array.
Overall, the tutorial provides a clear introduction to arrays in C programming
language.
Here’s a concise summary of the script:
Arrays in Memory
• Arrays are stored in contiguous locations in memory.
• Each location has an address, represented by a binary string (combination
of 1s and 0s).
• The difference between consecutive addresses is always 1.
Storage Layout for Integer Array
• An integer array of size 4 requires 8 bytes of memory (4 x 2 bytes per
element).
• In modern computers, this would typically be stored in a contiguous block
of memory.
• Each element is stored in the next available byte after the previous one.
• The first element (arr[0]) is stored at address arr for 2 bytes, then arr+1,
and so on.
Address Representation
• In modern computers, addresses are either 32-bit or 64-bit, but we’ll
consider it as an 8-bit binary string for simplicity.
• The size of integer variables affects the storage layout: 2 bytes per element
(e.g., int) vs. 4 bytes per element (e.g., float).
Overall, arrays are stored in contiguous memory locations with addresses corre-
sponding to each element in the array.
Here’s a concise summary of the program:
Arrays with Initial Values
• An array can be initialized with specific values.
• The sizeof operator returns the number of bytes occupied by an array or
its elements.
Printing Array Elements
• To print all elements in an array, use a loop that iterates over the size of
the array using sizeof.
• In this example, we initialize an array with some values and then use a
loop to print each element separated by tabs.
• The output shows the initial values stored in the array.
Incomplete Arrays
2
• If only part of an array is initialized, undefined values will be used for
remaining elements.
• This behavior can lead to unexpected results or errors if not handled
carefully.
Undeclared Arrays
• An array that is declared without specifying its size can still be initialized
with a specific number of elements.
• However, attempting to assign more elements than the declared size leads
to a compilation error.
Invalid Assignments
• Attempting to assign values to an array using arr[size] = value results
in a compile-time error if the last element index is out of bounds.
• Storing six integers in a single memory location is not allowed and causes
a syntax error.
Here’s a concise summary of the program:
Array Indexing Issues
1. Out-of-bounds indexing: When accessing arrays beyond their declared
size, results become unpredictable and may lead to runtime errors.
2. Assigning values outside array bounds: Assigning values to indices
greater than or equal to an array’s size can result in undefined behavior.
Example 1:
int arr[6];
for (int i = 0; i < 9; i++) {
printf("%d\t", arr[i]);
}
Output: First six elements of arr and junk values beyond that due to out-of-
bounds indexing.
Assigning more values than array size
Example 2:
int arr[6];
for (int i = 0; i < 8; i++) {
printf("%d\t", arr[i]);
}
Output: First six elements of arr and undefined behavior due to out-of-bounds
indexing.
Initializing array with a single value
Example 3:
3
int arr[6] = {0};
printf("%d\t", arr[6]); // Output: 0 (initialization) followed by segmentation fault due to
Output: Prints 0 for the first element, followed by undefined behavior.
Copying elements from one array to another
Example:
int old_arr[5] = {1, 2, 3, 4, 5};
int new_arr[5];
for (int i = 0; i < 5; i++) {
new_arr[i] = old_arr[i]; // Correct way to copy elements individually.
}
Output: Copies all five elements from old_arr to new_arr.
In summary, when working with arrays in C, it’s essential to:
• Be aware of array indexing and avoid accessing indices beyond the declared
size.
• Avoid assigning values outside an array’s bounds.
• Initialize arrays correctly using a single value or properly allocating space
for all elements.
Best Practice
When copying elements from one array to another:
int old_arr[5] = {1, 2, 3, 4, 5};
int new_arr[5];
for (int i = 0; i < sizeof(new_arr) / sizeof(new_arr[0]); i++) {
new_arr[i] = old_arr[i]; // Copy elements individually.
}
Summary: Arrays with Functions
Key Takeaways:
• Arrays are passed by Reference: When an array is passed to a func-
tion, it is copied into the stack frame allocated for that function. Any
changes made to the array within the function affect the original array in
the caller.
• Passing arrays as arguments: The size of an array must be specified
when passing it as an argument to a function.
• Returning Arrays from Functions: If an array is declared inside a
function, attempting to return it will result in a compilation error. This
is because the memory allocated for that array is destroyed when the
function returns.
4
Example Use Cases:
1. Calculating Array Averages:
• Declare an integer array arr with a specified size.
• Call a function calculate_average(arr) to compute and return the
average of all elements in the array.
2. Squaring Elements in an Array:
• Create an integer array square_arr with initial values (e.g., 1, 2, 3).
• Define a function squarer(arr) that squares each element in the
input array and returns the resulting average.
3. Returning Arrays from Functions: This is not recommended due to
memory management issues.
Code Snippets:
Calculating Array Averages
#include <stdio.h>
int main() {
int SIZE = 3;
int arr[SIZE] = {1, 2, 3};
printf("Array average: %d\n", calculate_average(arr, SIZE));
return 0;
}
Squaring Elements in an Array
#include <stdio.h>
int main() {
5
int SIZE = 3;
int square_arr[SIZE] = {1, 2, 3};
printf("Squared array average: %f\n", squarer(square_arr, SIZE));
return 0;
}
These examples demonstrate the basic usage of arrays with functions in C.
Linear Search Algorithm: A Step-by-Step Explanation
Overview
The linear search algorithm is a simple and straightforward technique used to
find an element in a sorted or unsorted array. In this explanation, we will break
down the process into smaller steps and provide sample code for implementation.
Example Walkthrough
Suppose we have an array arr with values [1, 3, 5, 7, 9], and we want to
search for the value 8. Here’s how the linear search algorithm would execute:
• Iteration 1: Start at index 0.
– Compare arr[0] = 1 with target element 8: No match.
– Move forward to index 1.
• Iteration 2: Compare arr[1] = 3 with target element 8: No match.
– Move forward to index 2.
• Iteration 3: Compare arr[2] = 5 with target element 8: No match.
– Move forward to index 3.
• Iteration 4: Compare arr[3] = 7 with target element 8: No match.
– Move forward to index 4.
• Iteration 5: Compare arr[4] = 9 with target element 8: Still no match.
At this point, we’ve exhausted all elements in the array without finding a match.
Therefore, the linear search algorithm indicates that the value 8 is not present
in the array.
Code Implementation
Here’s an example code implementation of the linear search algorithm in C:
6
#include <stdio.h>
// Element not found after comparing the array length with current "left" index
return -1; // Return element not found value
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
return 0;
}
In this code:
• We first initialize two pointers left and right to represent the search
7
range.
• Then we iterate using a while loop until left > right.
• Inside the loop, we calculate the midpoint of our current search range (mid)
and compare it with our target element. If there’s a match, we return its
index.
• Based on whether the middle value is smaller or larger than our target,
we decide to either ignore left half (if arr[mid] < target) or right half
(if arr[mid] > target).
• Finally, if no element matches after exhausting all comparisons, we return
-1, indicating that the search failed.
This C code demonstrates how you can implement a binary search algorithm
for finding specific elements within sorted arrays.
Sorting: A Fundamental Concept in Data Organization
What is Sorting?
Sorting refers to the process of arranging data in an increasing or decreasing
order. This can be applied to various types of data, including numbers, records,
or names.
8
Selection Sort Algorithm: A Step-by-Step Explanation
The Selection Sort algorithm is a simple sorting technique that works by repeat-
edly finding the minimum element from an unsorted portion of the array and
swapping it with the first element in that portion.
Key Steps:
1. Identify Unsorted Portion: Start by identifying the unsorted part of
the array.
2. Find Minimum Element: Find the smallest (minimum) element within
this unsorted portion of the array.
3. Swap Elements: Swap this minimum element with the first element in
the unsorted portion.
Example Walkthrough:
Consider an example array: [14, 27, 10, 19].
1. Identify Unsorted Portion: The entire array is considered as one unsorted
part initially.
2. Find Minimum Element: In this case, 10 happens to be the minimum
element in the unsorted portion of the array ([14, 27, 10, 19]). Swap
it with the first element at index zero [14 -> 10) and [27->19]. Thus we
have our sorted part as [10, 19] and Unsorted part is [14,27]
3. Find Minimum Element: In this remaining unsorted portion of array
which is [14,27], find minimum element among these two i.e., 14. Swap
it with the first element at index zero. Thus we have our sorted part as
[10,19,14] and Unsorted part is now [27]
4. Find Minimum Element: In this remaining unsorted portion of array
which is [27], find minimum element among these two i.e., 27. Swap
it with the first element at index zero [the only one]. Thus we have our
sorted part as [10,19,14,27] and Unsorted part is now [none]
5. Find Minimum Element: In this remaining unsorted portion of array
which is [none], find minimum element among these two i.e., None. Swap
it with the first element at index zero.
After completing all steps:
• Sorted Array: [10,19,14,27]
• Unsorted Area: [none] This array can now be considered as completely
sorted.
Selection Sort Algorithm: Implementation and Explanation
The Selection Sort algorithm is a simple sorting technique that works by repeat-
edly finding the minimum element from an unsorted portion of the array and
swapping it with the first element in that portion.
9
Key Steps:
1. Find Minimum Element: Identify the minimum element within the
unsorted part of the array.
2. Swap Elements: Swap the found minimum element with the last element
of the sorted part of the array.
C Code Implementation
#include <stdio.h>
// Swap the found minimum element with the last element of the sorted part of the ar
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
10
selectionSort(arr, n);
return 0;
}
Explanation
1. Find Minimum Element: In each iteration, the algorithm iterates over
the unsorted part of the array to find the minimum element.
2. Swap Elements: The found minimum element is swapped with the last
element of the sorted portion of the array.
Example Output:
Original Array: 64 34 25 12 22
Sorted Array: 12 22 25 34 64
Character Arrays in C
A character array is a data type that stores characters as elements, similar to
strings. In this section, we will explore examples of using character arrays and
string representation.
11
Example 3: Converting Characters to Uppercase
To convert characters to uppercase, use the toupper function from the ctype.h
library:
#include <stdio.h>
#include <ctype.h>
int main() {
char name[50];
scanf("%49s", name); // read a string with space for null character
for (int i = 0; name[i] != '\0'; i++) {
printf("%c", toupper(name[i]));
}
return 0;
}
int main() {
char name[50];
printf("Enter a string: ");
scanf("%49s", name);
for (int i = 0; name[i] != '\0'; i++) {
printf("%c", toupper(name[i]));
}
return 0;
}
Note that when using scanf, always specify the maximum number of characters
to read, including space for the null character.
Summary of Array Concepts
In this module, we covered key aspects of arrays in C programming:
Defining Arrays
• We learned how to define arrays using square brackets [] and initialize
their elements.
• Arrays can be used to store collections of related data objects together.
12
Initializing Arrays
• We explored ways to initialize array elements at the time of declaration or
later by assigning values to individual indices.
• Understanding when initialization should occur is crucial for writing effi-
cient code.
Memory Representation
• A deeper understanding of program memory was gained when we learned
about the storage layout of arrays.
• Recognizing the size and scope of variables is essential for managing code
effectively.
13