0
votes

This is the first time I am using C++ and it seems like I am having some difficulties. My task has the following statement: 'If the number of rows and columns of the matrix X is the same (i.e. X is a square matrix), then find the scalar product of the elements of the main diagonal with the elements from random row K(K<=N).'

 #include <iostream.h>
int main(){
    int M,N,i,j;
    int X[20][20];
    do{cout<<"\n Vavedete stoinost za M:"<<endl;cin>>M;}
    while(M<2||M>20);
    do{cout<<"\n Vavedete stoinost za N:"<<endl;cin>>N;}
    while(N<2||N>20);
    for(i=0;i<M;i++)
        for(j=0;j<N;j++)
        {cout<<"\n Vavedete element ot X["<<i<<"]["<<j<<"]="<<endl;cin>>X[i][j];}
        //usl.1.
        int k;
        if (i==j){
        cout<<"\n ELementite na koi red da se umnojat s tezi na glavniq diagonal:"<<endl;cin>>k;
        k=k-1;
        int K[20];
        for(i=0;i<M;i++)
        for(j=0;j<M;j++){
            int SP=1;
            for(k=0;k<M;k++)
            SP+=X[i][j]*X[k][j];
            cout<<SP<<endl;}
        return 0;}
        }
2
hmmm mind adding some meat to your question? we are not here to write your code for you..... what have you tried, which part of this homework are you blocked on, why is the code you supplied currently not working?Patrice
Actually, I am having problems with the formula for Scalar product.Mihaela Mincheva
Wtf is scalar product of the matrix? Do you mean determinant?Anonymous
If you have problem with formula (not algorithm) you should rather do your post on Math Overflow.Anonymous
@MihaelaMincheva I still don't quite understand. According to your algorithm, isn't the result of the 'scalar multiple' of two matrixes actually a vector? You are splitting the matrix by its rows? columns? and then multiplying them separately, and the result should be a vector of scalars? Please try to clarify your algorithm and better describe what exactly should be the output given some test cases.Rahul Manne

2 Answers

0
votes

Several things with your algorithm:

  • if i == j, you probably do not need to do the following loop for(i=0;i<M;i++) for(j=0;j<M;j++){} to calculate the final sum (see my example which is executed in O(N))

  • Once you enter the row number K, I don't understand why you are incrementing the K value

  • I would probably first check that M == N before entering all value in the matrix

Below my corresponding code:

#include <iostream.h>
int main(){
    int M,N,i,j;
    int X[20][20];

    do{cout<<"\n Vavedete stoinost za M:"<<endl;cin>>M;}
    while(M<2||M>20);

    do{cout<<"\n Vavedete stoinost za N:"<<endl;cin>>N;}
    while(N<2||N>20);

    //usl.1.
    int k;
    if (M==N)
    {
        for(i=0;i<M;i++)
            for(j=0;j<N;j++)
                {cout<<"\n Vavedete element ot X["<<i<<"]["<<j<<"]="<<endl;cin>>X[i][j];}

        cout<<"\n ELementite na koi red da se umnojat s tezi na glavniq diagonal:"<<endl;cin>>k;
        k=k-1;

        int SP=1;
        for(i=0;i<M;i++)
        {
            SP+=X[i][i]*X[k][i];
            cout<<SP<<endl;
        }

        return 1;
    }
    return 0;
}
0
votes

If you want to multiply a diagonal with the elements of random row K, then why :

SP+=X[i][j]*X[k][j];

?

This just multiplies every entry with the entry that is in the mirror location (along the diagonal). This does not what you describe.

What you would want is the following (pseudo)

k= random row number
for(column: columns){
  total += X(column,column)*X(row,column)
}

And don't call your table 'X' ... don't.