nucleon 114 Posting Pro in Training

If you simply want the numbers 1 to 9 uniquely randomized in a row, you could load the row with the numbers 1 to 9 and then shuffle it. No need to pick random numbers and check if they've already been picked. That's always something to avoid, if possible.
However, shuffled rows would still have column repetitions. Getting rid of those is the interesting part.
And to time the routine, you might consider the clock() function, as it has better resolution than time().

nucleon 114 Posting Pro in Training

I stand by my original post, every word of it. You're on your own. Good luck. (You'll need it!)

nucleon 114 Posting Pro in Training

Your tobase10 function does not work properly, and it is overly complicated. You do not need log10 or pow. You can loop until result becomes 0 (instead of using log10). A special case here is when result starts out as zero. Instead of pow you can start a number at 1 and multiply it by the base every time through the loop.

And you really don't want to convert to base 10 anyway, so the last part of the function should go (and the name should be changed to, say, getValue). You really just want the numerical value, not a base 10 representation.

Also there's no reason to use "displace". You can deal with the digits one at a time in a loop.

nucleon 114 Posting Pro in Training

> the 15 on line 12 doesn't count either.
There are two 15's on line 12.

You should probably post this in the Perl section.
I'd try (in Perl): /\..*\s(15|53|92)\s.*=>/

nucleon 114 Posting Pro in Training

sizeof p gives the size of an int**.
sizeof *p gives the size of an int*.
sizeof **p gives the size of an int.
sizeof *p[n] also gives the size of an int.

nucleon 114 Posting Pro in Training

You need to instantiate the OutputObject template: for_each (V.begin(), V.end(), OutputObject<T>);

nucleon 114 Posting Pro in Training

Remember what NicAx64 said, that the plugboard etc belong as data members within the Enigma class. So something like this:

class Enigma {

    PlugBoard plugBoard;
    Reflector reflector;
    Rotor rotor[3];

public:

    Enigma();
    // ...

    string encrypt(string plainText);
    string encrypt(char* plainText);

    string decrypt(string cipherText);
    string decrypt(char* cipherText);
};

int main() {

    Enigma machine;

    string msg("Hello world");
    cout << msg;

    string cipher = machine.encrypt(msg);
    cout << cipher;

    string plain = machine.decrypt(cipher);
    cout << plain;
}
nucleon 114 Posting Pro in Training

I've been on a wild goose chase! Sorry about that.

This just looks like pointer arithmetic. When you add a number to a pointer it increments the pointer by that number times the size of the thing pointed to. So you probably just want to add 1 to lastseen.

nucleon 114 Posting Pro in Training

What does sizemal hold?
Is 18h the size of a struct memory ?

nucleon 114 Posting Pro in Training

See this for code tags.
If you feel you must use sbrk, then this line looks fishy to me: newnode=(struct memory *)sbrk(sizemal+sizeof(struct memory)); Should that possibly be: newnode=(struct memory *)sbrk(sizeof(struct memory)); It depends on what sizemal is. If it's the current size of the data segment, then you certainly don't want to pass that into sbrk since sbrk increments the size of the data segment by its parameter (whereas brk sets it to its parameter).

nucleon 114 Posting Pro in Training

USE CODE TAGS!!!

Having said that, is it possible you mean to use brk and not sbrk?

nucleon 114 Posting Pro in Training

The design depends on how it will be used. Write a driver (a main) that uses the object's interface AS IF it was already written. Then create a class for an object that fits that interface. It also depends on how much detail you want in the simulation. If you wished you could model it right down to the wires.

nucleon 114 Posting Pro in Training

(Assuming you're not allowed to use std::list...)
It can be tricky swapping elements in a singly-liked list. Make diagrams so you can see what's needed. You'll probably need extra pointers to remember the previous elements of the two pointers with which you're traversing the list. Also, after you swap two elements, you will have to swap the contents of variables temp1 and temp2 to keep your loop on track.

JAGgededgeOB172 commented: Thank you! +1
nucleon 114 Posting Pro in Training

You need to declare showBoard like this: void showBoard(char[][3], int, int); Note that this hardcodes the size of the second dimension, but that will be okay if the board is always 3 by 3 (or indeed anything by 3).

nucleon 114 Posting Pro in Training

AD: Our analyses seem to gell nicely. It explains why all the extra stuff is at the end. Icebone is basically reading and writing 8 times too many bytes. It could have caused a segfault but didn't in this case.

Simply adding / 8 after width * height in the two loops involved (the reading and writing loops) writes the file perfectly.

But a better solution (assuming padding to the nearest byte) might be:

int size = width / 8;
if (width % 8 != 0) ++size;
size *= height;

And use size in the loop conditions.

nucleon 114 Posting Pro in Training

The reason your file is 8 time bigger is because you are forgetting that each bit (in this format) represents a pixel. You are reading (and writing) 1152 * 813 bytes, but that is actually how many bits you should be reading! There will probably be some padding at the end of the picture rows, so there will be more than 1152 * 813 / 8 bytes. You will have to look up the info for the padding. You could try assuming that it's simply to the nearest byte and see if that works, but it could be rounded off to 4 bytes. At any rate, that's your basic problem.

nucleon 114 Posting Pro in Training

You mean 3D array. In that case:

GLubyte ***bufImg;
    bufImg = new GLubyte** [height];
    for (int i = 0; i < height; ++i) {
        bufImg[i] = new GLubyte* [width];
        for (int j = 0; j < 3; ++j)
            bufImg[i][j] = new GLubyte[3];
    }
nucleon 114 Posting Pro in Training

(int)log10(n) + 1 will give you the number of digits.
You could strip digits off with a loop body like this:

digit = n % 10;
// store digit or whatever
n /= 10;
nucleon 114 Posting Pro in Training

You must use include guards. Do this:

#ifndef FILENAME_H_
#define FILENAME_H_
// header code here
#endif
nucleon 114 Posting Pro in Training

Yes, it should definitely be .h if it has no actual code.
Do you have include guards in your header files?

nucleon 114 Posting Pro in Training

I changed the structure. It's not a 3D array anymore, but a 2D array of RGB structures. So you access it like this: r = bufImage[im_x][im_y].r;

nucleon 114 Posting Pro in Training

Is this really what you want? #include "enum.cpp"

nucleon 114 Posting Pro in Training

Try this:

struct RGB {
    GLubyte r, g, b;
};

RGB **bufImg;

bufImg = new RGB*[height];
for (int i = 0; i < width; ++i)
    bufImg[i] = new RGB[width];
nucleon 114 Posting Pro in Training

Re-read my last post carefully.
In C it will NOT work.
In C++ it WILL work.
Here's a minimal program.
Compile it in C, then in C++ to see the difference. int main() { int i; &(i = 1); }

nucleon 114 Posting Pro in Training

In C, the assignment operator returns an rvalue.
In C++ it returns an lvalue, so your first example should work.
(Look up lvalue, rvalue if you don't know what they are.)

nucleon 114 Posting Pro in Training

You need to set the rankList pointers to NULL in Ranker's constructor.

You need to remove the ampersands in front of name in Competitor::print.

You should have destructors for Competitor and Ranker since they both dynamically allocate some storage.

And you don't need the (double) in this (and similar) lines: starters[0]->setTime((double)12.0); That's all I can see at a glance. Fix those and let me know if it works.

nucleon 114 Posting Pro in Training

You haven't said what's actually happening.
What is it doing wrong?
Is there any error messages?

nucleon 114 Posting Pro in Training

It would seem impossible for your original code (in post 1) to write more than width * height + 25 bytes (give or take a few bytes for different widths since you are writing these as text), so if you are getting 8 times this many bytes your problem must lie elsewhere. You need to attach your (minimal) program and data file.

nucleon 114 Posting Pro in Training

It's a matter of where you want to store the object, and whether you wish it to remain in existence after the function in which it was created has returned.

What you are calling "static" allocation (more properly "automatic") saves the data on the stack. Dynamic allocation stores the data in the free store (a.k.a., the heap).

When data is stored on the stack, it disappears when the function that allocated it returns. By storing the data in the free store, you can return it from a function by returning its address, which will still be valid after the function's stack space is freed.

nucleon 114 Posting Pro in Training

If you need any doubles, they would be cents and total. You only need one loop, with an integer index counting the days. Take another stab at it and think it through step by step.

nucleon 114 Posting Pro in Training

In class TextField, lines needs to be a Field** And in TextField::TextField, line 5 should be lines = new Field*[num];

nucleon 114 Posting Pro in Training

Isn't a pbm a text file?
Shouldn't you be writing your numbers as text, with: writer << img_pix[i].rgbb[0];

nucleon 114 Posting Pro in Training

You need to declare the operationType in menu as a reference.
86 the asterisks.
Remove one tab from the beginning of every line.

nucleon 114 Posting Pro in Training

You've forgotten the [ ] around the array size in the initializer of p. Thus you are declaring p to point to a single double of value n. As you attempt to read memory past this first value you eventually get a segfault.

However, if you fix that problem, another will arise. You are asking to allocate 110,075,314,176 elements, each 8 bytes long, for a total of 880,602,513,408 or about 820GB. So unless you've got a terabyte of RAM you shouldn't run this program.

For that reason, a huge array is usually implemented as a "sparse array" (look that up).

nucleon 114 Posting Pro in Training

He's using a special header.

You do not want the semicolon at the end of the parenthesized portion of the if statement.

Other than that, it seems weird to be assigning y and z a constant value and then testing whether or not either are zero, which is what your code is doing.

nucleon 114 Posting Pro in Training

Sleep a day at a time. Check the date/time every time you wake up.

nucleon 114 Posting Pro in Training

MyStudent.Name is the name of a storage location capable of holding up to 20 characters (including the terminating null character). You cannot simply assign a string constant to MyStudent.Name since that just tries to copy the address of the string constant to MyStudent.Name. What you need to do is copy the characters from the string constant into the space provided in MyStudent.Name. strcpy (MyStudent.Name, "Test Value"); (or strncpy for safety when using user input)

nucleon 114 Posting Pro in Training

Don't bother. Mine was "conceptually simple." Your bit-shifting solution is best. It's even a perfect candidate for coding in assembly if performance was an issue.

nucleon 114 Posting Pro in Training

You should profile them. Post your results.

nucleon 114 Posting Pro in Training

How tedious it is depends on how many different types you need to reverse. If just ints:

typedef unsigned char uchar;

union IntBytes {
    int i;
    uchar b[sizeof(int)];
};

WriteInt( int n, FILE* fout ) {
    union IntBytes uBytes;
    int i;
    uBytes.i = n;
    for (i = sizeof(int) - 1; i >= 0; --i)
        fputc( uBytes.b[i], fout );
}
nucleon 114 Posting Pro in Training

You haven't initialized the local variables you use in main.

nucleon 114 Posting Pro in Training

Try something like this:

#define MAXFILESIZE 120
...
char strFileString[MAXFILESIZE];
...
ReadFile(fh, strFileString, MAXFILESIZE, &dwRead, NULL);
CloseHandle(fh);

if (dwRead >= MAXFILESIZE) --dwRead; // so we don't write past the end
strFileString[dwRead] = 0; // ensure null termination of C-string

strncpy( strStartTime, strFileString, 11 );
strncpy( strWhateverTime, strFileString + 13, 11 );
// The + 13 skips the first 13 chars, the 11 reads 11 chars.
...
nucleon 114 Posting Pro in Training

You're mixing C-strings and the C++ string class. Here you just want plaing old C-strings, which do not have member functions associated with them, so you can't do the substr thing. Just do it in a normal old C way.

nucleon 114 Posting Pro in Training

InitCommonControlsEx (see the table at the bottom)
Try putting it in your WM_CREATE handler.

nucleon 114 Posting Pro in Training

You haven't posted the manifest file.
Did you InitCommonControlsEx() ?

nucleon 114 Posting Pro in Training

>That solution is basically like my original solution, and leaves me with input that never gets saved in the buffer that i made available for it.

Actually it isn't like your original solution. Differences:
1. yours consumes any characters, not just whitespace
2. yours consumes a line even when you don't want it to

Mine only consumes initial whitespace (incl. newline) leaving any non-whitespace to be picked up by the fgets. That doesn't mean it does what you want though!

Probably the best solution is to read all input with fgets and deal with the string so read (perhaps with sscanf). You could wrap fgets in a function that does two things:
1. Ensures the string is not only whitespace (if that's not allowed)
2. Removes the '\n' (if any) from the end of the buffer.

nucleon 114 Posting Pro in Training

Here's a function that uses a set to remove duplicates from a vector without changing the order of elements:

void remove_dups (vector<string>& v) {
    set<string> s;
    pair<set<string>::iterator, bool> result;

    for (vector<string>::iterator i = v.begin(); i < v.end(); ++i) {

        result = s.insert(*i);

        if (!result.second) {
            v.erase(i);
            --i;  // adjust iterator
        }
    }
}

I'm not sure if the --i can blow up (although note that the first element will never be erased because it definitely will not be in the set).

robotnixon commented: thanks for the help +2
nucleon 114 Posting Pro in Training
nucleon 114 Posting Pro in Training

I'm not sure if this is what the errors indicate, but have you properly wrapped your C declarations like so:

#ifdef __cplusplus
extern "C"
{
#endif

int cfunc1( int n );
int cfunc2( int n );

#ifdef __cplusplus
}
#endif
nucleon 114 Posting Pro in Training

Could you do something like this:

scanf( " " ); // Consume whitespace, if any (incl. newline)
  fgets( ... );