|
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.).
|
|
|
|
|
Well, its a Win32 Client.
Well, its hard to explain, but the application allows family and friends to connect with each other, share photos, videos, music and games in a new way. At the moment I just concentrating on the client window, and the dialog's when users launch the application.
It's not really an instant messaging, its more of a communication and sharing platform where people can upload, watch and share all their multimedia and profiles.
Andrew McIntyre
|
|
|
|
|
MrMcIntyre wrote: Well, its hard to explain, but the application allows family and friends to connect with each other, share photos, videos, music and games in a new way. At the moment I just concentrating on the client window, and the dialog's when users launch the application.
oh boy ...
Ok, take a big breath ... I think you are aiming too high too early! That looks like an advanced project, not a beginner one.
Start from the basics, because it's not an easy projects to start with.
Get yourself a MFC beginner's book, and do the exercises, go steps by steps...
make connection between what you learn and what you want to do (user interface, behaviours, ... )
Try to emulate/replicate what you've learn and start from there.
This signature was proudly tested on animals.
|
|
|
|
|
I know I am only a beginner but I am not completing the project myself. I already have other people to finish the advanced stuff for the project off already I am only doing the basics if you know what I mean. It will all make sense later, you just need to trust me.
Thanks
Andrew McIntyre
|
|
|
|
|
Okay. I have started to write the code for the Win32 client, but I may need help along the way as was wondering if you guys will be willing to help me if I get stuck,
Thanks
Andrew McIntyre
|
|
|
|
|
When I wrote the code I had 1 failed error. Here the code for the Client, and if someone could tell me what I have done wrong.
#include <windows.h><br />
<br />
const char g_szClassName[] = "MyProject";<br />
<br />
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)<br />
{<br />
switch(msg)<br />
{<br />
case WM_CLOSE:<br />
DestroyWindow(hwnd);<br />
break;<br />
case WM_DESTROY:<br />
PostQuitMessage(0);<br />
default;<br />
return DefWindowProc(hwnd, msg, wParam, lParam);<br />
}<br />
return 0;<br />
}<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,<br />
LPSTR lpCmdLine, int nCmdShow)<br />
{<br />
WNDCLASSEX wc;<br />
HWND hwnd;<br />
MSG Msg;<br />
<br />
wc.cbSize = sizeof (WNDCLASSEX);<br />
wb.style = 0;<br />
wc.lpfnWndProc = WndProc;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br />
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);<br />
wc.lpszMenuName = NULL;<br />
wc.lpszClassName = g_szClassName;<br />
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);<br />
<br />
if(!RegisterClassEx(&wc))<br />
{<br />
MessageBox(NULL, "Window Registration Failed!", "Error",<br />
MB_ICONEXCLAMATION | MB_OK);<br />
return 0;<br />
}<br />
<br />
hwnd = CreateWindowEx(<br />
WS_EX_CLIENTEDGE,<br />
g_szClassName,<br />
"The title of my window",<br />
WS_OVERLAPPEDWINDOW,<br />
CW_USERDEFAULT, CS_USERDEFAULT, 903, 729,<br />
NULL, NULL, hInstance, NULL);<br />
<br />
if(hwnd == NULL)<br />
{<br />
MessageBox(NULL, "Window Creation Failed", "Error!",<br />
MB_ICONEXCLAMAION | MB_OK);<br />
return 0;<br />
}<br />
<br />
ShowWindow(hwnd, nCmdShow);<br />
UpdateWindow(hwnd);<br />
<br />
while(GetMessage(&Msg, NULL, 0,0) > 0)<br />
{<br />
TranslateMessage(&Msg);<br />
DispatchMessage(&Msg);<br />
}<br />
return Msg.wParam;<br />
}<br />
Thanks.
Andrew McIntyre
|
|
|
|
|
MrMcIntyre wrote: When I wrote the code I had 1 failed error
I get 5 or 6 when I try to compile this (VC6, 2003 Platform SDK). The switch statement is screwed up, you're using non-existing window styles, typos, etc.
All I see is a pretty standard Win32 application skeleton that just creates a window and a message loop. Well, it would if you fixed all the obvious errors.
Don't get me wrong - I don't want to discourage you from writing this program but you're getting way ahead of yourself. Once you really understand what the above code does (and know how to fix the errors) you can start putting in your own code.
modified on Sunday, October 4, 2009 5:42 PM
|
|
|
|
|
It's alright. I got the application to work perfectly now.
Andrew McIntyre
|
|
|
|
|