-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.hpp
118 lines (100 loc) · 4.46 KB
/
Matrix.hpp
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
#ifndef MATRIX_HPP
#define MATRIX_HPP
/* Matrix.hpp
* Originally written by James Juett at the University of Michigan
* for project 3 in EECS 280, Winter 2016.
*
* The Matrix module is based on an earlier project by
* Andrew DeOrio.
*/
#include <iostream>
const int MAX_MATRIX_WIDTH = 500;
const int MAX_MATRIX_HEIGHT = 500;
// Representation of a 2D matrix of integers
// Matrix objects may be copied.
struct Matrix{
int width;
int height;
int data[MAX_MATRIX_WIDTH * MAX_MATRIX_HEIGHT];
};
// REQUIRES: mat points to a Matrix
// 0 < width && width <= MAX_MATRIX_WIDTH
// 0 < height && height <= MAX_MATRIX_HEIGHT
// MODIFIES: *mat
// EFFECTS: Initializes *mat as a Matrix with the given width and height.
// NOTE: Do NOT use new or delete here.
void Matrix_init(Matrix* mat, int width, int height);
// REQUIRES: mat points to a valid Matrix
// MODIFIES: os
// EFFECTS: First, prints the width and height for the Matrix to os:
// WIDTH [space] HEIGHT [newline]
// Then prints the rows of the Matrix to os with one row per line.
// Each element is followed by a space and each row is followed
// by a newline. This means there will be an "extra" space at
// the end of each line.
void Matrix_print(const Matrix* mat, std::ostream& os);
// REQUIRES: mat points to a valid Matrix
// EFFECTS: Returns the width of the Matrix.
int Matrix_width(const Matrix* mat);
// REQUIRES: mat points to a valid Matrix
// EFFECTS: Returns the height of the Matrix.
int Matrix_height(const Matrix* mat);
// REQUIRES: mat points to a valid Matrix
// ptr points to an element in the Matrix
// EFFECTS: Returns the row of the element pointed to by ptr.
int Matrix_row(const Matrix* mat, const int* ptr);
// REQUIRES: mat points to a valid Matrix
// ptr point to an element in the Matrix
// EFFECTS: Returns the column of the element pointed to by ptr.
int Matrix_column(const Matrix* mat, const int* ptr);
// REQUIRES: mat points to a valid Matrix
// 0 <= row && row < Matrix_height(mat)
// 0 <= column && column < Matrix_width(mat)
//
// MODIFIES: (The returned pointer may be used to modify an
// element in the Matrix.)
// EFFECTS: Returns a pointer to the element in the Matrix
// at the given row and column.
int* Matrix_at(Matrix* mat, int row, int column);
// REQUIRES: mat points to a valid Matrix
// 0 <= row && row < Matrix_height(mat)
// 0 <= column && column < Matrix_width(mat)
//
// EFFECTS: Returns a pointer-to-const to the element in
// the Matrix at the given row and column.
const int* Matrix_at(const Matrix* mat, int row, int column);
// REQUIRES: mat points to a valid Matrix
// MODIFIES: *mat
// EFFECTS: Sets each element of the Matrix to the given value.
void Matrix_fill(Matrix* mat, int value);
// REQUIRES: mat points to a valid Matrix
// MODIFIES: *mat
// EFFECTS: Sets each element on the border of the Matrix to
// the given value. These are all elements in the first/last
// row or the first/last column.
void Matrix_fill_border(Matrix* mat, int value);
// REQUIRES: mat points to a valid Matrix
// EFFECTS: Returns the value of the maximum element in the Matrix
int Matrix_max(const Matrix* mat);
// REQUIRES: mat points to a valid Matrix
// 0 <= row && row < Matrix_height(mat)
// 0 <= column_start && column_end <= Matrix_width(mat)
// column_start < column_end
// EFFECTS: Returns the column of the element with the minimal value
// in a particular region. The region is defined as elements
// in the given row and between column_start (inclusive) and
// column_end (exclusive).
// If multiple elements are minimal, returns the column of
// the leftmost one.
int Matrix_column_of_min_value_in_row(const Matrix* mat, int row,
int column_start, int column_end);
// REQUIRES: mat points to a valid Matrix
// 0 <= row && row < Matrix_height(mat)
// 0 <= column_start && column_end <= Matrix_width(mat)
// column_start < column_end
// EFFECTS: Returns the minimal value in a particular region. The region
// is defined as elements in the given row and between
// column_start (inclusive) and column_end (exclusive).
int Matrix_min_value_in_row(const Matrix* mat, int row,
int column_start, int column_end);
#endif // MATRIX_HPP