tmatrix<int> a(5,3); // a has 5 rows and 3 columns a[4][2] = 21; // store 21 in last row/last column
int RowSum(const tmatrix<int>& m, int r)
// pre: 0 <= r < m.numrows()
// post: returns sum of numbers in row r
{
int k;
int sum = 0;
for(k=0; k < m.numcols(); k++)
{
sum += m[r][k];
}
return sum;
}
Write ColSum
int ColSum(const tmatrix<int>& m, int c) // pre: 0 <= c < m.numcols() // post: returns sum of numbers in column c
The main diagonal of a matrix m consists of the elements
m[0][0], m[1][1], m[2][2], ...and, in general, of m[k][k] as k ranges from 0 to m.numrows()-1.
int DiagonalSum(const tmatrix<int>& m) // pre: m is a square matrix // post: returns sum of elements on main diagonal
6 1 8 7 5 3 2 9 4Write IsMagic that returns true if its matrix parameter m is magic. You can call RowSum, ColSum, and DiagonalSum.
bool IsMagic(const tmatrix<int>& m) // pre: m is a square matrix // post: returns true if m is a magic square, false otherwise
bool Search(const tvector<string>& list, const string& key, int index)
// pre: 0 <= index < list.size(), list contains list.size() elements
// post: returns true if key contained in list[index]...list[list.size()-1]
{
if (index >= list.size()) return false;
}
bool Search(const tvector<string>& list, const string& key)
// pre: list contains list.size() elements
// post: returns true if key in list, false otherwise
{
return Search(list, key, 0); // uses helper function
}
int Mode(const tvector<int>& a) // pre: a contains a.size() elements, all between 0 and 100 (inclusive) // post: returns number in a that occurs more frequently than any other // (assume no ties)