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?).


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

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>



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<rowcol;i++)
    {
        *(matrixa+i)=new int [rowcol];
        *(matrixb+i)=new int [rowcol];
        *(matrixc+i)=new int [rowcol];
    }

    for(int i=0;i<rowcol;i++)
    {
        for(int j=0;j<rowcol;j++)
        {
            *(*(matrixa+i)+j)=rand()%10;
            *(*(matrixb+i)+j)=rand()%10;
        }
    }

    for(int i=0;i<rowcol;i++)
    {
        std::cout<<"|";
        for(int j=0;j<rowcol;j++)
        {
            *(*(matrixc+i)+j)=*(*(matrixa+i)+j)*(*(*(matrixb+j)+i));
            std::cout<<" "<<*(*(matrixc+i)+j)<<" ";
        }
        std::cout<<"|\n";
    }

        for(int i=0;i<rowcol;i++)
    {
        delete [] *(matrixa+i);
        delete [] *(matrixb+i);
        delete [] *(matrixc+i);
    }

    delete [] matrixa;
    delete [] matrixb;
    delete [] matrixc;


}




Thank you for your time!
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?
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
#include <iostream>
#include <vector>
#include <random>
#include <iomanip>

// https://cal-linux.com/tutorials/vectors.html
// http://en.cppreference.com/w/cpp/language/type_alias
using matrix = std::vector< std::vector<int> > ;

matrix make_square_matrix( std::size_t rowcol )
{ return std::vector< std::vector<int> >( rowcol, std::vector<int>(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<int> 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.
3.I will add comments specifying where I declare,Initialize,perform multiplication and other operations of importance.
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.