Start from the left
Compare one element at a time with target value 9.
C programming · Lesson
Compare the target with one cell at a time and keep the current index visible.
Linear search skips no elements: in the worst case it must inspect the whole array. The returned index identifies the position, not the value itself.
i=0 → v[i] == target? → no: i++ · yes: store i
The flowchart keeps algorithm control visible; the Array Lab below makes cells, indices, comparisons, candidates and swaps visible.
Indices stay visible at all times. Sorting traces highlight comparisons, the current candidate, swaps and the region already sorted.
Compare one element at a time with target value 9.
4 != 9: continue.
7 != 9: continue.
2 != 9: continue.
9 == 9: store index 3.
1 != 9: continue.
Value 9 is located at index 3.
Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.
1#include <stdio.h>
2
3int main(void)
4{
5 int v[5] = {4, 7, 2, 9, 1};
6 int cercato = 9;
7 int trovato = -1;
8
9 for (int i = 0; i < 5; i = i + 1)
10 {
11 if (v[i] == cercato)
12 trovato = i;
13 }
14
15 printf("%d\n", trovato);
16 return 0;
17}
Read five integers and then a target; print the first index or -1.
This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.
In {4, 7, 2, 9, 1}, at which index is 9 located?
Challenge 1 / 5 · Understanding
Progress: 0 / 5 · correct: 0
ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.
Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.