|
holyinferno wrote: for(i=0;i
I assume something got missed in copy/paste, as this statement will not compile.
if(adj[start][i] && !visited[i])
This statement is a compound boolean expression, which will be true if the item in the 'adj' array is true AND the item in the 'visited' array is false.
From your question I would suggest it a good idea to get a C or C++ primer (book or online) to get a basic understanding of statements, operators, arrays, etc before trying to follow a complete program.
holyinferno wrote: Sorry for being a noob.
We were all noobs once Keep at it, you are obviously making the effort to learn.
|
|
|
|
|
I am actually following this program here http://c4swimmers.net/portal/bfs[^]
The statement is at is in the program. What do you mean by true? Does it mean the item exists in the arrays ? I understand compound statements but I am confused as there is not statement like (if a=b or if a>b) involved here.
Thanks for your reply !
Mike
|
|
|
|
|
holyinferno wrote: What do you mean by true?
Sorry for not elaborating my response. In C/C++ an element (simple, or array) is considered true if it has a non-zero value, and false if it is zero. So the statement
if (adj[root][i] && !visited[i])<br /> could be read as if the element at adj[root][i] has a non-zero value AND if the element at visited[i] has a zero value (the '!' means NOT so reverses the test).
[EDIT]edited for clarity, I hope![/EDIT]
|
|
|
|
|
Thanks mate, that cleared the confusions a little bit.
Mike.
|
|
|
|
|
holyinferno wrote: Does it mean the item exists in the arrays ?
Nope. It means: "the array element (an integer) is non-zero".
holyinferno wrote: (if a=b or if a>b)
Beware a=b is an assignment statement in C/C++ , the equality test operator is == .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
Your code snippet would be much easier to read like:
for (i = 0; i < N; i++)
{
if (adj[start][i] && ! visited[i])
{
q[++rear] = i;
visited[i] = 1;
}
}
holyinferno wrote: What is this if statement comparing ?
It is asking if the i th element of adj[start] is non-zero and if the i th element of visited is zero. If both of those conditions evaluate to true, the two statements within the curly brackets are executed.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello everybody,
In the code below there's a minor mistake. I can't find it. The whole code works fine, but in fact doesn't give a right result. Number of combination
6 out of 10 equals 210, but the program shows only 209 of them. What should I do ?
Here's the complete code:
#include "IndexCombination.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <sstream>
using namespace std;
using namespace stdcomb;
bool GetNextComb(std::vector<unsigned int> &vi);
struct Element
{
char name[6];
int numbers[30];
char chain[90];
};
const Element elements[] = {
{
"ia11",
{3,9,17,21,24,31,33,36,42,49,4,8,19,22,28,30,34,39,43,47,2,6,10,13,14,25,29,37,38,46},
"3,9,17,21,24,31,33,36,42,49,4,8,19,22,28,30,34,39,43,47,2,6,10,13,14,25,29,37,38,46"
}, {
"ia34",
{1,6,12,15,22,27,31,32,41,42,4,7,14,17,23,30,33,36,45,48,2,9,13,18,21,26,34,39,44,49},
"1,6,12,15,22,27,31,32,41,42,4,7,14,17,23,30,33,36,45,48,2,9,13,18,21,26,34,39,44,49"
}, {
"ia72",
{3,8,11,18,22,25,36,37,43,46,1,6,16,17,23,28,35,40,41,44,2,5,12,19,24,27,31,34,42,49},
"3,8,11,18,22,25,36,37,43,46,1,6,16,17,23,28,35,40,41,44,2,5,12,19,24,27,31,34,42,49"
}, {
"ia167",
{3,4,12,15,21,28,32,39,47,48,6,9,16,17,29,30,31,38,41,42,7,8,11,20,26,27,34,35,45,46},
"3,4,12,15,21,28,32,39,47,48,6,9,16,17,29,30,31,38,41,42,7,8,11,20,26,27,34,35,45,46"
}, {
"ia190",
{3,10,11,14,22,23,32,35,43,44,6,9,16,19,24,29,37,38,45,48,2,5,13,18,25,30,31,40,41,0},
"3,10,11,14,22,23,32,35,43,44,6,9,16,19,24,29,37,38,45,48,2,5,13,18,25,30,31,40,41,0"
}, {
"ia21",
{2,5,16,19,25,30,34,39,45,48,1,9,12,13,21,24,33,36,44,49,3,8,15,20,23,26,35,40,43,46},
"2,5,16,19,25,30,34,39,45,48,1,9,12,13,21,24,33,36,44,49,3,8,15,20,23,26,35,40,43,46"
}, {
"ia64",
{6,9,13,20,28,29,33,40,43,48,2,7,16,19,24,27,31,34,44,47,5,8,14,17,21,30,32,37,41,46},
"6,9,13,20,28,29,33,40,43,48,2,7,16,19,24,27,31,34,44,47,5,8,14,17,21,30,32,37,41,46"
}, {
"ia102",
{4,9,14,19,22,29,35,38,46,47,5,8,13,16,21,26,33,40,41,48,6,7,15,17,28,30,32,39,42,45},
"4,9,14,19,22,29,35,38,46,47,5,8,13,16,21,26,33,40,41,48,6,7,15,17,28,30,32,39,42,45"
}, {
"ia178",
{6,9,15,20,23,24,37,38,42,45,7,8,16,19,22,25,34,39,43,46,1,2,17,18,26,29,31,40,47,0},
"6,9,15,20,23,24,37,38,42,45,7,8,16,19,22,25,34,39,43,46,1,2,17,18,26,29,31,40,47,0"
}, {
"ia180",
{1,4,13,18,27,30,33,38,45,46,2,3,12,15,22,23,39,40,44,47,8,9,11,17,24,25,32,37,49,0},
"1,4,13,18,27,30,33,38,45,46,2,3,12,15,22,23,39,40,44,47,8,9,11,17,24,25,32,37,49,0"
}
};
int main(int argc, char *argv[])
{
CIdxComb cb;
cb.SetSizes(10,6);
vector<unsigned int> combination(6);
combination[0] = 0;
combination[1] = 1;
combination[2] = 2;
combination[3] = 3;
combination[4] = 4;
combination[5] = 5;
int Total = 0;
while(cb.GetNextComb(combination))
{
Total++;
cout << "Combination number. " << Total << endl;
for (unsigned i = 0; i < combination.size(); i++)
{
const Element &element = elements[combination[i]];
cout << element.name << ": " << element.chain << endl;
}
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
|
|
|
|
|
Waldemar Ork wrote: GetNextComb
the function name is well chosen.
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Waldemar Ork wrote: The whole code works fine, but in fact doesn't give a right result.
In software (or anywhere else) that statement cannot be true.
Also your code is badly formatted, try using the <pre></pre> tags or the "code block" button to display it like this
while(cb.GetNextComb(combination))
{
}
In the extract you posted the function GetNextComb() is not shown, but does not appear to return a value; what do you expect from that call?
|
|
|
|
|
apparently GetNextComb modifies its parameter from one combination to the next (that is OK, it is passed by reference), and returns false when it reaches the end.
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Your remark about GetNextCombination function is precious. I think I should modify the header file. What I think of is to apply somehow the rule "one element past the last". This should help.
Anyway here's the header file with GetNextCombination function if you'd like to have a look at it :
#include "IndexCombination.h"
using namespace stdcomb;
void CIdxComb::Init( unsigned int SetSize, unsigned int CombSize )
{
// Assign CombSize
////////////////////////
if( CombSize == 0 )
CombSize = 1;
m_ArrSize = CombSize;
m_LastIdx = CombSize - 1;
// Assign SetSize
////////////////////////
if( SetSize == 0 )
SetSize = 2;
if( CombSize > SetSize )
CombSize = SetSize;
m_SetSize = SetSize;
m_LastSetIdx = SetSize - 1;
}
bool CIdxComb::SetSizes( unsigned int SetSize, unsigned int CombSize )
{
if( SetSize == 0 )
return false;
if( CombSize == 0 )
return false;
if( CombSize > SetSize )
return false;
m_ArrSize = CombSize;
m_LastIdx = CombSize - 1;
m_SetSize = SetSize;
m_LastSetIdx = SetSize - 1;
return true;
}
bool CIdxComb::GetNextComb( std::vector<unsigned int> &vi )
{
// Check if the last element is at the end
if( vi[m_LastIdx] == m_LastSetIdx )
{
if( m_ArrSize == 1 ) // Completed
return false;
// Check if the subsequent elements(counted from back)
// is also at their subsequent positions
//////////////////////////////////////////////////////
bool Completed = true;
// Incomplete Index, init value not used
unsigned int IncompIdx = m_LastIdx - 1;
bool FirstIdx = false;
unsigned int ArrIdx = m_LastIdx - 1;
unsigned int SetIdx = m_LastSetIdx - 1;
while( !FirstIdx )
{
if( vi[ArrIdx] != SetIdx )
{
Completed = false;
IncompIdx = vi[ArrIdx] + 1;
break;
}
if( SetIdx )
--SetIdx;
if( !ArrIdx )
FirstIdx = true;
else
--ArrIdx;
}
if( Completed )
return false;
else
{
for( unsigned int i=ArrIdx; i<=m_LastIdx; ++i, ++IncompIdx )
{
vi[i] = IncompIdx;
}
}
}
else if ( vi[m_LastIdx] < m_LastSetIdx )
{
(vi[m_LastIdx])++;
}
else // bigger than the m_LastIdx! Impossible!
{
return false;
}
return true;
}
|
|
|
|
|
Waldemar Ork wrote: one element past the last
I have no idea what that could mean.
I would assume GetNextComb() returns the next combination, so if there are N you can call it successfully N-1 times, I see no problem here.
I did not study your code as it is not formatted (why don't you use PRE tags?) and it also is a lot of code, much more than I would anticipate for what it is supposed to do.
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
As has been suggested before here, you need to trim this code down to just what is absolutely necessary to exhibit the problem.
If you are seeing 209 out of 210 combinations (i.e., one-off bug), check the while() and for() loops to ensure they are running the expected number of times.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Unfortunately, You all are wrong. The number of combinations has nothing to do with GetNextCombination or "for" loop. The loop "do...while" is needed instead. Here's the code:
do {
Total++;
cout << "Combination number. " << Total << endl;
for (unsigned i = 0; i < combination.size(); i++)
{
const Element &element = elements[combination[i]];
cout << element.name << ": " << element.chain << endl;
}
}
while(cb.GetNextComb(combination));
Thank you and,
Take care!
|
|
|
|
|
Hi,
I am looking for someone to help me with a problem I am having creating a Win32 Client and to help me along the way creating windows for the project which is driving me crazy.
If anyone has spare time that could help me quickly that would be great.
Thanks
Andrew
Andrew McIntyre
|
|
|
|
|
A Win32 client for what ?
Can't you simply create a "new" project with a Wizard ? either a dialog based application or a SDI/MDI application ?
This signature was proudly tested on animals.
|
|
|
|
|
For my project.
No, I have created a "new" project and went through the wizard already. But I am stuck a quarter through the code.
Andrew McIntyre
|
|
|
|
|
MrMcIntyre wrote: But I am stuck a quarter through the code.
A quarter of what code? Your code? The Wizard code? The Da Vinci code?
|
|
|
|
|
The source code.
Andrew McIntyre
|
|
|
|
|
Right.
From you original post (and some of your previous ones) I gather that you are looking for some kind of tutor.
You have several options:
1. Buy some books and teach yourself programming
2. Pay someone to teach you
3. Find someone who would do that for free (highly unlikely although this appears to be your preferred option)
4. Pay someone to write the code for you.
5. ...
|
|
|
|
|
No I don't need someone to write the code I need someone that can help me if I get stuck.
Andrew McIntyre
|
|
|
|
|
MrMcIntyre wrote: No I don't need someone to write the code I need someone that can help me if I get stuck.
Fair enough. That's exactly what this site is all about. So, why don't you post the relevant part of your code with a description of what it is supposed to do and where you are stuck?
|
|
|
|
|
Thanks.
#include "MyProject.h"<br />
<br />
#define MAX_LOADSTRING 100<br />
<br />
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
<br />
Well, I am trying to get the code to create a Win32 client, with certain dimensions and to centre it to screen, but I have missed half of the code out because I have got stuck.
Andrew McIntyre
|
|
|
|
|
What kind of client?
I suggest you tell us what your app is supposed to do and how you want to implement it (MDI/SDI/Dialog based, etc.).
|
|
|
|
|