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

lecture(3)

The document covers programming concepts related to dimensional arrays, including examples of searching and manipulating two-dimensional arrays. It introduces linear search as a method to find elements in an array, detailing the steps involved in the algorithm. Additionally, it includes homework assignments focused on array operations and modifications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lecture(3)

The document covers programming concepts related to dimensional arrays, including examples of searching and manipulating two-dimensional arrays. It introduces linear search as a method to find elements in an array, detailing the steps involved in the algorithm. Additionally, it includes homework assignments focused on array operations and modifications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Programming 2

Lecture 3: Dimensional Arrays

Example:
Write a program to search for an element in a two
dimensional array[3][4]

Prepared by T: Nahla Al-Mhfadi


HW 1: Write a program to print the smallest element in a two-
dimensional array[3][3].

Example: In this array


int grades[3][3]={{70,50,30},{46,43,90},{17,49,45}};
if the grade is between 44 and 50 convert it into 50.

HW 2: Read an array [3][2] ,then print the sum of each row of


the array.

Prepared by T: Nahla Al-Mhfadi


Linear Search
*Linear Search* is a straightforward algorithm used to find a
specific element in a list or array. It checks each element one
by one until it finds the target element or reaches the end of
the list. This method is simple to implement but can be
inefficient for large datasets because it may require examining
every element.
How Linear Search Works
1. *Start at the first element* of the array.
2. *Compare* the current element with the target value.
3. If they match, *return the index* of the current element.
4. If they do not match, move to the *next element* and
repeat the process.
5. If the end of the array is reached without finding the target,
return an indication that the element is not found (e.g., -1).
Example of Linear Search
Let's say we have the following array and we want to find the
number 6:
Array: [3, 5, 2, 8, 6, 1]
Target: 6
*Steps of the Linear Search:*
1. *Start at index 0*: Compare 3 with 6 (not a match).
2. *Move to index 1*: Compare 5 with 6 (not a match).
3. *Move to index 2*: Compare 2 with 6 (not a match).
4. *Move to index 3*: Compare 8 with 6 (not a match).
5. *Move to index 4*: Compare 6 with 6 (match found!).

Prepared by T: Nahla Al-Mhfadi


The target 6 is found at index 4. If the target were not in the
array, the search would continue until the end of the list.

Example:

Prepared by T: Nahla Al-Mhfadi

You might also like