Multifile-program

[b][red]This message was edited by pasa at 2006-11-2 14:3:25[/red][/b][hr]
I have wrote this program contain 3 files. But it doesn't work perfectly. Can you plz help me to find out what's wrong? with my code or with my MS Visual C++?
Thank in advance.
Keon

//lab1_2.cpp
//
#include
#include "liblab1_2.h"
#include "StdAfx.h"
using namespace std;
int main()
{
matrix mas(9);
for (int i=0; i
#include "lab1_2.h"
#include "StdAfx.h"
using namespace std;
matrix::matrix(int N)
{
n=N;
m=new float [n*n];
}

//delete M, return true if m can be deleted
//bool matrix::DeleteM()
//{
// if (m)
// {
// delete [] m;
// return true;
// }
// else
// return false;
//}

//set value for variable[i,j]
bool matrix::SetM(int i,int j,float v)
{
//SetM=false;
m[(i-1)*n + j - 1]=v;
return true;
}

//get value of variable[i,j]
float matrix::GetM(int i,int j)
{
return m[(i-1)*n + j - 1];
}

//LenM
int matrix::LenM()
{
return n;
}

//12.h
//
class matrix
{
float* m;
int n;
public:
matrix(int N);
//bool DeleteM(); //delete M
//~matrix();
bool SetM(int i,int j,float v); //set value for variable[i,j]
float GetM(int i,int j); //get value of variable[i,j]
int LenM();

};


Comments

  • [b][red]This message was edited by HK_MP5KPDW at 2006-11-2 14:2:18[/red][/b][hr]
    : I have wrote this program contain 3 files. But it doesn't work perfectly. Can you plz help me to find out what's wrong? with my code or with my MS Visual C++?
    : Thx in advance.
    : Keon
    :

    [blue]Please learn to use code tags. Also, it really helps to explain what errors you may be getting, or what you are expecting to see as far as output goes versus what you are seeing that might lead you to believe that the program is not working correctly. I've added the code tags below.[/blue]

    [code]
    //12.h
    //
    class matrix
    {
    float* m;
    int n;
    public:
    matrix(int N);
    //bool DeleteM(); //delete M
    //~matrix();
    bool SetM(int i,int j,float v); //set value for variable[i,j]
    float GetM(int i,int j); //get value of variable[i,j]
    int LenM();

    };[/code]

    [code]
    //12.cpp
    //
    #include
    #include "lab1_2.h"
    #include "StdAfx.h"
    using namespace std;
    matrix::matrix(int N)
    {
    n=N;
    m=new float [n*n];
    }

    //delete M, return true if m can be deleted
    //bool matrix::DeleteM()
    //{
    // if (m)
    // {
    // delete [] m;
    // return true;
    // }
    // else
    // return false;
    //}

    //set value for variable[i,j]
    bool matrix::SetM(int i,int j,float v)
    {
    //SetM=false;
    m[(i-1)*n + j - 1]=v;
    return true;
    }

    //get value of variable[i,j]
    float matrix::GetM(int i,int j)
    {
    return m[(i-1)*n + j - 1];
    }

    //LenM
    int matrix::LenM()
    {
    return n;
    }
    [/code]

    [code]
    //lab1_2.cpp
    //
    #include
    #include "liblab1_2.h"
    #include "StdAfx.h"
    using namespace std;
    int main()
    {
    matrix mas(9);
    [red]for (int i=0; i<mas.LenM(); i++)
    for (int j=0; j<mas.LenM(); j++)
    mas.SetM(i,j,9);[/red]

    for (i=0; i<mas.LenM(); i++)
    {
    for (int j=0; j<mas.LenM(); j++)
    cout << mas.GetM(i,j) << " ";
    cout << endl;
    }
    return 0;
    }
    [/code]

    [blue]One problem I see is your loop above (in red) where you are calling the [italic]SetM[/italic] function. The [italic]SetM[/italic] function assumes that incomming indicies are 1-based (i.e. the indicies start at 1) while the loop variables [italic]i[/italic] and [italic]j[/italic] start off at 0 which means that the [italic]SetM[/italic] function is calculating an invalid offset into your matrix. You either need to adjust your starting values for [italic]i[/italic] and [italic]j[/italic] in the loop to 1(and also change the < to <=), or you can keep those starting values and change the [italic]SetM[/italic] function to use 0 based indicies (which would be my choice). Your [italic]GetM[/italic] function might also need to be changed depending on what method you use.

    I'd personally overload the stream insertion operator << so that you could just output the matrix directly, i.e.:

    [code]
    matrix mas(9);

    ...

    cout << mas;
    [/code]
    ...instead of your double nested [italic]for[/italic] loop.
    [/blue]






  • Thank for your reply.
    Everything is ok now :)

    : [b][red]This message was edited by HK_MP5KPDW at 2006-11-2 14:2:18[/red][/b][hr]
    : : I have wrote this program contain 3 files. But it doesn't work perfectly. Can you plz help me to find out what's wrong? with my code or with my MS Visual C++?
    : : Thx in advance.
    : : Keon
    : :
    :
    : [blue]Please learn to use code tags. Also, it really helps to explain what errors you may be getting, or what you are expecting to see as far as output goes versus what you are seeing that might lead you to believe that the program is not working correctly. I've added the code tags below.[/blue]
    :
    : [code]
    : //12.h
    : //
    : class matrix
    : {
    : float* m;
    : int n;
    : public:
    : matrix(int N);
    : //bool DeleteM(); //delete M
    : //~matrix();
    : bool SetM(int i,int j,float v); //set value for variable[i,j]
    : float GetM(int i,int j); //get value of variable[i,j]
    : int LenM();
    :
    : };[/code]
    :
    : [code]
    : //12.cpp
    : //
    : #include
    : #include "lab1_2.h"
    : #include "StdAfx.h"
    : using namespace std;
    : matrix::matrix(int N)
    : {
    : n=N;
    : m=new float [n*n];
    : }
    :
    : //delete M, return true if m can be deleted
    : //bool matrix::DeleteM()
    : //{
    : // if (m)
    : // {
    : // delete [] m;
    : // return true;
    : // }
    : // else
    : // return false;
    : //}
    :
    : //set value for variable[i,j]
    : bool matrix::SetM(int i,int j,float v)
    : {
    : //SetM=false;
    : m[(i-1)*n + j - 1]=v;
    : return true;
    : }
    :
    : //get value of variable[i,j]
    : float matrix::GetM(int i,int j)
    : {
    : return m[(i-1)*n + j - 1];
    : }
    :
    : //LenM
    : int matrix::LenM()
    : {
    : return n;
    : }
    : [/code]
    :
    : [code]
    : //lab1_2.cpp
    : //
    : #include
    : #include "liblab1_2.h"
    : #include "StdAfx.h"
    : using namespace std;
    : int main()
    : {
    : matrix mas(9);
    : [red]for (int i=0; i<mas.LenM(); i++)
    : for (int j=0; j<mas.LenM(); j++)
    : mas.SetM(i,j,9);[/red]
    :
    : for (i=0; i<mas.LenM(); i++)
    : {
    : for (int j=0; j<mas.LenM(); j++)
    : cout << mas.GetM(i,j) << " ";
    : cout << endl;
    : }
    : return 0;
    : }
    : [/code]
    :
    : [blue]One problem I see is your loop above (in red) where you are calling the [italic]SetM[/italic] function. The [italic]SetM[/italic] function assumes that incomming indicies are 1-based (i.e. the indicies start at 1) while the loop variables [italic]i[/italic] and [italic]j[/italic] start off at 0 which means that the [italic]SetM[/italic] function is calculating an invalid offset into your matrix. You either need to adjust your starting values for [italic]i[/italic] and [italic]j[/italic] in the loop to 1(and also change the < to <=), or you can keep those starting values and change the [italic]SetM[/italic] function to use 0 based indicies (which would be my choice). Your [italic]GetM[/italic] function might also need to be changed depending on what method you use.
    :
    : I'd personally overload the stream insertion operator << so that you could just output the matrix directly, i.e.:
    :
    : [code]
    : matrix mas(9);
    :
    : ...
    :
    : cout << mas;
    : [/code]
    : ...instead of your double nested [italic]for[/italic] loop.
    : [/blue]
    :
    :
    :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories