0% found this document useful (0 votes)
12 views13 pages

ITP Week 5

Arrays are collections of fixed-sized elements of the same data type stored contiguously in memory, allowing for efficient data management and operations. The document covers array declaration, initialization, accessing elements, and common issues such as out-of-bounds indexing. It also introduces fundamental concepts like sorting and searching algorithms, particularly focusing on linear search and selection sort.

Uploaded by

Hashem Abdullah
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)
12 views13 pages

ITP Week 5

Arrays are collections of fixed-sized elements of the same data type stored contiguously in memory, allowing for efficient data management and operations. The document covers array declaration, initialization, accessing elements, and common issues such as out-of-bounds indexing. It also introduces fundamental concepts like sorting and searching algorithms, particularly focusing on linear search and selection sort.

Uploaded by

Hashem Abdullah
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/ 13

Summary of Arrays

Introduction
Arrays are a collection of fixed-sized elements of the same data type stored
contiguously in memory.

Declaration and Initialization


• An array is declared using the syntax type arr[size];, where arr is the
name of the array and size is its size.
• For example, int arr[10]; declares an integer array with 10 elements.
• The elements are initialized by assigning values to each index of the array.

Importance and Advantages


• Arrays provide easier storage, access, and management of data.
• They allow for efficient searching of elements in a collection.
• Arrays enable easy organization of elements in ascending or descending
order.
• They serve as a basis for performing matrix operations in programming
languages.
• Arrays are useful in databases and can be used to implement other data
structures such as stacks, queues, trees, heaps, etc.

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 calculate_average(int arr[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum / size;
}

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 squarer(int square_arr[], int size) {


double sum = 0.0;
for (int i = 0; i < size; i++) {
// Square each element and accumulate the sum
sum += pow(square_arr[i], 2);
}
return sum / size;
}

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.

How Linear Search Works


1. Start at Index 0: Begin searching from the first element of the array.
2. Compare with Target Element: Check if the current element matches
the target element (the value that needs to be found).
3. Move Forward or Backward: If the elements match, stop and return
the index as a success indicator.
4. Exhaust All Elements: Iterate through all array indices until a match
is found.

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>

int binary_search(int arr[], int n, int target) {


// Initialize two pointers for searching range
int left = 0;
int right = n - 1;

while (left <= right) {


// Calculate mid-point of current search range
int mid = left + (right - left) / 2;

// Check if target is present at index 'mid'


if (arr[mid] == target)
return mid; // Return successful index

// If x is greater, ignore left half


else if (arr[mid] < target)
left = mid + 1;

// If x is smaller, ignore right half


else
right = mid - 1;
}

// 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;

printf("Enter the element to search (target): ");


scanf("%d", &target);

if (binary_search(arr, n, target) != -1)


printf("Element found at index %d\n", binary_search(arr, n, target));
else
printf("Element not found in array.\n");

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.

Why Sort Data?


The primary advantage of sorting is reduced lookup time. In other words, when
data is sorted, it becomes easier to locate specific information within the dataset.
A well-organized dataset can significantly improve search efficiency and overall
productivity.

Example: Telephone Directory


Consider a telephone directory with thousands of people’s phone numbers ran-
domly arranged. Without organization, searching for someone’s number would
be a daunting task. However, if the directory is sorted alphabetically by name,
locating a specific person becomes much simpler and more efficient.
Common Sorting Algorithms
Some popular sorting algorithms include:
• Selection Sort: A simple algorithm that works by repeatedly finding the
minimum element from unsorted part of data.
• Bubble Sort: An easy-to-understand algorithm that works on the prin-
ciple of swapping adjacent elements if they are in wrong order.
• Insertion Sort: A stable sorting algorithm that works well for small data
sets and is used to sort array by one or more criteria.
These algorithms will be discussed in detail throughout this course, with a focus
on the selection sort algorithm.

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>

// Function to perform Selection Sort


void selectionSort(int arr[], int n) {
// Iterate over each element in the array
for (int i = 0; i < n - 1; i++) {
// Initialize min index to store the index of minimum element found so far
int minIndex = i;

// Find the minimum element in the unsorted part of the array


for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

// 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;
}
}

// Main function to demonstrate Selection Sort implementation


int main() {
int n = 5; // Number of elements in the array

// Create an example array


int arr[] = {64, 34, 25, 12, 22};

printf("Original Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Call the selectionSort function to sort the array

10
selectionSort(arr, n);

// Print the sorted array


printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\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.

Example 1: Character Array with Fixed Size


char color[3] = {'a', 'b', 'c'};
In this example, the array color has a fixed size of 3 elements. Note that there
is no null character at the end of the array.

Example 2: Automatic Null Character Addition


When assigning values to a character array:
char name[50];
name[0] = getchar();
// ...
The compiler automatically adds a null character ('\0') at the end of the array,
making it a string. This is because strings in C must have a null character as
their last element.

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

Example 4: Reading and Printing Characters using scanf and printf


Instead of using getchar and putchar, use scanf to read characters from the
user:
#include <stdio.h>
#include <ctype.h>

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.

Using Array Elements


• We saw how arrays can perform computational tasks, such as inserting
and deleting elements, reversing an array without using a second array,
and sorting data using selection sort algorithms.

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.

Linear Search Algorithm


• We implemented linear search algorithms to find elements in an array,
showcasing a fundamental technique for searching data structures.

Selection Sort Algorithm


• The selection sort algorithm was introduced as a simple yet effective
method for arranging array elements in increasing or decreasing order.
• This algorithm has applications beyond sorting arrays, such as in decision-
making and priority management.

13

You might also like