473,413 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Need help completing the code of program

Hi all,,
I am hoping that you will help me. I have trouble how I can complete the code of program that allows the user to input a number of integers such as N, and then stores them in an integer matrix where:

1- The dimension of matrix = NXN.
2-Entries of matrix can take any number from 0-2.
3- Arrangement of entries of first row is the same arrangement of entries of first column.
4-The diagonal of matrix =o.
5- Total of every row =4,Also Total of every column.

Then, the program displays all probabilities as table.


I tried to write this code but I can't complete it. just I want to find all probabilities for the rest of rows.I hope you don't have mind to help me to complete that.(please,,look at attachement)


Your help would greatly be appreciated

Thanks in advance..

Respectfully,,
Miss_Lolitta
Attached Files
File Type: txt cpp.txt (4.5 KB, 523 views)
Aug 7 '06 #1
11 2862
can all the elements of a particular row except diagonal element have the same value......
Aug 7 '06 #2
Banfa
9,065 Expert Mod 8TB
You question is not clear. For some reason you start talking about probabilities half way through with no previous reference.

The code appeared to assign 2 to every entry on the first row when I ran it.

You haven't actually stated what it is that the code should do, just some of the conditions.
Aug 7 '06 #3
The code in attachment is not complete,because I don't know how I can satisfied the rest of conditions?

more detail:
Idea of program:
1- this program compute how many ways can draw curves cross points" points is a number which input by user also it is the dimension of matrix".

2- From point to itself has no intersection, that is the diagonal of matrix =o ,,but from any point to another one can found more than one intersection (0-1-2).

************************************************** **************

How the program do?:

For example:

I- If user enter N=3 we have a following matrix

0 2 2
2 0 2
2 2 0

Which satisfying all conditions:
1- The dimension of matrix = 3×3
2-Entries of matrix can take any number from 0-2.
3- Arrangement of entries of first row is the same arrangement of entries of first column.
4-The diagonal of matrix =o.
5- Total of every row =4, Also Total of every column.


II- If user enter N=4 we have following matrices whose dimension is 4×4
***************************************
0 0 2 2
0 0 2 2
2 2 0 0
2 2 0 0
****************************************
0 2 0 2
2 0 2 0
0 2 0 2
2 0 2 0
****************************************
0 1 2 1
1 0 1 2
2 1 0 1
1 2 1 0
****************************************
0 2 2 0
2 0 0 2
2 0 0 2
0 2 2 0
****************************************
0 2 1 1
2 0 1 1
1 1 0 2
1 1 2 0
****************************************
0 1 1 2
1 0 2 1
1 2 0 1
2 1 1 0
****************************************

thanks for all..

regards,.
Lolitta
Aug 7 '06 #4
Banfa
9,065 Expert Mod 8TB
Ah I see so the program has to produce all matrices that satisfy the conditions, OK.

(just for information where you have used the word probabilities either combinations or solutions would have been better which is why I was a little confused).

Right we are getting somewhere so

You have fully describe the problem you are trying to solve
You have indicated that you have a problem, however you have not really described the problem except to post some code and state that it doesn't work. Really you should describe your problem too.


However I have look at your code and I will make some comments

FindPossibleOption

You have a problem in the way this function is defined/called. The first parameter is Number *, and you only ever call it with mat[i] where i=0, i.e. you only ever access the first line of your matrix.

You have come up against a common problem in C, handing round a dynamically create array of more than 1 dimension (in this case 2). In C I would solve this by using a array of only 1 dimension and just accessing like 2 dimensions.

However you have an alternitive, because you are using C++ you could create a matrix class and encapsulate the memory allocation and deallocation andhave a function to access the individual matrix cells. Then your functions could pass round either a pointer or reference to the class.

At the very least you need to pass Number ** to FindPossibleOption or iterate through all the rows in order to solve this problem.

The reset option to this function seems of little use, it sets the values of some static variables that are not used elsewhere in the code.


The internal workings of InitMat and ClearMat are a little odd since they work in BOOL where everything else works in Number.


Can you post the logic you think you are solving with the function FindPossibleOption and the functions it calls.
Aug 7 '06 #5
As you know the decimal system and binary system are numeral systems. They depend on a number we call the base number. For example the decimal system base number is 10. That means there are 10 numbers 0~9 and the base number needs 2 digits and will look like 1 beside a 0 that means 10. When the base number is 3 the system contain 3 digits 0~2 and the base number also will look like (10)3. In that case the system is called trinary system.

Hereby, I think it would be nice to cal that you want to fill the matrix using trinary numbers to satisfy other conditions. The question is why we may need that? The answer is to make all the possible options look like a sequence.

Example:
fill a vector of 4 values with the first is zero and the other three ranges from 0~2.
Sol: I will solve as it is decimal then transfer in trinary.
A0 A1 A2 A3 Trinary number Sum decimal
Always
0 0 0 (0)3 0
0 0 1 (1)3 1
0 0 2 (2)3 2
0 1 0 (3)3 1
0 1 1 (4)3 2
0 1 2 (5)3 3
0 2 0 (6)3 2
0 2 1 (7)3 3
0 2 2 (8)3 4
1 0 0 (9)3 1
1 0 1 (10)3 2
1 0 2 (11)3 3
1 1 0 (12)3 2
1 1 1 (13)3 3
1 1 2 (14)3 4
1 2 0 (15)3 3
1 2 1 (16)3 4
1 2 2 (17)3 5
2 0 0 (18)3 2
2 0 1 (19)3 3
2 0 2 (20)3 4
2 1 0 (21)3 3
2 1 1 (22)3 4
2 1 2 (23)3 5
2 2 0 (24)3 4
2 2 1 (25)3 5
2 2 2 (26)3 6

Note that 26 is number of digits in this case 3 power by the base number also 3.

Total options is (digits)base =27 (from 0 to 26(

So what we did now? We just could generate all possibilities of that three numbers using one loop. In C++ terminology we need to transferee from decimal to trinary and vise versa. But the binary should be in the matrix form to get the index of the digits.

Here are two functions made for that purpose

Expand|Select|Wrap|Line Numbers
  1. // Transfer from Decimal to trinary
  2. // no: decimal number
  3. // row the array (started from 0 as usual with C and C++)
  4. // size is over all size 
  5. //  row StartInd is the starting point of filling the array 
  6. // EndInd is the end location 
  7.  
  8. void DectoMat3(int no,Number*  row, int size, int StartInd, int EndInd)
  9. {
  10.     for(int k=EndInd; k>=StartInd; k--){
  11.         row[k] = no%BASE_NO;
  12.         no /= BASE_NO;
  13.     }
  14.  
  15. int Mat3toDec(Number*  row, int size, int StartInd, int EndInd)
  16. {
  17.     int fac = 1, power, res=0;
  18.     for(int k=EndInd; k>=StartInd; k--){
  19.         power = EndInd-k;
  20.         for(int j=1; j<=power; j++){
  21.             fac *= BASE_NO;
  22.         }
  23.         res += fac * row[k];
  24.     }
  25.     return(res);
  26.  
  27.  
  28.  

if we talk about case N=4,we get some solutions for first row which satisfy sum =4 from elements 0,1,2 as:

2 2 0 0
2 0 2 0
2 1 1 0
1 2 1 0
1 1 2 0
0 2 2 0

this is a first step,find all solutions for first row.let us write this operation at function FindPossibleOption

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     Number **mat=NULL;
  4.     int size=0;
  5.  
  6.     if(!(mat=CreateMat(&size))){
  7.         return(1);
  8.     }
  9. //-->////Program start here
  10.     int i=0; 
  11.     FindPossibleOption(mat[i], size, SUM, 1,size-1, TRUE);
  12.     PrintMat(mat, size);
  13.     while(FindPossibleOption(mat[i], size, SUM, 1, size-1, FALSE) ){
  14.         PrintMat(mat, size);
  15.     }
  16. //-->// End of Program
  17.  
  18.     ClearMat(mat, size);
  19.     getch();
  20.     return(0);
  21. }
  22.  
  23.  
Aug 8 '06 #6
Banfa
9,065 Expert Mod 8TB
Wow! I think that has to be about the most compete answer I have ever received on these forums :D

OK Here are my suggestions (these may just be the first step to a solution not the actual solution).

FindPossibleOption
Actually I am very impressed with the maths and logic, however you need to modify this function, currently it finds the first row for all solutions, it needs to be capable of finding all rows for all solutions. Here are some of the things that are blocking that

The variable j (this is extremely poorly named, for a variable as important as this is to the working of your code you should give it a meaningful name). This variable is a major stubling block to solving the problem because the is only 1 of it. However I little thought indicates that you will need 1 for each row (because for any given solution for row 1 there may be more than 1 solution for the rest of the rows) so the data that controls the logic (j and count) need to be held for each row currently being processed and since each row is dependent on all previous rows (because the matrix is reflected on it's diagonal) that means that when solving the penultimate row you will be in the middle of solving all rows.

So the mechanism for storing j needs to external to FindPossibleOption and needs to be a vector of N-1 values for an NxN array. (N-1 because you never have to solve the last row).

Also insolving a row you solve a column so FindPossibleOption needs access to the whole matrix so it can fill in the columns as well as the rows as it solves them.

Finally EndInd will always be size-1 and since you pass in size you do not need to pass in EndInd.

So you need to rewrite FindPossibleOption so that it doesn't contain the definition and initialisation of j and count (NOTE bits does not need to be static), it can handle rows other than row 1 and it does fill in the column solved as well as the row solved. It might have a prototype that looks like

BOOL FindPossibleOption(Number **matrix, Number &decimal, int size, int rowSum, int StartInd);


Then in main your loop is not complex enough, everytime you find a solution to row 1 you have to continue and find a solution to all the other rows something like

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     Number **mat=NULL;
  4.     int size=0;
  5.  
  6.     if(!(mat=CreateMat(&size))){
  7.         return(1);
  8.     }
  9.  
  10.     Number *vec = new Number[size];
  11.  
  12.     if (vec == NULL) {
  13.         exit(EXIT_FAILURE);
  14.     }
  15.  
  16.     // Set all row values to 0
  17.     memset((void *)vec, 0, sizeof(*vec) * size);
  18.  
  19. //-->////Program start here
  20.     int row = 0;
  21.  
  22.     for(;;) {
  23.         BOOL rowOK = FindPossibleOption(mat, vec[row], size, SUM, row+1);
  24.  
  25.         if ( rowOK ) {
  26.             if ( row == size-2 ) {
  27.                 // penultimate row OK 
  28.                 // therefore matrix complete so print it
  29.                 PrintMat(mat, size);
  30.  
  31.                 // Now look for next solution on previous row
  32.                 row--;
  33.                 vec[row]++;
  34.             }
  35.             else
  36.             {
  37.                 // This row has a solution look for solution on next row
  38.                 // Have to start search from beginning again
  39.                 row++;
  40.                 vec[row] = 0;
  41.             }
  42.         }
  43.         else {
  44.             if (row == 0) {
  45.                 // No more solutions for row 1 so end of calculation
  46.                 break;
  47.             }
  48.             else {
  49.                 // No more solutions for current row
  50.                 // look for next solution on previous row
  51.                 row--;
  52.                 vec[row]++;
  53.             }
  54.         }
  55.     }
  56. //-->// End of Program
  57.  
  58.     ClearMat(mat, size);
  59.     delete[] vec;
  60.     getch();
  61.     return(0);
  62. }
  63.  
Make sure that you understand how that loop I wrote works, also depending on how you re-write FindPossibleOption it may need tweaking.
Aug 8 '06 #7
Banfa
9,065 Expert Mod 8TB
Opps the are 2 errors in my loop in the section dealing with a found solution

Expand|Select|Wrap|Line Numbers
  1.             if ( row == size-2 ) {
  2.                 // penultimate row OK 
  3.                 // therefore matrix complete so print it
  4.                 PrintMat(mat, size);
  5.  
  6.                 // Now look for next solution on previous row
  7.                 row--;
  8.                 vec[row]++;
  9.             }
  10.  
The first is that there may be further solutions on the current row, we don't want to start looking at the previous row, this is easy to fix

Expand|Select|Wrap|Line Numbers
  1.             if ( row == size-2 ) {
  2.                 // penultimate row OK 
  3.                 // therefore matrix complete so print it
  4.                 PrintMat(mat, size);
  5.  
  6.                 // Now look for next solution on previous row
  7.                 vec[row]++;
  8.             }
  9.  
The second is that although we do not need to calculate the last row we do need to check that it meets all the requirements (adds up to 4) before we print the solution, I will leave you to write this code.
Aug 8 '06 #8
thanks for all responding..
I'll try to rewrite code again..

thank u so much
Aug 13 '06 #9
D_C
293 100+
Wow this is an interesting problem. I was looking at a recursive way, and I think I found how it could be done with a balance of brute force and elegance. If you solved it, though, keep it the way you have it. Also, I haven't actually ran the code, so if there are any errors, you need to understand the code to fix it, which isn't such a bad thing :D.

Matrix dimension N, Max Increment B (identically 2), total value T (identically 4).
  • int array of size (N*(N-1))/2, for unique matrix entries
  • two int arrays of size N, for row and column sums
Actually each array entry needs at most ceiling(log base 2 (T+B)). This would be three bits for your scenario, B=2 and T =4.

Using some nested loops and recursion, you can make a pretty algorithm that would look quite ugly if followed on paper. Don't forget, unique_entries, row_sum and col_sum data should be static, so they may be accessed from the middle of the code. It would be wasteful to pass it every time.
Expand|Select|Wrap|Line Numbers
  1. int col(int c) // trim symmetric half and diagonal entries
  2. {              // 0=>(0,1), N-1=>(0,N), and (N*(N-1))/2=>(N,N)
  3.   int dim = N-1;
  4.   int add = 0;
  5.   while(c > dim)
  6.   {
  7.     c -= dim;
  8.     dim--;
  9.     add++;
  10.   }
  11.   return (c+add);
  12. }
  13.  
  14. int row(int c); // same as col(int) except return (1+add);
  15.  
  16. // PROBLEM SOLVING ROUTINE
  17. solve(int index) 
  18. {
  19.   if(index == LENGTH) // LENGTH = (N*(N-1))/2
  20.     return;
  21.  
  22.   for(j = 0; j <= B; j++)
  23.   {
  24.     unique_entries[index] = j; // unique_entries
  25.     row_sum[row(index)] += j;
  26.     col_sum[col(index)] += j;
  27.  
  28.     // Because of matrix symmetry
  29.     row_sum[col(index)] += j;
  30.     col_sum[row(index)] += j;
  31.  
  32.     check to see if all entries of row_sum and col_sum = T
  33.     if so, print matrix, then continue without returning.
  34.  
  35.     if j != 0, see if any of the four modified is now greater than T.
  36.     if j != B, see if we are too far away from T, i.e. 000X or 001X.
  37.     // X is a don't care, because any value it takes, it won't help.
  38.  
  39.     if either condition is violated, return;
  40.     else recursively call solve(index+1);
  41.  
  42.     // Almost forgot
  43.     row_sum[row(index)] -= j;
  44.     col_sum[col(index)] -= j;
  45.     row_sum[col(index)] -= j;
  46.     col_sum[row(index)] -= j;
  47.   }
  48. }
Aug 16 '06 #10
Thanks sooosooo much for help..

Really,I don't know how can arrange all ideas in one code there are more than idea in one program. :( I'm confused.. :confused: :confused: this program needs to Fills in the diagonal values of matrix with zeros then Total of every row and column must be = 4 and Entries of matrix should take symmetry around the diagonal..i.e there are some conditions should be satisfied in this program. :mad:

once again thanks so much for all..

regards..Miss. Lolitta
Aug 16 '06 #11
D_C
293 100+
It did, see. It prints in an unpretty line manner. N = 3 has 1 solution, N = 4 has 6 solutions, N = 5 has 73 solutions, N = 6 has 1690 solutions.

To understand the output, assume the solution to a 5x5 is printed:
A B C D E F G H I J
Expand|Select|Wrap|Line Numbers
  1. 0 A B C D
  2. A 0 E F G
  3. B E 0 H I
  4. C F H 0 J
  5. D G I J 0
That is how to interpret it as a matrix.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. static const int B = 2; // entries take values 0-2
  5. static const int T = 4; // each row/col sums to 4
  6. static const int N = 5; // dimension of matrix
  7. static const int LENGTH = (N*(N-1))/2;
  8.  
  9. static int unique_entries[LENGTH];
  10. static int sums[2*N]; // 1st half is rows, 2nd half is columns
  11.  
  12. int col(int c) // trim symmetric half and diagonal entries
  13. {              // 0=>(0,1), N-1=>(0,N), and LENGTH=>(N,N)
  14.   int dim = N-1;
  15.   int add = 1;
  16.   while(c >= dim)
  17.   {
  18.     c -= dim;
  19.     dim--;
  20.     add++;
  21.   }
  22.   return c+add;
  23. }
  24.  
  25. int row(int c)
  26. {
  27.   int dim = N-1;
  28.   int add = 0;
  29.   while(c >= dim)
  30.   {
  31.     c -= dim;
  32.     dim--;
  33.     add++;
  34.   }
  35.   return add;
  36. }
  37.  
  38. bool valid(int index)
  39. {
  40.   bool b = true;
  41.  
  42.   b &= (sums[row(index)] <= T);
  43.   b &= (sums[col(index)] <= T);
  44.   b &= (sums[N+row(index)] <= T);
  45.   b &= (sums[N+col(index)] <= T);
  46.  
  47. // Could also check whether we have underestimated.
  48. // For example, 000X or 001X, although it may not
  49. // save any considerable time.
  50.  
  51.   return b;
  52. }
  53.  
  54. bool correct()
  55. {
  56.   int i = 0;
  57.   bool b = true;
  58.   while(b && (i < 2*N))
  59.   {
  60.     if(sums[i] != T)
  61.       b = false; // break;
  62.     i++;
  63.   }
  64.   return b;
  65. }
  66.  
  67. void print_ents()
  68. {
  69.   int i;
  70.   for(i=0; i < LENGTH; i++)
  71.     cout << unique_entries[i] << " ";
  72.   cout << endl;
  73. }
  74.  
  75. // PROBLEM SOLVING ROUTINE
  76. void solve(int index)
  77. {
  78.   if(index == LENGTH) // LENGTH = (N*(N-1))/2
  79.   {
  80.     if(correct())
  81.       print_ents();
  82.  
  83.     return;
  84.   }
  85.  
  86.   for(int j = 0; j <= B; j++)
  87.   {
  88.     unique_entries[index] = j; // unique_entries
  89.  
  90.     sums[row(index)] += j;
  91.     sums[col(index)] += j;
  92.     sums[N+row(index)] += j;
  93.     sums[N+col(index)] += j;
  94.  
  95.     if(valid(index))
  96.       solve(index+1);
  97.  
  98.     sums[row(index)] -= j;
  99.     sums[col(index)] -= j;
  100.     sums[N+row(index)] -= j;
  101.     sums[N+col(index)] -= j;
  102.   }
  103.  
  104.   unique_entries[index] = 0;
  105.   return;
  106. }
  107.  
  108. int main()
  109. {
  110.   solve(0);
  111.  
  112.   system("PAUSE");
  113.   return 0;
  114. }
Aug 17 '06 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dan | last post by:
Hi, I want to migrate a survey program to web application and have selected PHP to use a on the server end along with apache and firebird rdms. As developing a web application is much different...
34
by: John Harrison | last post by:
An odd confession; an odd request; but here's the tale.... My company has a few PC systems which we have used for about 7 years and not updated - we've "made do", and besides, "if it ain't...
0
by: redmondtab | last post by:
I have done this . Write a C program that will accept keyboard input and display a loan amortization schedule back on the screen. The program should ask for the Loan Amount (real number), Term...
3
by: Jeff | last post by:
Hey ..NET 2.0 I'm about to start learning C# 2.0. I want to develop an C# 2.0 application which I can use to impress my employer. I think it's much more fun to program on something I find...
4
by: pycraze | last post by:
Hi , I am working on Fedora core 5 and my OS version is 2.6.15-1.2054_FC5 . My GCC version is 4.1.1 20070105 (Red Hat 4.1.1-51) . I am currently using the openssl libraries to write a...
2
by: sudhashekhar30 | last post by:
hi all i have textbox in step in wizard control(step-10). there are 10 steps. so i want validation on every page and user can't move to other step without completing all correct entry. when i...
2
by: aszush | last post by:
//Title: Programming Assignment 1 //Version: //Copyright: Copyright (c) 1999 //Author: Andrew //Company: //Description: Computes employee's weekly gross and net pay....
0
by: Ivan Ven Osdel | last post by:
According to the site, Wings 101 doesn't support "Auto-completion for Python and extension modules" but I agree that Wings is worth it if he (or his company) can afford the license(s). If not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.