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
11 2893
can all the elements of a particular row except diagonal element have the same value......
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.
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
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.
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 -
// Transfer from Decimal to trinary
-
// no: decimal number
-
// row the array (started from 0 as usual with C and C++)
-
// size is over all size
-
// row StartInd is the starting point of filling the array
-
// EndInd is the end location
-
-
void DectoMat3(int no,Number* row, int size, int StartInd, int EndInd)
-
{
-
for(int k=EndInd; k>=StartInd; k--){
-
row[k] = no%BASE_NO;
-
no /= BASE_NO;
-
}
-
}
-
-
int Mat3toDec(Number* row, int size, int StartInd, int EndInd)
-
{
-
int fac = 1, power, res=0;
-
for(int k=EndInd; k>=StartInd; k--){
-
power = EndInd-k;
-
for(int j=1; j<=power; j++){
-
fac *= BASE_NO;
-
}
-
res += fac * row[k];
-
}
-
return(res);
-
}
-
-
-
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 -
int main(void)
-
{
-
Number **mat=NULL;
-
int size=0;
-
-
if(!(mat=CreateMat(&size))){
-
return(1);
-
}
-
//-->////Program start here
-
int i=0;
-
FindPossibleOption(mat[i], size, SUM, 1,size-1, TRUE);
-
PrintMat(mat, size);
-
while(FindPossibleOption(mat[i], size, SUM, 1, size-1, FALSE) ){
-
PrintMat(mat, size);
-
}
-
//-->// End of Program
-
-
ClearMat(mat, size);
-
getch();
-
return(0);
-
}
-
-
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 -
int main(void)
-
{
-
Number **mat=NULL;
-
int size=0;
-
-
if(!(mat=CreateMat(&size))){
-
return(1);
-
}
-
-
Number *vec = new Number[size];
-
-
if (vec == NULL) {
-
exit(EXIT_FAILURE);
-
}
-
-
// Set all row values to 0
-
memset((void *)vec, 0, sizeof(*vec) * size);
-
-
//-->////Program start here
-
int row = 0;
-
-
for(;;) {
-
BOOL rowOK = FindPossibleOption(mat, vec[row], size, SUM, row+1);
-
-
if ( rowOK ) {
-
if ( row == size-2 ) {
-
// penultimate row OK
-
// therefore matrix complete so print it
-
PrintMat(mat, size);
-
-
// Now look for next solution on previous row
-
row--;
-
vec[row]++;
-
}
-
else
-
{
-
// This row has a solution look for solution on next row
-
// Have to start search from beginning again
-
row++;
-
vec[row] = 0;
-
}
-
}
-
else {
-
if (row == 0) {
-
// No more solutions for row 1 so end of calculation
-
break;
-
}
-
else {
-
// No more solutions for current row
-
// look for next solution on previous row
-
row--;
-
vec[row]++;
-
}
-
}
-
}
-
//-->// End of Program
-
-
ClearMat(mat, size);
-
delete[] vec;
-
getch();
-
return(0);
-
}
-
Make sure that you understand how that loop I wrote works, also depending on how you re-write FindPossibleOpt ion it may need tweaking.
Banfa 9,065
Recognized Expert Moderator Expert
Opps the are 2 errors in my loop in the section dealing with a found solution -
if ( row == size-2 ) {
-
// penultimate row OK
-
// therefore matrix complete so print it
-
PrintMat(mat, size);
-
-
// Now look for next solution on previous row
-
row--;
-
vec[row]++;
-
}
-
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 -
if ( row == size-2 ) {
-
// penultimate row OK
-
// therefore matrix complete so print it
-
PrintMat(mat, size);
-
-
// Now look for next solution on previous row
-
vec[row]++;
-
}
-
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.
thanks for all responding..
I'll try to rewrite code again..
thank u so much
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. - int col(int c) // trim symmetric half and diagonal entries
-
{ // 0=>(0,1), N-1=>(0,N), and (N*(N-1))/2=>(N,N)
-
int dim = N-1;
-
int add = 0;
-
while(c > dim)
-
{
-
c -= dim;
-
dim--;
-
add++;
-
}
-
return (c+add);
-
}
-
- int row(int c); // same as col(int) except return (1+add);
-
-
// PROBLEM SOLVING ROUTINE
- solve(int index)
-
{
-
if(index == LENGTH) // LENGTH = (N*(N-1))/2
-
return;
-
-
for(j = 0; j <= B; j++)
-
{
-
unique_entries[index] = j; // unique_entries
-
row_sum[row(index)] += j;
-
col_sum[col(index)] += j;
-
-
// Because of matrix symmetry
-
row_sum[col(index)] += j;
-
col_sum[row(index)] += j;
-
-
check to see if all entries of row_sum and col_sum = T
-
if so, print matrix, then continue without returning.
-
-
if j != 0, see if any of the four modified is now greater than T.
-
if j != B, see if we are too far away from T, i.e. 000X or 001X.
-
// X is a don't care, because any value it takes, it won't help.
-
-
if either condition is violated, return;
-
else recursively call solve(index+1);
-
-
// Almost forgot
-
row_sum[row(index)] -= j;
-
col_sum[col(index)] -= j;
-
row_sum[col(index)] -= j;
-
col_sum[row(index)] -= j;
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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 .
| |
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
|
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;
|
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/
|
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,...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
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...
| |