473,804 Members | 5,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help completing the code of program

5 New Member
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,,l ook at attachement)


Your help would greatly be appreciated

Thanks in advance..

Respectfully,,
Miss_Lolitta
Attached Files
File Type: txt cpp.txt (4.5 KB, 527 views)
Aug 7 '06 #1
11 2893
shinelakshmanan
13 New Member
can all the elements of a particular row except diagonal element have the same value......
Aug 7 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
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
miss_Lolitta
5 New Member
The code in attachment is not complete,becaus e 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 Recognized Expert Moderator Expert
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

FindPossibleOpt ion

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 FindPossibleOpt ion 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 FindPossibleOpt ion and the functions it calls.
Aug 7 '06 #5
miss_Lolitta
5 New Member
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 FindPossibleOpt ion

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 Recognized Expert Moderator Expert
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).

FindPossibleOpt ion
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 FindPossibleOpt ion 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 FindPossibleOpt ion 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 FindPossibleOpt ion 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 FindPossibleOpt ion(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 FindPossibleOpt ion it may need tweaking.
Aug 8 '06 #7
Banfa
9,065 Recognized Expert Moderator Expert
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
miss_Lolitta
5 New Member
thanks for all responding..
I'll try to rewrite code again..

thank u so much
Aug 13 '06 #9
D_C
293 Contributor
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

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

Similar topics

5
2168
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 than a standard app I have some questions: 1) The survey has 50 questions with answers being 1-10. Is it best to store all 50 questions and corresponding answers in a javascript array and then up all in a batch update? The downside to this...
34
18259
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 broke....." as they say. Anyway, everything's finally getting replaced by a new set-up, and there is just one program, an Access program running under Windows 95, which we want to change for the most minor of amendments (almost cosmetic). The guy...
0
2023
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 of the Loan in months (integer), and the Interest Rate (real number); the input data should be validity checked. The output should list a heading showing the input values, then a separate line indicating Payment Number, Loan Balance, Payment...
3
2159
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 inspiring... I've spent some hours thinking on what to program... but all which comes to me are accounts software.. which I find a bit boring, because so many already exist.. Basically I want to develop an application where must demonstrate Active...
4
3803
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 simple base64 encode routine and base64 decoding routine . My openssl version is 0.9.8a 11 Oct 2005 .
2
1569
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 click on link in side bar step moves without completing validation. thnx
2
2951
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. package prog1; import javax.swing.JOptionPane;
0
146
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 then he should at least contribute time or money to one of the free (as in free speech, not free beer) alternatives. Ivan Ven Osdel http://www.datasyncsuite.com/
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.