how to make this program more general ?

This is a discussion on how to make this program more general ? within the C++ Programming forums, part of the General Programming Boards category; The problem I need to solve is simple to expose. Given two strictly positive integers m and n, I want ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    how to make this program more general ?

    The problem I need to solve is simple to expose. Given two strictly positive integers m and n, I want to generate and count all the matrix of dimension m (that is arrays with m lines and m columns) such that both
    * each element is non negative
    * the sum of elements in every line and in every column equals n.

    I wrote pograms that generate and count these matrix for n as a variable but not for any dimension m. Here are programs for m=3 and for m=4. It works for any value of m.

    Does anyone have a suggestion that would allow me to write a general program that works for any value of n ? How can I avoid the fact that the number of loops increases dramaticaly with n ?? Any suggestion would be of great help to me.


    first program : for m=3 :


    #include <iostream.h>

    int max(int,int);

    int main()
    {
    unsigned long u; // counter of matrix of dimension 3 with the
    // two properties
    int n; // dimension

    cout<<"m=3. Enter n, the dimension of the matrix :";
    cin>>n;

    int c[1][1]; // c denotes any matrix with dimension m=3
    //property of the sum enables us to work with a
    // reduced dimension (2 instead of 3)
    for (c[0][0]=0; c[0][0]<=n; c[0][0]++)
    {
    for (c[0][1]=0; c[0][1]<=n-c[0][0]; c[0][1]++)
    {
    for (c[1][0]=0; c[1][0]<=n-c[0][0]; c[1][0]++)
    {
    for c[1][1]=0;c[1][1]<=n-max(c[0][1], c[1][0]);c[1][1]++)
    {
    if (c[0][0]+c[0][1]+c[1][0]+c[1][1]>=n)
    // this condition ensures that elements of the last row and
    // column are positive and that every line and column has a
    //total equals to n. It is the mathematical traduction that the
    //last element of the matrix c[2][2] is non negative, that is that the sum of elements of the submatrix without last row an column is at least equal to (m-2)*n.
    {
    u=u+1;
    }
    }

    }

    }

    }

    }

    cout<<"there exist "<<u<<"matrix with dimension 3 with positive elements and such that the total of every lign and every column equals"<<n;

    return 0;
    }

    int max (intx,int y)
    {
    if (x<y) return y;
    else return x;
    }





    second program : for m=4 :


    #include <iostream.h>

    int max(int,int);

    int main()
    {
    unsigned long u; // counter of matrix of dimension 4 with the
    // two properties
    int n; // dimension

    cout<<"m=4. Enter n, the dimension of the matrix :";
    cin>>n;

    int c[2][2]; // c denotes any matrix with dimension m=3
    //property of the sum enables us to work with a
    // reduced dimension (3 instead of 4)

    for (c[0][0]=0; c[0][0]<=n; c[0][0]++)
    {
    for (c[0][1]=0; c[0][1]<=n-c[0][0]; c[0][1]++)
    {
    for (c[0][2]=0; c[0][2]<=n-c[0][0]-c[0][1]; c[0][2]++)
    {
    for (c[1][0]=0; c[1][0]<=n-c[0][0]; c[1][0]++)
    {
    for (c[2][0]=0; c[2][0]<=n-c[0][0]-c[1][0]; c[2][0]++)
    {
    for c[1][1]=0;c[1][1]<=n-max(c[0][1], c[1][0]);c[1][1]++)
    {
    for (c[2][1]=0; c[2][1]<=n-max(c[0][1]+c[1][1], c[1][0]);
    c[2][1]++)
    {
    for (c[1][2]=0; c[1][2]<=n-max(c[1][0]+c[1][1],
    c[0][1]); c[1][2]++)
    {
    for (c[2][2]=0; c[2][2]<=n-max(c[0][2]+c[1][2],
    c[2][0]+c[2][1]); c[2][2]++)
    {
    if (c[0][0]+c[0][1]+c[0][2]+c[1][0]+c[1][1]+c[1][2]
    +c[2][0]+c[2][1]+c[2][2]>=2*n)
    // this condition ensures that elements o the last row and
    // column are positive and that every line and column has a
    //total equals to n. It is the mathematical traduction that the
    //last element of the matrix c[3][3] is non negative, that is that the sum of elements of the submatrix without last row an column is at least equal to (m-2)*n.
    {
    u=u+1;
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }

    cout<<"there exist "<<u<<"matrix with dimension 4 with positive elements and such that the total of every lign and every column equals"<<n;

    return 0;
    }

    int max (intx,int y)
    {
    if (x<y) return y;
    else return x;
    }

  2. #2
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Exclamation

    too advanced for me by far sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this program I'm trying to make
    By Sshakey6791 in forum C++ Programming
    Replies: 14
    Last Post: 12-01-2008, 04:03 PM
  2. Replies: 9
    Last Post: 06-17-2008, 12:38 PM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. any suggestions on how to make this program smaller?
    By bajan_elf in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 04:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21