Hi, I should printout the value(s) of the "sum" of the end of the code... But i don't know...:( I am not good at C++. Please help!

The code is not so difficult: it generates random 3 dimensional vectors, e.g. number of 5. Then makes a matrix of 3x3 from a vector and another vector makes another matrix, ...etc. And finally adds the five matrix and divide by 5. The sum of the 5 matrix in the end of the code is "sum". I should printout this matrix.

#include <cstdlib>  
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>

using namespace std;

const float M_2PIf = 6.283185307179586476925286766559f;
const float M_PIf  = 3.141592653589793238462643383279f;

/* -----------------RANDOM_GENERATOR------------------------------- */

float rand(float min, float max)
{
	return min + (max - min) * rand() / (float)RAND_MAX;
}
/*------------------------------------------------------------------*/

struct vektor {
float x,y,z;
};

/* ------------------MATRIX_CLASS-----------------------------------*/
class Matrix {

private:

 	float tomb[3][3]; 

public:
       
        void init(vektor &a){ 

	tomb[0][0] = a.x * a.x;
	tomb[0][1] = a.x * a.y;
	tomb[0][2] = a.x * a.z;
	tomb[1][0] = a.y * a.x;
	tomb[1][1] = a.y * a.y;
	tomb[1][2] = a.y * a.z;
	tomb[2][0] = a.z * a.x;
	tomb[2][1] = a.z * a.y;
	tomb[2][2] = a.z * a.z;
    }

Matrix &operator +=(Matrix m){
 int k,l;
 for(k=0; k<3; k++)
 for(l=0; l<3; l++) 
  tomb[k][l] += m.tomb[k][l];
 return *this;
}

 Matrix &operator/=(float c){
 int k,l;
 for(k=0; k<3; k++)
 for(l=0; l<3; l++)
  tomb[k][l] /= c;
 return *this;
}

};

/* -------------------------------------------------------------------- */

int main() {

    int i,j;

    vektor tar[5]; 

    for (i = 0;i<5;i++) {

	float phi   = rand(0.0f, M_2PIf);
        float costheta = rand(-1.0f, 1.0f);
	
	float x = 1.0f * sqrtf(1-costheta*costheta) * cosf(phi);
	float y = 1.0f * sqrtf(1-costheta*costheta) * sinf(phi);
	float z = 1.0f * costheta;

	tar[i].x = x;
	tar[i].y = y;
	tar[i].z = z;

    }

    Matrix m[5]; 
    for (i=0;i<5;i++) {
        
	m[i].init(tar[i]);
    }
 
    Matrix sum = m[0];
 

    for (i=0;i<5;i++) {
        
	sum += m[i];
    }
	
    sum /= 5;
    


return 0;
}

>>But i don't know... I am not good at C++.
I take it then that you did not write the code you posted. Did your teacher give you that code for this assignment or did you copy (steal) it from someone else ? (rhetorical question not to be answered here).

Looks to me that all you have to do is display each of the matrix rows and columns. Create two loops with loop counters i and j then use cout to display the values cout << sum[i][j];

it is not so goog...:( but i have no counter j at all in the code...

this is the error message:

cov.cpp: In function ‘int main()’:
cov.cpp:108: error: no match for ‘operator[]’ in ‘sum

My eyes are not so good so I can't see the code that's on your monitor very well. Will you post it so that I can see it better?

>>but i have no counter j at all in the code...
sum is a 2d array right? Then if you don't have a j variable you have to add one.

Sorry, I don't understand. I am talking about the fact that I have a result, which is a simple 3d matrix. This would be the result or somewhat...
You are right, I should use loops, but how...?? Obviously the
cout << sum << endl; or cout << sum[j] << endl; is incorrect on the basis of my code. Maybe my code is wrong, because I use only the counter i. I am stuck in this point...:(

I'd attack this problem by creating a display() method for the Matrix class or overload the << for the Matrix class. This will allow you to see what the result of sum /= 5 is.

I think the real problem is the need for an overloaded assignment operator for the Matrix class. To prove it, I'd display m[0] before this line:

Matrix sum = m[0];

and sum after that line. I believe you need an overloaded assignment operator in order to allow you to assign the values of m[0].tomb to sum.tomb, since tomb is an array and you can't assign one array to another.

Yes. This is the question. What is the method to print out the sum? Or can you suggest me other code to this part?

Well I can see you have no clue what you are doing. Add this method to Matrix class

void display()
{
for(int i = 0; i < 3; i++)
{
   for(int j = 0; j < 3; j++)
   {
       cout << tomb[i][j] << " ";
   }
   cout << "\n";
}

Than call it in main() after sum has been calculated sum.display();

how bout this one :

void cout_mat(int matrix[20][20],int r, int c)
 {
for(int i=0; i<r ;i++)
      {
      cout<<endl<<endl;
       for(int j=0; j<c ;j++)
	{
	 cout<<" | "<<matrix[i][j]<<" | ";
	 }
       }
  }

It gives out put in somewhat a tabular form.

I'd display it like this for routine matrixes

for(int i= 0; i < 3; ++i)
{
    for(int  j = 0; j < 3; ++j)
    {
       cout << tomb[i][j] << ' ';
     }
     cout << endl;}
}

Given the significant mantissa(?) (whatever you call the decimal portion of the float variable) however you may find it more convenient to do something like this:

for(int i= 0; i < 3; ++i)
{
    for(int  j = 0; j < 3; ++j)
    {
       cout << '(' << i << ", " << j << ')' << tomb[i][j] << endl;
     }
     cout << endl;}
}

I gave an error message:

cov.cpp:124: error: expected `}' at end of input
cov.cpp:124: error: expected unqualified-id at end of input

You need to post the relevant code. I suspect you left out a '}' on or about line 124 of your code or else you left in a '{' that was supposed to be removed with change in code and wasn't. Without seeing the code there's no way to know what's what.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.