deceptikon 1,790 Code Sniper Team Colleague Featured Poster

near and far were keywords used to modify pointers back when the segmented memory architecture was both used and transparent. Near pointers would access the current segment while far pointers could reach across segments. Compilers that supported such keywords probably still support them for backward compatibility, but on modern systems they're pretty useless as everyone uses or at least simulates a flat memory model now.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

when the user input numbers, it should be arranged just like an actual tree

If the tree is required to be displayed top-down then you're looking at some form of level order traversal to get the nodes in the proper order for display and a relatively non-trivial algorithm for determining the grid positioning of each node for printing. If you can get away with it, rotating the tree by 90 degrees is leaps and bounds simpler, and looks just as good:

#include <cstdlib>
#include <iostream>

using namespace std;

struct node
{
    int data;
    node* left;
    node* right;

    node(int data): data(data), left(0), right(0) {}
};

node* insert(node* root, int data)
{
    if (root == 0)
    {
        return new node(data);
    }
    else if (data < root->data)
    {
        root->left = insert(root->left, data);
    }
    else
    {
        root->right = insert(root->right, data);
    }

    return root;
}

void prettyprint_node(node* root, int level)
{
    for (int i = 0; i < level; i++)
    {
        cout << '\t';
    }

    if (root == 0)
    {
        cout << "--\n";
    }
    else
    {
        cout << root->data << '\n';
    }
}

void prettyprint_90(node* root, int level)
{
    if (root == 0)
    {
        prettyprint_node(root, level);
    }
    else
    {
        prettyprint_90(root->right, level + 1);
        prettyprint_node(root, level);
        prettyprint_90(root->left, level + 1);
    }
}

int main()
{
    node *root = 0;

    for (int i = 0; i < 10; i++)
    {
        root = insert(root, rand() % 100);
    }

    prettyprint_90(root, 0);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Probably a stupid question, but do you have Firefox set up to remember form data in your privacy options?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's not standard Markdown, it's one of the forks with extensions: https://github.com/egil/php-markdown-extra-extended#readme. In particular, the ~~~ syntax is for fenced code blocks.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

BBCode no longer works for new posts, it's all Markdown based now. As far as making a code block, it's a matter of indentation (at least 4 spaces). Presently the code button only pastes an example into the editor if there's no text selected. To paste actual code, you would just paste it in, then select the entire block and hit tab or click the code button.

Another option is wrapping your code in ~~~, which will force a code block without the need for indentation.

I recognize that selecting the code to turn it into a code block is awkward, we're working on making the editor more convenient. ;)

I tried using the Code button but that didn't seem to work either (i.e. past code into editor, highlight, then hit the Code button).

I just tested and it appears to work properly. Can you reproduce the problem consistently with all options described above?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

hello !
well today i got two down votes , i dont think that there is any thing wrong with my post , as we know who add reputation to us , can we know the person who is down voting us , and is there any way to balance them , well this thing make me feel very bad , i always tried to help others and to share my knowledge and to learn something new from you experienced people.but this thing is very bad , :( , i spend lots of time when someone post a question to answer him , am i doing all this to get down votes :( ,

Regards

I'm sorry you feel bad about getting downvoted, and I'm also sorry for the harsh tone of this reply, but I can't think of a softer way to say what I want to say at the moment.

No matter what you say, someone will always disagree with you. If you think you're being harassed, we can look into who might be doing it. As far as wanting to know who an anonymous voter is, the only other reason I can think of is you want to reciprocate, and nothing good can come from that.

as we can not give reputation with out giving any reason , they can make it possible with down vote also .

We may do that in the future, but for now the intended design of the voting …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Visual Studio doesn't support variadic templates yet.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

More like it's filtering out the better candidates because they won't consider an employer that cares more about trivia than real skills.

zeroliken commented: Agree :) +9
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but also the questions asked in the HR round of interview

What little respect I had for the business world has just died.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Modern programming education: where you learn totally useless stuff at the expense of useful stuff.

can i know how to find greatest of two numbers?
the condition is we should not use any comparision operators..

You can easily find the answer to this on google. I can't in good conscience enable stupid teachers by assisting with stupid homework. Sorry.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Because Event is a reserved keyword in C#, I don't think your code will work as you intended.

C# is case sensitive. event is a reserved keyword, but Event is not. It may be a standard class, but I don't think so.

@ Deceptikon - What do you mean by null?

I mean this:

private Event myEventParticipating;

Is equivalent to this:

private Event myEventParticipating = null;

If you never say athelete1.EventPart = new Event() or something similar, then athelete.EventPart is equal to null and you cannot access the members of a null object.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

myEventParticipating is null by default and you never set it in the snippets.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Replace reverseString() in your original code with this:

int reverseString(string a[])
{
    for (int i = 0; i < 1; i++)
    {
        std::string rstr = a[i];
        std::reverse(rstr.begin(), rstr.end());
        std::cout << rstr << std::endl;
    }
}

I'd offer other suggestions, but I don't want to overwhelm you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Now reverseString() doesn't do any reversing. All it does is make a copy of strings in the array and print them as-is...

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need to include the algorithm header for std::reverse(). You're also trying to pass an array of strings to reverseString() when it only accepts a single string object.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The overloaded<< operator only allows non-const references, which excludes temporaries like the ones returned by transpose() or calculated with the overloaded operator*. You should change it to expect a reference to const instead as you don't make any changes to the object's state:

ostream &operator<< (ostream &out, Matrix const &myMatrix)
{
	for(int i=0; i < myMatrix.num_rows(); i++){
		for (int j=0; j< myMatrix.num_cols(); j++){
			out.width(3);
			out << myMatrix.get_element(i,j) << "  ";
		}
		out << endl;
	}
	out << endl;
	return (out);
}

That also requires the called methods of num_rows(), num_cols(), and get_elements() to be declared as const methods, but that's how they should be defined anyway to maintain const correctness:

int Matrix::num_rows() const
{
	return numRows;
}

int Matrix::num_cols() const
{
	return numColumns;
}

ElementType Matrix::get_element(int row, int col) const
{
	return myArray[row][col];
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

why instead of this why cant we use array???

And if you don't know the size of the array at compile time?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Wow, there are some unsympathetic people here. :icon_rolleyes: If it's legitimate harassment then that's an issue, but to the best of my knowledge there's no way to find out who is doing the voting without direct database queries, if that information is presently stored at all.

You'd need to contact Dani for such a task as she's the only one with database access in the current system.

happygeek commented: Agreed +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why do you need a 2D array if you already have a class holding the data?

personClass[] people = new personClass[N];

for (int i = 0; i < N; i++)
{
    people[i] = new personClass();

    people[i].Name = "John Doe";
    people[i].Address = "123 Nowhere Lane";
    people[i].Phone = "(555) 555-5555";
    people[i].PostCode = "12345";
    people[i].Height = "182cm";
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The draft standard will tell you what's legit as far as C++ goes. The competition might have another definition of "standard" though.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

However,it seems that i cant use 'if else' to call them

Could you elaborate on this? The only real problem with how you're calling the functions is that they don't exist; you've only declared the function signatures, not actually defined the function bodies.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The only problem I see is processing the # wildcard. Other than that it's a simple matter of grabbing a word from both the pattern and the source string, then either checking them for equality or not depending on if the pattern word is a * wildcard.

Processing a # wildcard is kind of tricky if you also want to check literals beyond it. So if "#.dsf" should match "eur.stock.dsf" but not "stock.nasdaq", the algorithm needs to consider how many pattern words are remaining and not match too much when processing a # wildcard.

So maybe something like this pseudocode:

WHILE more pwords AND more swords DO
    IF pword = "#" THEN
        -- Get the next non-# pattern word
        NEXT pword WHILE pword <> "#"

        IF NOT more pwords THEN
            -- # is the last word in the pattern, so it matches everything
            RETURN true
        ELSE
            remaining := COUNT pwords

            -- Match everything up to the first remaining pattern word
            WHILE remaining > 0 AND more swords DO
                DECREMENT remaining
            LOOP

            IF remaining = 0 THEN
                -- Insufficient words in the source to match remaining pattern words
                RETURN false
            ENDIF
        ENDIF
    ELSE
        IF pword <> "*" AND sword <> pword THEN
            -- The source word doesn't match a literal pattern word
            RETURN false
        ENDIF
    ENDIF
LOOP

RETURN NOT more pwords OR more swords
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in the same directory as the .cpp file so I'm lost right now.

That's probably your problem. Typically relative file names will be looked for in the current working directory, not the source file directory. Sometimes those will be the same, but more often than not they won't be.

The current working directory is probably going to be the same directory as where the executable is. You might be able to see it with a test program like this:

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc >= 1)
    {
        cout << "Current working directory: " << argv[0] << endl;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but when i build i still get the error message

What's "the error message"? If you mean when you *run* the program you get the error opening file message, then I'd suggest using perror() to get more details:

if (infile.fail()) {
    perror("Error opening file!!! Now closing program!!!");
    exit(1);
}

Don't forget to include <cstdio> for perror()'s declaration.

Oh and I know exit(1); is an un-natural way of closing the program because it literally crashes it

Not really. exit(1) is identical to return 1 from main(). You're probably thinking of abort().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Endianness only matters when you make an assumption about how bytes are organized. Bitwise operators will work with the value and have consistent behavior regardless of the endianness of the system, and that's one way to ensure portable code when sending or receiving binary data:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <limits>
#include <type_traits>
#include <vector>

using namespace std;

template <typename T>
vector<unsigned char> GetBytesNonPortable(T value, typename enable_if<is_integral<T>::value>::type * = nullptr)
{
    // Extract bytes as stored in memory (different endianness will give different results)
    unsigned char* pun = (unsigned char*)&value;

    return vector<unsigned char>(pun, pun + sizeof(T));

}

template <typename T>
vector<unsigned char> GetBytesPortable(T value, typename enable_if<is_integral<T>::value>::type * = nullptr)
{
    unsigned const bits = numeric_limits<unsigned char>::digits;
    unsigned const max = numeric_limits<unsigned char>::max();

    vector<unsigned char> result;

    // Extract bytes regardless of endianness
    for (size_t i = 0; i < sizeof(T); i++)
    {
        result.push_back((value >> (bits * i)) & max);
    }

    return result;
}

int main()
{
    int value = 12345678;
    vector<unsigned char> v1 = GetBytesNonPortable(value);
    vector<unsigned char> v2 = GetBytesPortable(value);

    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " ")), cout << endl;
    copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " ")), cout << endl;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Some folks really believe in DataSets, DataAdapters and DataTables.

The implication being that you think they're not a good solution? If so, please explain why. I always enjoy hearing different perspectives. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

By auto parse do you mean turn a URL into a clickable link?

Starting off in the new system there will be only one editor, and we're moving from BBCode to Markdown for formatting purposes. I doubt that's going to change this close to release, so I feel comfortable stating it publicly. ;)

As far as things are in dev right now, there's no auto parsing of URLs in the final post. You would need to use a proper Markdown format to make the link clickable:

This will be parsed: http://www.daniweb.com
This will not be parsed: [noparse]http://www.daniweb.com [/noparse]

In the current system, you can just wrap your URLS in noparse tags: [noparse]http://www.daniweb.com [/noparse]. The only down side to that is there's no convenient button for noparse tags, they must be typed manually. But it's much better than the rigmarole you've been going through with switching vBulletin editors.

diafol commented: noparse - no brainer! +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does that mean the values for B, C and D are randomized too?

Well...yes. When you call rand() it gives you the next pseudorandom number in the sequence. The sequence can be reset by calling srand().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You're not doing anything wrong. Windows Forms is notorious for having a noticeably slow redraw out of the box. There are a number of ways to improve matters, but the list is kind of long, so I'll just suggest searching google.

One thing to keep in mind is that C# has to go through the .NET framework while VB6 would hit the Win32 API directly. There's a whole extra layer or two difference, though that's not an excuse for the unresponsiveness of WinForms. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That depends on the algorithm rand() uses. It's probably some form of linear congruential random number generator, but there's so many of those it's impossible to tell what sequence will result without seeing the code or doing a lot of analysis on produced sequences.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If the seed is always the same, the pseudorandom sequence will always be the same. Change the seed to something other than 8, and A will probably be different.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I didn't understand that question. Could you elaborate and possibly provide a simple example outside of the encryption algorithm of what you're confused about?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your printword() function is making the results look different from the paper. Try this instead:

void printword(WORD A) {
    printf("%08X ", A);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When you say you "can't use arrays", what exactly does that mean? Because it's quite impossible to sort anything when you can't retain it, and while you can use 25 variables to get the same effect as an array, that's totally insane. I can't imagine a teacher expecting you to do that unless they subscribe to the school of hard knocks and want to teach you the importance of arrays.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not the moderators (at least most of us are savvy enough to understand that)

Agreed, but I still think I'll leave the final call to Dani. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The code continually allocates new memory to ptr, which leaks away the previous memory and loses any previously assigned numbers. Rather than outline all of the possible cases and explain why they're necessary, I'll just give you a function to study:

bool ResizeArray(int*& a, unsigned old_size = 0, unsigned new_size = 0)
{
    bool rc = false;
    
    if (new_size != 0 && old_size != new_size)
    {
        if (!a)
        {
            // Allocate a new array of non-zero size
            //
            try {
                a = new int[new_size];
                rc = true;
            }
            catch (...)
            {
                // No cleanup, just converting an exception into a return code
            }
        }
        else
        {
            // The new size is different on an existing array; try to resize
            //
            unsigned n = new_size > old_size ? old_size : new_size;
            int* temp = nullptr;
            
            try
            {
                temp = new int[new_size]; // Might throw
                
                for (int i = 0; i < n; i++)
                {
                    temp[i] = a[i]; // Might throw
                }
                
                delete[] a;
                a = temp;
                rc = true;
            }
            catch (...)
            {
                if (temp)
                {
                    delete[] temp;
                }
            }
        }
    }
    
    return rc;
}

Don't copy it mindlessly, try to understand what's being done, in what order, and why. There are three steps:

  1. Allocate a temporary array.
  2. Copy existing elements into the temporary array.
  3. Replace the original array with the temporary array.

But the logic varies depending on things like if the original array hasn't yet been allocated any memory and is a null pointer, and the relationship between the old size and the new size.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need to run the query before trying to parse the results. :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Where do you give $banners_result a value?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Beej's guide has been the recommended first stop for many years. I'd recommend purchasing the book for offline reading too, 'tis worth it even though the book is rather thin. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Cant we have a sneak peek?

The site hasn't been skinned yet, so it doesn't look like much. We don't really want to show off the raw site as folks have a tendency to focus on how it looks and ignore how it works.

It'll be ready for show soon, though. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's certainly a suggestion worth looking into.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

the title says it all

If by "says it all" you mean that it says one can ignore your thread due to lack of information, I'd agree. :D Since I know jack squat about CDF, I'll just try to answer what I think is your question.

What I know is that I need to find numbers greater than zero, then number greater than one and so... but when I looked at it, I figured out that I cannot have like 50 if condition in the program to check that.

You can use a counted loop where the counter is the exclusive lower bound of numbers you're looking for:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
    // Get a list of random numbers with possible negatives
    //
    auto rgen = bind(
        uniform_int_distribution<int>(-99, 99),
        mt19937((unsigned)time(nullptr)));
    vector<int> v;
    
    generate_n(back_inserter(v), 10, rgen);
    
    // Find the numbers greater than any given value in the range
    //
    int ubound = *max_element(v.begin(), v.end());
    vector<vector<int>> distribution(ubound);
    
    for (int i = 0; i != ubound; i++)
    {
        for_each(v.begin(), v.end(), [&](int x)
            {
                if (x > i)
                {
                    distribution[i].push_back(x);
                }
            });
    }
    
    // Display the raw results
    //
    for_each(distribution.begin(), distribution.end(), [](vector<int> x)
        {
            if (!x.empty())
            {
                copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));
                cout << endl;
            }
        });
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is a=b+c+d considered as one instruction or 2 instructions?

Neither. Barring compiler optimizations, there are three operations going: two additions and an assignment. As far as instructions go, a naive interpretation would be:

mov r1, b
mov r2, c
add
mov r2, d ; Assuming add puts the result in r1
add
mov a, r1

Similarly is a+=b one instruction or 2?

It depends on how the compiler generates machine code for that kind of compound operator. But when working out time complexity, you should be looking at operations in the source language, not the resulting instructions after the source language is compiled. In that case your two example statements are 3 operations and 1 operation, respectively.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In the general case you're getting into multithreading territory. Otherwise, you can just check the time every now and again to see if 60 seconds have passed. Even in my sample program, such a check wouldn't be more than half of a second off because of the Sleep() call.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

p is a pointer to char, not int. So when you increment by 1, it increments by 1 byte instead of the number of bytes in an int. I think you should throw that book away if that program is a sample of its quality.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This game is an amusing diversion. Like Ali 2101 said, kbhit() is the easiest way to check for a key press in compilers that support it. You can also fake kbhit() with the Win32 API, but that's not necessary in this case.

Here's a sample game that does something similar:

#include <iostream>
#include <random>
#include <string>
#include <cctype>
#include <ctime>
#include <conio.h>
#include <windows.h>

using namespace std;

namespace 
{
    void SetPos(int x,int y)
    {
        COORD coord = {(SHORT)x, (SHORT)y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    void ShowAt(string const& s, int x, int y)
    {
        SetPos(x, y);
        cout << s << flush;
    }
    
    class GameChar
    {
    public:
        GameChar(char value, int x, int y): _value(value), _pos(x, y) {}
        
        int Value() const { return _value; }
        void Clear() const { cout << "\b "; }
        int Next() const {return _pos.Y + 1; }
        void Advance() { _pos.Y = Next(); }
        
        void Show() const
        {
            Clear();
            SetPos(_pos.X, _pos.Y);
            cout.put(_value);
        }
    private:
        struct ScreenCoord
        {
            ScreenCoord(int x, int y): X(x), Y(y) {}
            int X, Y;
        };
    
        char _value;
        ScreenCoord _pos;
    };
}

int main()
{
    string const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int const SPEED = 500;
    int const MAX_X = 40;
    int const MAX_Y = 10;
    
    mt19937 rgen((unsigned)time(nullptr));
    int answers[2] = {0};
    bool done = false;
    
    ShowAt("Match the letters before they hit the bottom:", 0, 0);
    ShowAt(string(MAX_X, '-'), 0, MAX_Y);
    
    while (!done)
    {
        GameChar ch(alphabet[rgen() % alphabet.size()], rgen() % MAX_X, 1);
        
        for (; ch.Show(), ch.Next() <= MAX_Y; ch.Advance(), Sleep(SPEED))
        {
            if (kbhit())
            {
                ++answers[toupper(getch()) == toupper(ch.Value())];
                break; …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

One example: You may want the lifetime of the object to not be restricted to the scope in which it was declared. Dynamic memory has a lifetime of when you allocate it to when you release it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Be aware that 2 digit dates *will* cause overlap between the 1900s and the 2000s, but if you have a hard lower bound on the 1900s, like 1970, you can check for that explicitly and be good until the same year in the 2000s:

// Works until 2070
if (year < 70)
    year += 2000;
else
    year += 1900;

There's not a good solution other than using 4 digit dates everywhere.