forked from hiimmuc/Transportation_Optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculation_func.cpp
98 lines (86 loc) · 2.49 KB
/
Calculation_func.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<iostream>
int** initializearray(int rows, int cols)
{
int** arr = new int* [rows];
for (int i = 0; i < rows; ++i)
arr[i] = new int[cols];
return arr;
};
void fillexample(int** arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
arr[i][j] = i;
};
void printarray(int** arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
std::cout << arr[i][j] << " ";
std::cout << "\n";
}
};
void freearray(int** arr, int rows, int cols)
{
for (int i = 0; i < rows; i++)
delete[] arr[i];
delete[] arr;
};
void calculate(int** distances, int** drivers, int** customers, int** customers_final, int rows, int cols)
{
int distance = 0, distance_final = 0, distance_start = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < rows; j++)
{
//for (int k = 0; k < cols; k++)
// distances[i][j] = distances[i][j] + abs(drivers[i][k] - customers[j][k]);
for (int k = 0; k < cols; k++)
{
distance_start = distance_start + abs(drivers[i][k] - customers[j][k]);
distance_final = distance_final + abs(customers[i][k] - customers_final[j][k]);
distance = distance_final + distance_start;
}
distances[i][j] = distance;
distance = distance_final = distance_start = 0;
};
};
int main(int argc, char** argv)
{
int rows = 3;
int cols = 2;
if (argc < 3)
{
std::cout << "Using default rows = 3 cols = 2" << "\n";
}
else
{
rows = strtol(argv[1], NULL, 10);
cols = strtol(argv[2], NULL, 10);
std::cout << "Using modified rows = "<< rows <<" cols = "<< cols << "\n";
}
int** drivers = initializearray(rows, cols);
int** customers = initializearray(rows, cols);
int** distances = initializearray(rows, rows);
int** customers_final = initializearray(rows, cols);
fillexample(drivers, rows, cols);
fillexample(customers, rows, cols);
fillexample(customers_final, rows, cols);
std::cout << "Driver 's coordinates:" << "\n";
printarray(drivers, rows, cols);
std::cout << "Customer 's start coordinates:" << "\n";
printarray(customers, rows, cols);
std::cout << "Customer 's final coordinates:" << "\n";
printarray(customers_final, rows, cols);
calculate(distances, drivers, customers, customers_final, rows, cols);
std::cout << "Distances (Driver x Customer):" << "\n";
printarray(distances, rows, rows);
freearray(drivers, rows, cols);
freearray(customers, rows, cols);
freearray(distances, rows, cols);
return 0;
};