Ok, therockon7throw was right. That is ugly. That looks like FORTRAN. Word to the wise, you won't get many responses with code looking like that. Explain what you are trying to do better than just giving a bunch of code with minimal comments.
Listen -- AVOID hardcoded numbers! This is a computer program not pencil and paper. Especially since you want to resize things. And learn what a vector is. It is a dynamically allocating array. You don't want to have a function parameter that takes a vector argument like std::vector<int> p[3][3]. Hell, I am not even sure if that is valid bc I cant begin to understand what you are trying to accomplish there.
So for what you first wrote:
creat class have
*if two columns can be different sizes.
*the object can be resize.
poor english but I am guessing this means you want a vector. Use the push_back() function to add on to the end, or clear() and resize() if you want to start from scratch.
*the object check if the user refers to an element that is out of range
You want a try catch exception for this.
in other words:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <stdexcept>
#include <iostream>
#include <exception>
#include <vector>
int main(int argc, char* argv[])
{
std::vector myVector;
myVector.resize(5,0); // initialize vector to ndim = 5, value = 0
myVector.push_back(1); // resize the vector to ndim = 6 with myVector[5] = 1
int someNumber = 8;
try {
myVector.at(someNumber);
} catch (std::out_of_range const &ex) {
std::cout << "Exception: " << ex.what() << " -- "<< someNumber
<< " is out of the allowed range" << std::endl;
}
}
|
*provide different matrix-oriented operation
with vectors you can access this sort of information with size()
in other words:
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
|
unsigned int ndim = 10; // 10x10 square matrix
// notice no hard coded numbers except for ndim which wouldn't
// really exist because you should be passing "second" to the function
// and returning transpose
std::vector<double> first;
std::vector<std::vector<double> > second;
first.resize(ndim,0); // initialize all ten rows to zero
second.resize(ndim,first); // initialize ten columns of zero from first
int num = 1;
// second is 10x10 square matrix, transpose will also be 10x10
// try using this function, add in code to print out second and transpose
std::vector<std::vector<double> > transpose = second;
// transpose the matrix, notice no h
for(unsigned int i = 0; i < second.size(); ++i) {
for(unsigned int j = 0; j < second[i].size(); ++j) {
second[i][j] = num;
transpose[j][i] = second[i][j];
++num;
}
}
|