-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
109 lines (95 loc) · 2.47 KB
/
common.cpp
File metadata and controls
109 lines (95 loc) · 2.47 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
//
// common.cpp
// zad3
//
// Created by Piotr Sokólski on 25.05.2014.
// Copyright (c) 2014 Piotr Sokólski. All rights reserved.
//
#include "common.h"
#include "matgen.h"
int numRows, numColumns, seed;
int bestI, bestJ, bestK, bestL;
struct timeval startTime;
matrix_t max_sum;
matrix_t * matrixPtr;
extern void safe_exit(int code);
void printMatrix(matrix_t const * m, int r, int c)
{
// int i, j;
// for (i = 0; i <= r; ++i)
// {
// for (j = 0; j <= c; ++j)
// {
// printf(" %lld", m[at(i, j)]);
// }
// printf("\n");
// }
// printf("\n");
}
void printEnd(){
struct timeval endTime;
if (gettimeofday(&endTime, NULL))
{
fprintf(stderr, "ERROR: Gettimeofday failed!\n");
safe_exit(1);
}
double duration =
((double) endTime.tv_sec + ((double) endTime.tv_usec / 1000000.0)) -
((double) startTime.tv_sec + ((double) startTime.tv_usec / 1000000.0));
fprintf(stderr, "PWIR2014_Piotr_Sokolski_292408 Input: (%d,%d,%d) Solution: |(%d,%d),(%d,%d)|=%lld Time: %.10f\n",
numRows, numColumns, seed,
bestJ, bestI, bestK, bestL, max_sum, duration);
}
void printUsage(char const * prog)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s <num_rows> <num_colums> <seed>\n\n", prog);
}
void initialize(int argc, char * argv[]){
matgen_t * matgenPtr;
if (argc != 4)
{
fprintf(stderr, "ERROR: Invalid arguments!\n");
printUsage(argv[0]);
safe_exit(1);
}
numRows = atoi(argv[1]);
numColumns = atoi(argv[2]);
seed = atoi(argv[3]);
if (numRows <= 0 || numColumns <= 0 || seed <= 0)
{
fprintf(stderr, "ERROR: Invalid arguments: %s %s %s!\n", argv[1],
argv[2], argv[3]);
printUsage(argv[0]);
safe_exit(1);
}
matgenPtr = matgenNew(numRows, numColumns, seed);
if (matgenPtr == NULL)
{
fprintf(stderr, "ERROR: Unable to create the matrix generator!\n");
printUsage(argv[0]);
safe_exit(1);
}
matrixPtr = (matrix_t *) malloc(sizeof(matrix_t) * (numRows + 1) * (numColumns + 1));
if (matrixPtr == NULL)
{
fprintf(stderr, "ERROR: Unable to create the matrix!\n");
printUsage(argv[0]);
safe_exit(1);
}
for (int j = 1; j <= numRows; ++j)
{
for (int i = 1; i <= numColumns; ++i)
{
matrixPtr[j * (numColumns + 1) + i] = matgenGenerate(matgenPtr);
}
}
matgenDestroy(matgenPtr);
}
void startTiming(){
if (gettimeofday(&startTime, NULL))
{
fprintf(stderr, "ERROR: Gettimeofday failed!\n");
safe_exit(1);
}
}