-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.cpp
More file actions
66 lines (61 loc) · 1.43 KB
/
final.cpp
File metadata and controls
66 lines (61 loc) · 1.43 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using std::vector;
#include <cmath>
struct point {
double x;
double y;
};
/* #2: brute force max distance between two points */
double maxDist(const vector<point>& V)
{
/* IDEA: go through all pairs of points, compute
* distance; keep track of max. */
double max = 0; /* smallest possible value for dist. */
for (size_t i = 0; i < V.size(); i++) {
for (size_t j = i+1; j < V.size(); j++) {
/* compute distance^2 between V[i],V[j] */
double d = (V[i].x - V[j].x)*(V[i].x - V[j].x)
+ (V[i].y - V[j].y)*(V[i].y - V[j].y);
if (d > max) max = d;
}
}
return sqrt(max);
}
/* #3 print an integer in octal. */
void printOctal(size_t n)
{
/* IDEA: induction on number of (base 8) digits */
if ( n < 8 /* single digit */ ) {
printf("%lu",n);
return;
}
/* print high order digits via recursive magic: */
printOctal(n/8);
/* then print the last digit: */
printf("%lu",n%8);
}
/* #5 remove last element of a linked list */
void removeLast(node*& L)
{
/* walk to end of list, but don't go all the way off... */
node* p = L;
if (p == NULL) return; /* there is no last node! */
node* q = NULL; /* make q follow p */
while (p->next != NULL) { /* issue: might crash! */
q = p;
p = p->next;
}
delete p;
if (q) q->next = NULL;
else L = NULL;
/* NOTE: if list had length 1, q would be NULL! */
}
/* for testing: */
int main()
{
printOctal(133);
printf("\n");
return 0;
}