nucleon 114 Posting Pro in Training

So you mean something like this?

class ArtClass {
};

class School {
    ArtClass* m_artclass;
public:
    School(ArtClass* artclass) { m_artclass = artclass; }
};

class Main {
    School*   m_school;
    ArtClass* m_artclass;
public:
    Main() {
        m_artclass = new ArtClass();
        m_school = new School(m_artclass);
    }
    ~Main() {
        delete m_school;
        delete m_artclass;
    }
};
nucleon 114 Posting Pro in Training

Your code is spaced all over the place. Mixed spaces and tabs?

If your basic question is how to initialize reference members of a class, you do it like this:

class Class { /*...*/ };

class Class2 {
    Class& m_class_ref;
    //...
public:
    Class2(Class& class_ref);
    //...
};

Class2::Class2(Class& class_ref) : m_class_ref(class_ref) {
    //...
}
nucleon 114 Posting Pro in Training

Remember to always look for punctuation errors near the error line. Here you forgot the semicolon after enum cmp_t (after the closing brace).

nucleon 114 Posting Pro in Training

Try g++ (c++ compiler) instead of gcc (c compiler).

nucleon 114 Posting Pro in Training

> it takes so much of time to find a number greater
> than searching number

That's the heart of the problem. How do you decrease that time? You definitely don't want to do a linear search!

I guess I understand the "infinity" stuff. It means that you can access any index whatsoever without an error. This could be implemented as a function that returns the element value for indices up to n-1 but returns "infinity" (say max int) otherwise.

The question is basically "how do you do a binary search (or similar) on an infinite array"? Obviously you can't start in the "middle", so you need to consider other options.

nucleon 114 Posting Pro in Training

I think you'll need something like this:

typedef vector<Word*> WordList;
class Word : public string // either inherit from string or contain one
{
    vector<WordList*> v;
public:
    ...
};

This amounts to a vector of vectors, which I think you may need.

nucleon 114 Posting Pro in Training

I was thinking she needed something more complicated too, but now that I look again, maybe she is looking for an n-ary tree. Taking her example, starting with a given root word (e.g. "cat"), generate all words with one letter different:

* for each letter in word
  * go through all possible letters
  * if you form a word already in the tree, store a pointer
    to it
  * add new words (found in dictionary) to the list for
    this tree node

Then go through all new words, changing only letters that haven't yet been changed, checking for the words' preexistence in the tree, checking if it's a word (in the dictionary), adding it if it is.... This would create an n-ary tree, perhaps with some extra structure.

Maybe that's the kind of thing she's looking for.

nucleon 114 Posting Pro in Training

How can 8 be fine when the string you give in demonstration is 15 characters long? Also you're calling strftime with a maximum of 80. Making the hwnd global works for me:

#include <windows.h>
#include <ctime>
#include "resource.h"

static bool keepRunning = true;
static HANDLE hThread = NULL;
static void run();
static void end();
static DWORD WINAPI ThreadTime(LPVOID lpParam);

char strCurrentTime[80]; // change back to extern if needed

HWND hWndCurrentTime;
HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
{
  DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLS_DLG), hWnd,
              reinterpret_cast<DLGPROC>(DlgProc));

  hInst = hInstance;

  return 0;
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{

//  char strMeetingTime[8], strStartTime[8], strBreak1Start[8], strLunch[8],
//         strBreak2Start[8], strLeaveTime[8],  strLateBreak[8], strTime[10];

//  HWND hWndMeetingTime, hWndStartTime, hWndBreak1,
//                hWndLunch, hWndBreak2, hWndLeaveTime, hWndLateBreak;

  hWndCurrentTime = GetDlgItem(hWndDlg, IDC_CURRENT_TIME);
/*  hWndMeetingTime = GetDlgItem(hWndDlg, IDC_MEETING_TIME);
  hWndStartTime   = GetDlgItem(hWndDlg, IDC_START_TIME);
  hWndBreak1      = GetDlgItem(hWndDlg, IDC_BREAK1);
  hWndLunch       = GetDlgItem(hWndDlg, IDC_LUNCH);
  hWndBreak2      = GetDlgItem(hWndDlg, IDC_BREAK2);
  hWndLeaveTime   = GetDlgItem(hWndDlg, IDC_LEAVE_TIME);
  hWndLateBreak   = GetDlgItem(hWndDlg, IDC_LATE_BREAK_BTN);*/

  switch(Msg)
  {
    case WM_INITDIALOG:
      run();  // start thread for showing current time
/*      SetWindowText(hWndMeetingTime, "");
      SetWindowText(hWndStartTime, "");
      SetWindowText(hWndBreak1, "");
      SetWindowText(hWndLunch, "");
      SetWindowText(hWndBreak2, "");
      SetWindowText(hWndLeaveTime, "");*/
    return TRUE;

    case WM_COMMAND:
      switch(wParam)
      {
//        case IDC_LATE_BREAK_BTN:
          // Start a 15 minute timer

//        return TRUE;

        case IDCANCEL:
          end();
          EndDialog(hWndDlg, 0);
        return TRUE;
      }
    break;
  }

  return FALSE;
}

static void run()
{
  DWORD dummy;
  hThread = CreateThread(NULL, 0, ThreadTime, NULL, 0, &dummy);
}

static void end()
{
  keepRunning = false;
} …
nucleon 114 Posting Pro in Training

This should probably be 80, not 8. extern char strCurrentTime[8];

nucleon 114 Posting Pro in Training

Try making hWndCurrentTime global. Then change strCurrentTime (in ThreadTime) temporarily to "xyz" and see if it displays. If not, then you'll need to do what AD suggests.

nucleon 114 Posting Pro in Training

Yes, you need a loop in main. main should not (as far as I know) be called recursively. Since it's simple tail recursion, it's possible in this case that it would be optimized away. But it's still not a usual idiom and not necessary. In general, your main and overall program structure are a little messy. What about a structure like this (still using C++ in the procedural style):

const char* introduction = "howdy";
const int NCHILDREN = 25;
const int MAXMUTATIONS = 3;

int main()
{
    srand(time(0));
    string goal;
    cout << introduction << endl;
    while (getString(goal)) { // getString returns false on "q"uit
        string start = rndString(goal.size());
        cout << s << endl;
        cout << evolve(start, goal);
    }
    return 0;
}

int evolve(string str, string goal)
{
    string children[NCHILDREN];
    int nGeneration = 0;
    while (str != goal) {
        reproduce(str, children);
        str = select(children, goal);
        nGeneration++;
    }
    return nGeneration;
}

void reproduce(string str, string children[])
{
    for (int i = 0; i < NCHILDREN; i++) {
        children[i] = str;
        mutate(str);
    }
}

// mutate will even mutate correct characters (see * below)
void mutate(string str)
{
    int nMutations = rand() % MAXMUTATIONS + 1;
    for (int i = 0; i < nMutations; i++) {
        str[rand() % str.size()] = rndChar();
    }
}

That's basically the heart of your code, but much cleaner looking. (I haven't run it, yet, so no guarantees. Note that it doesn't bother to save all the "parents".)

And yes, a C++ programmer would probably define at …

nucleon 114 Posting Pro in Training

About variable names, Stroustrup makes a good point saying that as the scope gets larger the names should get bigger. If you plan on doing any Windows API, it's nice to distinguish your functions by starting them with a lower-case letters, since the API always (or at least almost always) uses mixed case starting with uppercase.

Anyway, you are coding in the procedural mode, making your program look more like C than C++. A couple of points on your C-like program. You have a constant 8 (sometimes appearing as 7) throughout your program. So either: #define SIZE 8 /* Hard-core C */ const int SIZE = 8; // C++ You could get rid of other constants similarly, and calling main recursively to go again is probably not the best idea. Put the quit option as number 3 in your main menu (you could exit(0) there and use an ininite loop in main for now at least).

So your decision now is whether to learn pure C first or whether to go straight to C++.

nucleon 114 Posting Pro in Training

First of all, this is an excellent effort and an interesting program. Here are some suggestions.

  • Give the user the option to go again.
  • Use C++ style includes: cstdio, cstdlib, etc.
  • Never let a line go over, say, 78 columns. (Definintely not over 80.)
  • Splitting string constants across lines can be done like this:
    cout << "\nUpper and lower case letters, spaces, "
                    "and common punctuation are allowed."
                    "\nInput target sentence:  ";

    As noted by death_oclock, this mechanism is why you didn't get the double-quotes you wanted around ancestor (and no syntax error either).

  • It's best to have an overall program description at the top. Since your intro message is a good summary, you could put something like this at the top of your program.
    // Sentence Evolver by Me & Dawkins
    
    const char* Description =
    "This program attempts to demonstrate the fact\n"
    "that natural selection is not random chance, but a chance-\n"
    ...
    "\"ancestor\" sentence of the length of the target string.\n"
    ...;

    Then get rid of the function "introduction" and replace the call to it by cout << Description;

  • Use \n for newline when there's already a double-quoted string right there. E.g:
    cout << "Ancestor:  " << initial << endl << endl
              << "Enter to continue." << endl;
    // could be:
        cout << "Ancestor:  " << initial << "\n\nEnter to continue.\n";
  • In general, I would rather see the comments before their target (rather than after or on the same line). I suppose that's just …
nucleon 114 Posting Pro in Training

You do not want to free temp there. Remember that you do not free pointer variables, but the memory that they point to. In this case, you do not want to free the memory that temp points to because you still have a pointer (*head) pointing to it and the memory is still being used.

free(xxx) is used when you actually want to delete the memory (often called the object) that the pointer points to.

nucleon 114 Posting Pro in Training

You need to clear the flags before the seek.

file.clear();
    file.seekg (0, ios::beg);
nucleon 114 Posting Pro in Training

The code looks reasonable, except that "and"ing with 0x0fff and 0xffff does nothing in this situation (assuming 16-bit shorts). Removing those will change nothing, however. I would check the "answer" you've been given before going any further. Your code may be just fine.

As for test_crc, there is no reason for those params.

nucleon 114 Posting Pro in Training

I see what you mean. Good point.

nucleon 114 Posting Pro in Training

It is incorrect to say that a is "converted" to a pointer. a simply is a pointer. That's what an array name is, a constant pointer to the first element.

nucleon 114 Posting Pro in Training

Inside the class, "a" is really just a pointer variable, which is usually the same size as an int, so that's why you get one.

Outside the class, "a" is a 3-element int array, so that's why you get 3.

Remember that C++ does not automatically know the size of an array that is passed to a function. You would normally pass the size as well.

nucleon 114 Posting Pro in Training

Also note that you have to use radians (not degrees) for your angles, so pass = 2 * M_PI / vertex (M_PI is in math.h) .

Alex_ commented: Thank you for telling me about radians! +1
nucleon 114 Posting Pro in Training

Thr program still has a bug, though. You are not zero-terminating the c-string "rev". The best way is perhaps:

int i, j;
        for(i = 0, j = strlen(word) - 1; j >= 0; i++, j--)
            rev[i] = word[j];
        rev[i] = 0; // zero-terminate rev

Note that I changed i <= j, j >= 0 to j >= 0 since that's all it's evaluating to anyway. (I can't think of any reason to use the comma operator in the test of a for loop.)

nucleon 114 Posting Pro in Training

I believe you can put this in main.h:

extern CAMARA camera;
extern OBJECT *obj;  // should probably be a longer name for a global

and then in main.c you just put

CAMERA camera;
OBJECT *obj;
nucleon 114 Posting Pro in Training

Still havent been able to solve it. Is it even possible to do this?

There must be a way to do it.
What exactly are you trying to do.
What exactly is it doing wrong.

nucleon 114 Posting Pro in Training

Try SRCCOPY in your first BitBlt. Look into other modes if that doesn't do what you want. Also check out TransparentBlt which allows you to have a transparent rgb color.

nucleon 114 Posting Pro in Training

Also, your lattice values can be 16 or greater, so you get out of range errors accessing table with lattice. Try this to see the problem. (Replaces lines 90 to 98 in your previous listing.)

for (i = 0; i < row; i++)
    for (j = 0; j < col; j++)
        if (lattice[i][j] >= 16)
            printf ("%d\n", lattice[i][j]);
        else
            table[lattice[i][j]][1]++;
nucleon 114 Posting Pro in Training

I assume you expect this idiom to give a number between 0 and row - 1: rand() / (RAND_MAX / row) But in fact it occasionally gives a value of row. This is because RAND_MAX is not evenly divisible by row (i.e., there's a little left over). Here's some empirical proof:

int n = 0, i;
    for (i = 0; i < 100000; i++)
        if (rand() / (RAND_MAX / 5) >= 5) n++;
    printf ("%d\n", n);

Instead change it in all similar places to this which can never be out of range: rand() % row

nucleon 114 Posting Pro in Training

There should be an option to not create a console window for a "console" program. There is in Dev-C++ (with gcc).

nucleon 114 Posting Pro in Training

csurfer makes an important point about testing only up to the square root of num. It will actually reduce the number of comparisons to much less than half. For example, with the number 101 you only need to test up to the number 10 -- quite a savings!

To reduce that number of comparisons by half again, you can test 2 separately and then only test odd numbers (stepping by 2 in the for loop), something like this:

sqr = (int) sqrt (num);
if (num % 2 == 0)
    isPrime = 0;
else
    for (i = 3; i < sqr; i += 2)
    ...
nucleon 114 Posting Pro in Training

Sorry csurfer, I wasn't thinking.

nucleon 114 Posting Pro in Training

csurfer: Do you always just repeat what other people have already said?

nucleon 114 Posting Pro in Training

You can replace your entire switch statement with table[lattice[i][j]][1]++ .

By the way, you are using ++ and -- incorrectly. It is NOT a = a++ , but simply a++ .

As for the segfault, it may be because of the way you are using rand(). Try changing everywhere it says rand() / (RAND_MAX / max) to rand() % max .