### Can someone give me an opinion on this code?

Hello!
Yesterday when I was in a lecture of num. methods.My Professor said that programing is basically an automation of everyday tasks and time consuming excersises.
That got me thinking to when the difference engine was designed because it not only wanted to achieve that task but wanted to calculate the typeset mathematical tables.

So I took it as a challenge upon myself to do what Charles Babbage did.
But instead of calculation of the typeset math tables.
I decided to use the calculation of matrices(multiplication,rank,determinant...)
Because I thought that a self-respecting(at least in my opinion) programmer should be able to do these sort of automated math calculations.

And my questions are:
Have I gotten the core principles of programming?
I have managed to complete the beginner exercises on this forum with the bunny graduation but I don't think I'm skilled enough to move on other projects how can I determine if I'm skilled enough or not?
And what do you think of part of my code that multiplies matrices?
I know it works well but I feel that it isn't perfect(why do I have this feeling?And not only in this exercise but everytime I program why?).

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` `````` #include #include #include #include int main() { srand(time(NULL)); int matrixnumber=0,rowcol=0; std::cout<<"Enter the size of the matrix you want to multiply "; std::cin>>rowcol; int **matrixa=new int *[rowcol]; int **matrixb=new int *[rowcol]; int **matrixc=new int *[rowcol]; for(int i=0;i

Last edited on
OK, but what if you need to multiply three matrices? What if you want to multiply and add matrices but you want to let the user specify the sequence of operations?
What if I want to use your program and modify it to do something else? I have to figure out where initialization ends and multiplication begins? Does the language provide any facilities to construct higher level operations out of basic building blocks?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include #include #include // https://cal-linux.com/tutorials/vectors.html // http://en.cppreference.com/w/cpp/language/type_alias using matrix = std::vector< std::vector > ; matrix make_square_matrix( std::size_t rowcol ) { return std::vector< std::vector >( rowcol, std::vector(rowcol) ) ; } matrix make_random_square_matrix( std::size_t rowcol ) { // http://en.cppreference.com/w/cpp/numeric/random static std::mt19937 engine( std::random_device{}() ) ; static std::uniform_int_distribution distrib( 0, 9 ) ; matrix mtx = make_square_matrix(rowcol) ; // http://www.stroustrup.com/C++11FAQ.html#for // http://www.stroustrup.com/C++11FAQ.html#auto for( auto& row : mtx ) for( int& v : row ) v = distrib(engine) ; return mtx ; } void print( const matrix& mtx ) { for( const auto& row : mtx ) { for( int v : row ) std::cout << std::setw(5) << v ; std::cout << '\n' ; } std::cout << '\n' ; } // invariant: a and b are square matrices of equal size matrix multiply_square_matrices( const matrix& a, const matrix& b ) { matrix c = make_square_matrix( a.size() ) ; for( std::size_t i = 0 ; i < a.size() ; ++i ) for( std::size_t j = 0 ; j < a.size() ; ++j ) c[i][j] = a[i][j] * b[j][i] ; return c ; } int main() { std::size_t rowcol ; std::cout<<"Enter the size of the matrix you want to multiply "; std::cin >> rowcol; const matrix a = make_random_square_matrix(rowcol) ; print(a) ; const matrix b = make_random_square_matrix(rowcol) ; print(b) ; const matrix c = multiply_square_matrices(a,b) ; print(c) ; }``````
 OK, but what if you need to multiply three matrices? What if you want to multiply and add matrices but you want to let the user specify the sequence of operations? What if I want to use your program and modify it to do something else? I have to figure out where initialization ends and multiplication begins? Does the language provide any facilities to construct higher level operations out of basic building blocks?

1.I will use recursion.
2.I will ask the user to specify in which order to perform the sequence of operations.
4.
 Does the language provide any facilities to construct higher level operations out of basic building blocks?
I don't understand,what do you mean by that?

Thanks for the tip as well JLBorges.

Though I was looking for an opinion on how part of my code looks I did get questions questions which give a bigger challenge.

But still I haven't gotten answers to my first 2 questions.

Thank you for your time :).
All three answers are wrong (well, I suppose answer #2 is not wrong, it just doesn't answer my question). All the questions I asked have a single answer. Look at how JLBorges structured his code and how you structured yours.
Last edited on
Topic archived. No new replies allowed.