-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTR143_Ax.cpp
More file actions
251 lines (223 loc) · 6.12 KB
/
CPTR143_Ax.cpp
File metadata and controls
251 lines (223 loc) · 6.12 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*****************************************************************************
Program file name: main.cpp OS: Windows XP x64 Assignment #: 3
Programmer: Daniel Peterson Class: CPTR 143 Date: 4/13/07
Compiler: Dev-C++ 4.9.9.2
Assistance/references:
Description:
Inputs:
Outputs:
Special Comments:
if statement in search causes program to crash if no entry is found.
Reason for this is unknown.
~~~~~~~~~~~~~~~~~~~~~~~~~~Grading Criteria~~~~~~~~~~~~~~~~~~~~~~~~~~
Assignment Requirements ___/3.0
Code Format/Cosmetics ___/3.0
Header & Code Comments: ___/2.0
Output Format/Cosmetics ___/2.0
***Does Not Compile***: ___ (-10.0)
***Late Work Deduction***: ___ (-0.5/day)
Total Grade: ___/10.0
*****************************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <time.h>
using namespace std;
void adj(int n, int matrx[][32])
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
matrx[i][j] = (rand() % 2);
}
void Cost(int n, int cost_matrx[][32], int adj[][32])
{
if(n <= 8)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if (i == j)
{
cost_matrx[i][j] = 0;
}
else if (adj[i][j] == 1)
{
cost_matrx[i][j] = (1 + rand() % 20);
}
else
{
cost_matrx[i][j] = 2000;
}
}
else if(n <= 16)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if (i == j)
{
cost_matrx[i][j] = 0;
}
else if (adj[i][j] == 1)
{
cost_matrx[i][j] = (30 + rand() % 370);
}
else
{
cost_matrx[i][j] = 2000;
}
}
else
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if (i == j)
{
cost_matrx[i][j] = 0;
}
else if (adj[i][j] == 1)
{
cost_matrx[i][j] = (90 + rand() % 810);
}
else
{
cost_matrx[i][j] = 2000;
}
}
}
bool Warshall(int A[][32], int T[][32], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
T[i][j] = A[i][j];
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
T[i][j] = min(T[i][j], T[i][k]+T[k][j]);
}
void Dijkstra(int v, int cost[][32], int D[], int n)
{
int found[n];
memset(found, 0, sizeof(found));
for (int i = 0; i < n; i++)
D[i] = cost[v][i];
found[v] = true;
D[v] = 0;
for (int i = 2; i < n; i++)
{
int run_min = 999999;
int u = -1;
for (int w = 0; w < n; w++)
if (!found[w])
if (run_min > D[w])
{
run_min = D[w];
u = w;
}
found[u] = true;
for (int w = 0; w < n; w++)
if (!found[w])
D[w] = min(D[w], D[u]+cost[u][w]);
}
}
void printDijk(int D[], int n)
{
for(int i = 0; i < n; i++)
cout << setw(4) << D[i];
cout << endl;
}
void printMatrix(int matrix[][32], int n)
{
// Top numbers
cout << setw(3) << ' ';
for (int i = 0; i < n; i++)
{
cout << setw(4) << i+1;
}
cout << endl;
//Top of brackets
cout << setw(3) << ' ' << char(218);
cout << setw(4*n) << char(191) << endl;
// Loop through rows
for (int i = 0; i < n; i++)
{
cout << setw(2) << i+1 << " " << char(179);
cout << setw(3);
if (matrix[i][0] == 2000)
cout << char(236);
else
cout << matrix[i][0];
for (int j = 1; j < n; j++)
{
cout << setw(4);
if (matrix[i][j] == 2000)
cout << char(236);
else
cout << matrix[i][j];
}
cout << char(179) << endl;
}
// bottom of bracket
cout << setw(4) << char(192) << setw(4*n) << char(217) << endl;
}
void chosen(int x)
{
int matrix[32][32];
int cost[32][32];
int transitive[32][32];
int D[32];
adj(x, matrix);
Cost(x, cost, matrix);
Warshall(cost, transitive, x);
Dijkstra(0, cost, D, x);
cout << "Adjacency Matrix:\n";
printMatrix(matrix, x);
cout << "Cost Matrix:\n";
printMatrix(cost, x);
cout << "Transitive Closure from Warshall's:\n";
printMatrix(transitive, x);
cout << "Transitive Closure from Dijkstra:\n";
printDijk(D, x);
}
void printMenu() //the menu function
{
cout << endl; //insert blank line here
cout << "+----------------------------------+" << endl;//top of menu box
cout << "| Choose an option: |" << endl;//tell user what to do
cout << "| 1 - Create an 8 element graph |" << endl;//1st option
cout << "| 2 - Create a 16 element graph |" << endl;//2nd option
cout << "| 3 - Create a 32 element graph |" << endl;//3rd option
cout << "| 0 - Exit |" << endl;//quit? why would you want to do that?
cout << "+----------------------------------+" << endl;//bottom of menu box
cout << endl; //another blank line
}
int main(int argc, char *argv[])
{
char choice;
srand(time(NULL));
printMenu(); //print the menu
cout << endl << "Option: "; //blank line followed by "Option: "
cin >> choice; //input for user's choice
cin.ignore(1); //ignores one input
// loop in the menu till the user selects 0 to exit
while (choice != '0')
{
cout << endl;
// Switch over the users choice
switch (choice)
{
case '1': chosen(8); break;
case '2': chosen(16); break;
case '3': chosen(32); break;
case '0': break;
default: cout << "Invalid Option" << endl; break;
}
// Print the menu again
printMenu();
// Get the user's choice
cout << "Option: ";
cin >> choice;
cin.ignore(1);
}
system("PAUSE");
return EXIT_SUCCESS;
}