-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemJ1.cpp
More file actions
50 lines (41 loc) · 796 Bytes
/
Copy pathProblemJ1.cpp
File metadata and controls
50 lines (41 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Jingi Min
CIS 22A 2023 Fall
Laboratory Assignment J
ProblemJ1
Find the index of the target number
*/
#include <iostream>
using namespace std;
int main()
{
vector<int> vector;
vector = {2, 3, 5, 8, 13, 21, 34};
int result, target;
cout << "Input target you want to find: ";
cin >> target;
result = -1;
for (int i = 0; i < vector.size(); ++i)
{
if (vector[i] == target)
{
result = i;
}
}
if (result >= 0)
{
cout << "Target is at index " << result << endl;
}
else if (result == -1)
{
cout << "Can't find the target" << endl;
}
return 0;
}
/*
Execution results
Input target you want to find: 5
Target is at index 2
Input target you want to find: 7
Can't find the target
*/