Skip to content

Commit 4573808

Browse files
author
Carlos Leonard
committed
update
1 parent 0feb03a commit 4573808

File tree

11 files changed

+87
-6
lines changed

11 files changed

+87
-6
lines changed

60_ClosestEnemy.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// For this challenge you will search in an array for an enemy.
2+
/*
3+
have the function ClosestEnemy(arr) take the array of numbers stored in arr and from the position in the array where a 1 is, return the number of spaces either left or right you must move to reach an enemy which is represented by a 2. For example: if arr is [0, 0, 1, 0, 0, 2, 0, 2] then your program should return 3 because the closest enemy (2) is 3 spaces away from the 1. The array will contain any number of 0's and 2's, but only a single 1. It may not contain any 2's at all as well, where in that case your program should return a 0.
4+
*/
5+
6+
#include <iostream>
7+
#include <algorithm>
8+
using namespace std;
9+
10+
11+
/*
12+
Locate the one index
13+
iterate to the left and search for any enemies
14+
If found keep track of its distance
15+
iterate to the right and search for any enemies and keep distance
16+
report the closest enemy
17+
if none found return 0
18+
*/
19+
20+
int ClosestEnemy(int arr[], int size)
21+
{
22+
int enemy = size * 100;
23+
24+
// Locate the index of our position
25+
int start = distance(arr,find(arr, size+arr, 1));
26+
27+
// Locating the enemy to the left
28+
// We increment the length to use as a boundary check
29+
// If is equal to that boundary check length than we know the value was not found
30+
int left = distance(arr, find(arr, (start + arr)+1, 2));
31+
32+
// Loop condition in the case we have multiples enemies before the start location
33+
// We have to increase our index from the start each time to ensure we reach the closest enemy
34+
int temp = left;
35+
int index = 0;
36+
while (temp != start + 1)
37+
{
38+
left = temp;
39+
temp = distance(arr, find(arr+index, (start + arr) + 1, 2));
40+
index++;
41+
}
42+
43+
// Condition to check if found
44+
// If found find the difference in distance
45+
if (left != start+1)
46+
{
47+
enemy = start - left;
48+
}
49+
50+
// Locating the closest enemy to the right
51+
int right = distance(arr, find(start + arr, (size + arr)+1, 2));
52+
if (right != size + 1 && right - start < enemy)
53+
{
54+
enemy = right - start;
55+
}
56+
57+
// Condition to check if any enemies where found
58+
if (enemy == size * 100)
59+
{
60+
return 0;
61+
}
62+
else
63+
{
64+
return enemy;
65+
}
66+
}
67+
68+
int main()
69+
{
70+
int A[] = { 0, 0, 1, 0, 0, 2, 0, 2 };
71+
int B[] = { 1, 0, 0, 0, 2, 2, 2 };
72+
int C[] = { 2, 0, 0, 0, 2, 2, 1, 0 };
73+
cout << ClosestEnemy(A, sizeof(A) / sizeof(A[0])) << endl; // 3
74+
cout << ClosestEnemy(B, sizeof(B) / sizeof(B[0])) << endl; // 4
75+
cout << ClosestEnemy(C, sizeof(C) / sizeof(C[0])) << endl; // 1
76+
return 0;
77+
78+
}

Debug/Fun Practice.log

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
Build started 11/28/2016 1:41:01 PM.
1+
Build started 12/20/2016 12:12:23 PM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
3-
1>Link:
4-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\test.obj
3+
1>ClCompile:
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 60_ClosestEnemy.cpp
5+
60_ClosestEnemy.cpp
6+
Link:
7+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\60_ClosestEnemy.obj
58
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
69
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
710

811
Build succeeded.
912

10-
Time Elapsed 00:00:01.05
13+
Time Elapsed 00:00:00.89
12.4 KB
Binary file not shown.
822 Bytes
Binary file not shown.
842 Bytes
Binary file not shown.
44 Bytes
Binary file not shown.
-494 Bytes
Binary file not shown.
22 Bytes
Binary file not shown.

Debug/vc120.idb

8 KB
Binary file not shown.

Fun Practice.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</ProjectConfiguration>
1212
</ItemGroup>
1313
<ItemGroup>
14-
<ClCompile Include="59_VowelSquare.cpp" />
14+
<ClCompile Include="60_ClosestEnemy.cpp" />
1515
</ItemGroup>
1616
<PropertyGroup Label="Globals">
1717
<ProjectGuid>{48DF0788-2D72-474C-9946-5512D2A5D092}</ProjectGuid>

0 commit comments

Comments
 (0)