An array search is simply a traversal through each of its elements until the desired element is found, or until all elements have been looked at. Since C style arrays offer no extra data along with their contents, it's simply a linear search of the array until either the entire array has been traversed, or the target value has been found.
When searching for a value, we start from index 0 and move through each of the array elements sequentially. Each array element we "visit" we compare to the value we are looking for, and if it is equal we then know that it has been found.
At this point we can choose to store this index and then terminate the loop using break.
#include <iostream>
int main()
{
int numbers[7] = {4, 9, 1, 7, 9, 3, 9};
int target = 9;
int found_at = -1;
// Increment the value before the expression uses it
for (int i = 0; i < 7; ++i)
{
if (numbers[i] == target)
{
found_at = i;
break;
}
}
if (found_at != -1)
{
std::cout << "First occurrence is at index " << found_at << '\n';
}
else
{
std::cout << "Value not present\n";
}
return 0;
}
If the loop continues to the end, without finding a match, we could use a sentinel value such as -1 and have this be what is stored, indicating that the target could not be found.
This special value is known as a sentinel, and it is a simple way of indicating the success or failure of a search without requiring any special language construct to differentiate between the two.
To find all of the values that match a target value, simply loop all the way through the array and each time we come across a match we add it to our list of indices.
Counting elements is simply an extension of searching. Instead of using the break keyword, we can just keep track of how many matches we find. We use a counter that is set at 0 initially.
Each time we find an element that matches, we increment the counter.
#include <iostream>
int main()
{
int scores[8] = {10, 20, 10, 30, 10, 40, 10, 50};
int target = 10;
int count = 0;
// Stop before the upper boundary is reached
for (int i = 0; i < 8; ++i)
{
if (scores[i] == target)
{
++count;
}
}
std::cout << "The value " << target << " appears " << count << " times\n";
return 0;
}
When we reach the end of the array, our counter will hold the number of elements that were found that were equal to the target value. The loop doesn't have to be specific to finding an equal value.
We could just as easily test to see if a number falls between a given range, is odd/even, or whatever else the given condition might be so long as it can be expressed using the C language's operators.
When updating the values in an array that match a given condition, we use the same traversal pattern, but instead of simply checking the elements we are going to perform a modification to them. Just like in the previous two cases, we examine the element at index and compare it to the required condition.
When the condition is met, we make an assignment to change the element at index to our desired replacement value.
#include <iostream>
int main()
{
int data[6] = {2, 5, 2, 8, 2, 1};
int old_value = 2;
int new_value = 99;
for (int i = 0; i < 6; ++i)
{
if (data[i] == old_value)
{
data[i] = new_value;
}
}
for (int i = 0; i < 6; ++i)
{
std::cout << data[i] << '\n';
}
return 0;
}
After the loop has completed, every element in the array that satisfied the condition has been replaced with the new value.
After the loop has completed, we can then perform a second loop that prints the new contents of the array. So long as we are accessing valid indices in the array (meaning less than the size), our assignment operation will perform correctly.
We are also able to perform selective assignments rather than just setting an entire element's value to another integer. We can choose to only assign to an index if it is within a specific range.
As with assignments, we will start from index 0, iterate through every index in the array until we've finished or encountered our exit condition, but if the element at the current index passes our test to see if it meets our condition, we will assign it a new value. We do not need any extra steps, because it is only after passing our test that we are updating the index.
#include <iostream>
int main()
{
int values[5] = {3, 12, 7, 15, 4};
for (int i = 0; i < 5; ++i)
{
if (values[i] > 10)
{
values[i] = values[i] - 10;
}
}
for (int i = 0; i < 5; ++i)
{
std::cout << values[i] << '\n';
}
return 0;
}
Searching, counting, and updating are all conceptually very similar processes. Every one of them requires a loop to start at index 0, and continue until the size of the array.
We must avoid attempting to use any index greater than or equal to the array size, or less than 0.
0 comments
No comments yet. Be the first.