deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Ignoring variations of the following, you can initialize it manually member-by-member, copy it from an existing object, use an initializer list, or define a constructor.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Using the initializer list you can take advantage of initialization rules:

#include <iostream>

using namespace std;

struct a {
    double id;
    int tag;
    char array[10];
};

int main()
{
    a var1 = {100, 1, "The City"};
    a var2 = var1;

    cout << "var2.id = " << var2.id << endl;
    cout << "var2.tag = " << var2.tag << endl;
    cout << "var2.array = " << var2.array << endl;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You should use <iostream> and <cmath> (all C++ standard headers don't have the .h, and all those taken from C (like math.h, stdlib.h, stdio.h, etc.) also omit the .h and are prefixed with the letter c).

It's also important to remember that some compilers (Microsoft's in particular) "helpfully" don't require std qualification on a large portion of the C library. But if you rely on that your code will not be portable and will fail to compile on other compilers without a using directive. But a using directive isn't generally considered to be a good practice with new code because it defeats the purpose of namespaces.

The return value should be 0 if the program executed correctly, and something else otherwise.

The final nail in the coffin for void main() was standard C++'s feature that allows you to omit the return statement and a return value of 0 will be assumed. This only works for the main() function, but it means you can do this:

int main()
{
}

And the code is perfectly legal and portable across the board. It even saves you a keystroke over void main(), so there are no excuses anymore.

You should declare your variables as close as possible to where you first use them

That's debatable. I understand the intention, but if a function is large enough that declaring variables at the top of each scope can be described as error prone, you should probably do …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But can you tell me why in the definition of f1 (in your code) p's address has not been passed and still it's passed by reference?

The entire point of using another level of indirection is so that you can access the original object. If you already have that level of indirection in place, there's no need for another. In other words, if the object you have is already a pointer to the object you want, you don't need to pass any addresses.

On a side note, what you're doing is not pass by reference, it's passing a pointer to simulate the effect of pass by reference.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, it is possible but it is not recommended.

Why?

It is better to have dynamically allocated array in this case.

Again, why? Please assume that the compiler supports C99, since the obvious "it's not portable" prior to C99 is obvious.

int *array = malloc(i*sizeof(int)); // alloc enough memory for the array

A cleaner way to do this without listing the type twice is by using a quirk of the sizeof operator:

int *array = malloc(i * sizeof *array);

Because sizeof doesn't evaluate the expression given as an operand, you're not dereferencing the (at this point) uninitialized pointer. It's not so big of a deal in cases where you're declaring and allocating all in one statement, but consider cases where the type changes and you do the allocation elsewhere. Then you'd need to grep the code looking for any instance of sizeof(int) in reference to that variable and change the type.

free array;

free() is a function:

free(array);
tux4life commented: Nice tip about sizeof ;) +13
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Assuming the time part of your date is consistent (such as midnight), it's a simple check against getdate():

"select * from usernames where [Username] = '" & TextBox2.Text & "' and [Password] = '" & TextBox1.Text & "' and [ExpiresOn] >= getdate()"

You might also consider using parameterized queries instead of string concatenation for security reasons (and to make the query easier to read).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So you're supposed to do this project, but you're asking for someone to send it to you? I'm sorry, but that sounds a lot like you're asking others to do your project for you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's both funny and sad the kind of ire people can have over an operating system debate.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

According to the manual WriteLine() returns void, you can't assign that to answer[i].

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How would you do it on paper? The first task is to understand the problem and solve it manually, step by step. Then you'll be in a better position to translate those steps into C++.

ddanbe commented: The way to answer! +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but you learn by understanding the old ones before diving into the new ones, right?

Not really. Outdated syntax is of historical interest, but if you want to learn how to program in a language, you should learn the latest version of the language.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

We're working on it, no worries.

tux4life commented: Like a boss! :D Wise words in the signature too ;) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Pointers are built-in types, they don't have constructors per se. If you want your ppointers to be null, make them null explicitly.

student *head = 0;
student *tail = 0;

Assuming those pointers are members of another class then you can certainly use the default constructor to make them null.:

class foo {
public:
    foo(): head(0), tail(0) {}
private:
    student *head, *tail;
};
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I assume you're asking about the ternary operator. This:

'left': (direction === 0) ? '-100%' : '100%'

Is roughly equivalent to this:

if (direction === 0) {
    'left': '-100%';
}
else {
    'left': '100%';
}

So it's just a expressionized version of an if..else statement.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I would appreciate it if someone can put more detailed comments in this source code to help me better understand it.

Sorry, but no. There won't always be someone around who's both capable and willing to read, understand, and recomment large amounts of code just to make things easier on you. Further, the comments a lot of us would put in probably wouldn't help you any more than the comments already there. Just work through the code bit by bit until you understand it better.

If you have specific questions about a small part of the code, then feel free to ask about it. But asking others to go through a significant amount of effort because you don't want to properly work through the code yourself is very inconsiderate.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Manually? With Excel scripting? Using third party code using an Excel library? Your question is specific as far as what you want to happen, but vague in terms of how you're looking to accomplish it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Here's a simple example:

#include <iostream>

class foo {
public:
    virtual void action() const { std::cout << "foo\n"; }
};

class bar: public foo {
public:
    virtual void action() const { std::cout << "bar\n"; }
};

class baz: public foo {
public:
    virtual void action() const { std::cout << "baz\n"; }
};

void do_action(const foo& obj)
{
    obj.action();
}

int main()
{
    do_action(foo());
    do_action(bar());
    do_action(baz());
}

The magic is in do_action(). It doesn't care what object in the inheritance hierarchy it's given as long as that object provides the correct interface (ie. it inherits or overrides action()). This means that you don't need to have three different overloads of do_action() that a foo, a bar, and a baz, respectively. It all just works with a reference to foo through virtual functions and dynamic binding.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But without it, I can safely say the min numbers of nodes is 4, correct?

If you're changing the rules then remove the other constraint and the minimum number of nodes is zero.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

wat confused me was least deep leaf has depth 2. Why so, it should be depth 3 if height is 3.

It means the leaf that's not at the deepest level (level 3, matching the height). In this case, 7 in the following tree is the "least deep" leaf. The height is 3, the least deep leaf is at level 2, and the tree is as full as it can be, so it's the answer to part a of your question:

             1
      2             3
  4       5     6       7
8   9   a   b c   d

That's 13 nodes, on the assumption that the height excludes the root. However, if the root is at depth 0 then that's more of a safe assumption than usual. For the minimum you need to recognize that the tree isn't required to be full or complete, and the smallest tree possible while maintaining the two restrictions (a height of 3 and the least deep leaf at level 2) is some variation of this:

             1
      2
  3       4
5

The height is still 3, the least deep leaf is still at level 2, and the tree cannot have fewer than 5 nodes without breaking one of those two invariants.

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

Um...your thread title and post message are completely unrelated. Can you be specific as to what you want to accomplish?

BARI DANIYAL commented: because own wast taged in titel +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The best reference you can find in terms of details is the standard, but it takes time to learn how to read for maximum value.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When I try to store the location of fp (file pointer) at a desirable location

How exactly are you doing this? While FILE is supposed to be an opaque type, it's generally implemented as a structure. If you're storing the pointer to a FILE then that's meaningless. If you're storing some serialization of the structure then that's largely meaningless as well (not to mention non-portable).

How do I know the location of any byte in a file from the file's first byte?

You can find the position within the file using ftell() or fgetpos(). Note that I didn't say that this is the location of any byte, because these functions return an encoded offset rather than a raw byte offset. It only matters when the file is opened in text mode, and even then only when newline characters are more than one byte on the platform (eg. Microsoft Windows).

But if I'm understanding your question correctly, this is what you want.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd like to think that the more experience one gains with reading and writing code, the less these little details matter. A modicum of common sense in the coding style and overall consistency is what matters.

This attitude is especially important if you plan to step into the professional world, because you'll be asked to compromise when designing style guidelines for a team or even throw out your personal style entirely to conform to an existing style guideline.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When I hear function I think about maths, with f(x)= 2x + 4.

Computer science and programming are rooted in mathematics, so that's not a bad way to approach the concepts. Taking your example, it translates easily to C++ like so:

int f(int x)
{
    return 2 * x + 4;
}

And by providing x, the caller can execute the function:

int main()
{
    std::cout << f(5) << '\n'; // 2 * 5 + 4
}

What is a function's caller?

The code that executes a function, provides arguments, and accepts the return value is the caller. In the example above, main() is the caller for f(), and f() is the callee.

PrimePackster commented: Worth it! +3
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I ban you for not closing your <MICHAEL> tag.

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

I considered forcing you to post the actual error message rather than the error code because most people haven't memorized all of the codes for Visual C++. But if you prototype your min() function, you'll fix those two and be left with the rogue semicolon in one of your output statements.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

because bl is capable of handling more and much intense instructions, am I right?

Not really. AL and BL are functionally identical, but because AL will be reused almost immediately by the next few instructions, its contents must be saved into another register or they'll be lost.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Btw, you, again, introduced three map lookups where only one is needed

Because, again, I'm not especially concerned over what I feel is a negligible cost.

The best, simplest and clearest solution would certainly have been to store them by value, but short of that, go with unique_ptr.

I don't disagree, but you're neglecting the arbitrary restrictions I put in place when writing this code. Unless you have an example of using unique_ptr without any explicit use of new or raw pointers, I'll stand by my choice of shared_ptr. Off the top of my head, I can't think of any way to use unique_ptr directly without violating one or both of those restrictions.

Dumping those restrictions and with more of an eye toward map performance, I'd probably go with something more like this (quickly done, so please excuse any bugs or obvious omissions):

#include <iterator>
#include <map>
#include <memory>
#include <stack>
#include <string>

template <typename CharT>
class trie {
    struct node {
        std::map<CharT, std::unique_ptr<node>> link;
        bool terminal;
        CharT key;
    public:
        node(CharT key): terminal(false), key(key) { }
    };

    std::unique_ptr<node> root;
public:
    trie(): root(new node(CharT())) {}

    /*
        @description:
            Adds a word path specified by s to the trie.
    */
    void add(const std::basic_string<CharT>& s);

    /*
        @description:
            Removes a word path specified by s from the trie.
    */
    void remove(const std::basic_string<CharT>& s);

    /*
        @description:
            Searches for the prefix s when require_terminal is false or exactly matches
            a terminal path created by add() when require_terminal is true.
        @returns:
            basic_string::npos is …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just to make things crystal clear, NULL is a macro intended for use as a null pointer, it should never be used as the null character ('\0', also known as NUL with one L). While in C++ NULL is defined as 0 and that's compatible with the null character, it will confuse the bejeebus out of readers of your code.

So to summarize best practice:

  • In pointer context use nullptr. Prior to C++11 use either NULL or 0.
  • In character context use '\0'. 0 is acceptable, but not recommended.
  • In integer context use 0.
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I notice that add() got broken in your changes. It just traverses the (probably nonexistent) path rather than adding keys where they don't exist. You'd end up dereferencing null pointers unless new nodes are added at the right places:

template <typename String>
void add(const String& s) {
    auto it = &root;
    // Traverse the path and extend it as necessary
    for (auto c : s) {
        if (!(*it)->link[c]) {
            (*it)->link[c].reset(new node());
        }
        it = &( (*it)->link[c] );
    }
    (*it)->terminal = true;
}

Is there any reason at all for using a shared_ptr<node> for your links?

Yes. It's not a good reason, but I do have a reason. ;)

Why not use a unique_ptr<node>?

Ultimately it comes down to two things:

  1. As an arbitrary restriction I didn't want to use new explicitly, and there's no std::make_unique(). I could have written my own make_unique(), but like I said, it's just a reason, not a good one.

  2. I didn't want to diddle about working around unique_ptr's ownership rules with non-owning aliases, and as another arbitrary restriction I didn't want to use raw pointers at all.

For this class, I'm not especially concerned about the overhead of shared_ptr, so I went with what at the time felt like an easier approach. If it were intended as a serious library I'd put more work into optimization.

const-correctness for the match() function (doesn't change the object);

Bugger, I totally forgot to make match() const. Thanks for the …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please tell me how to optimise.

Delete the printf() line. I/O constitutes some of the most, if not the most time intensive operations.

On a side note, your code exhibits undefined behavior by accessing uninitialized memory. There's no guarantee that the program won't crash intermittently.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

King book where no strings are covered and I have to do this only by arrays and loops not by string functions like you.

Can you not read? I already stated that the only string function I used was strrev(). I also essentially duplicated the effect of strrev() later in the code without using any other string functions. You can replace size_t with int, the strrev() call with a facsimile and there will be nothing from the string.h header used in that code. It's mostly a manual solution, though to be honest that's only because strrev() isn't suitable for reversing substrings.

I can't tell if you're really as stupid as you're presenting yourself, or just pissed off that I'm forcing you to use your brain and do actual work.

I think you can't solve this question by another method without strings.

If you mean without string functions provided by the standard library then I'll direct you to exhibit A, where I've written all of those functions. I'm in no way dependent on the standard library. If it's not there, I'll just write it myself (and I have). Can you do the same?

So you might consider that just because someone refuses to do your homework for you, it doesn't mean they're unable to do it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't cage a swallow can you?

I'm assuming you meant the reversal to be "you can't swallow a cage can you?", because the one you posted would require a bit more work in specifying which words are being reversed as opposed to reversing all of them.

And the "hint" actually tells you the whole algorithm:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[] = "you can cage a swallow can't you";
    size_t begin = 0, end;
    size_t i;

    puts(s);

    /* Reverse the whole string */
    _strrev(s);

    /* Reverse each word in the string */
    while (s[begin]) {
        end = begin;

        /* Find the end of the word */
        while (s[end] && !isspace(s[end])) {
            ++end;
        }

        /* Reverse the word */
        for (i = end - 1; begin < i; begin++, i--) {
            char temp = s[i];
            s[i] = s[begin];
            s[begin] = temp;
        }

        /* Find the beginning of the next word */
        begin = end;

        while (s[begin] && isspace(s[begin])) {
            ++begin;
        }
    }

    puts(s);

    return 0;
}

And before you cry that I didn't comply with the restrictions of the assignment, it's not my assignment to do, so you can use my code as a template but not turn it in for a grade.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As the title says, it's a simple trie class written in C++11. The only operations implemented are insert, remove, and search (both prefix and exact matches). I sort of abandoned the test/debug process, and while I'm somewhat confident that there aren't any serious bugs, I can't guarantee it. Use this class at your own risk. ;)

nitin1 commented: well done sir!! :-) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How i can combine them together ?

That depends on your compiler, really. If you're using an IDE you can just lump everything together in the same project and then build it. The IDE will take care of the grunt work. If you're using a command line compiler, they tend to be fairly consistent in that you provide all of the .c files for compilation (the .h files will be handled automatically) and they will be compiled then linked into an executable:

$ gcc SetADT.c main.c

You can also compile them separately and link them:

$ gcc -c SetADT.c
$ gcc -c main.c
$ gcc SetADT.o main.o

The options and extensions may be different, but that's the general pattern for command line compilation.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If you're writing code that depends on an exact data type size (and often on a data type size in general), you're probably doing something wrong.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

tl;dr - Everyone starts as a newbie but you only learn by exploring and using the things you don't know about.

I want to preface this with the comment that there's no malice or judgement in this post, it's just an observation based on limited experience.

I'm reminded of the difference between "newbie" and "noob" in MMO circles. A newbie is a beginner, any ignorance is temporary as they learn how things work. A noob is willfully ignorant and refuses to learn regardless of how long they've been playing.

riahc3 strikes me as the type that reached a point of being able to write code that "works" and doesn't want to continue improving. What he knows is sufficient to do what he wants, and that's all that matters. Granted this is an observation over the course of maybe three threads, so it's hardly a statistically significant sample.

ddanbe commented: Very well spoken, like your signare. +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why does the coordinator need a completely separate table and functions? Typically administrative users are still users, the only difference is permissions attached to the account.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Specify a section. The three most common are:

  • 1: General Commands
  • 2: System Calls
  • 3: Library Functions

So if you want the open() function, you'd search using man 3 open

fyra commented: thanks. +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What I know is that the size of Pointers vary according to the size of Address.

The size of pointers can vary according to the datatype they point to as well. So a pointer to int isn't guaranteed to be the same size as a pointer to double. There's also no guarantee that the size of a pointer match the largest address on the platform. For modern PC platforms you'll find that they do tend to have these properties, but the C++ language definition does not require it.

It's kind of silly to make assumptions about your platform if it's not needed, and I'd be very interested to hear ways that it would be needed in this case.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sorry, but it doesn't work like that. All of the links in your tree are pointers to the memory of your current process. If you save those pointer values, they'll be meaningless when read by another proces. Really the best you can do is save the tree's data in a way that's convenient for reconstructing it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does that clear things up?

Yes indeed. That has nothing to do with Daniweb (not 100% on that, but I'm reasonably sure). It's your browser remembering previous input. I don't get any options because I haven't flagged any posts. ;)

The other issue can be seen by logging out of your account and you read the message, it says its an error :)

I was actually confused about the first issue and kind of ignored the second. I'll look into it.

edit: No issues logging out from this thread. What pages are you viewing when it happens?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

4 * sizeof(double*) bytes. You can't assume the size of a pointer, so the 32-bit machine part is irrelevant to the question.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have unfortunately installed a fake copy of xp

Then unfortunately you should uninstall the pirated copy, buy a legit copy, and install that.

the microsoft co-operation has tracked it and the screen display has turned upside down

That's utterly ridiculous. I don't doubt that you've accidentally hit the hotkey to flip your screen (fixable with alt+ctrl+down arrow), but to blame it on Microsoft tracking your piracy and doing something so silly without direct remote access is ludicrous.

By the way, discussion of pirated software is against Daniweb's rules.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You don't embed DLLs into executables, you package them in an installer such that they're copied to and registered with the target machine. So in your case you need to create a setup and deployment project. This project will produce an MSI that will handle installing all of the required files to run your application.

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

First and foremost, I think insFirstNode() is poorly named. It implies that the node will be prepended to the list in all cases, but in reality what it does is append.

As far as converting the list to an array, it's simplicity itself. But a few things will need to change for the code to make sense. You can't really call the class linkedlist anymore. Since it's only a collection of students, why not student_collection? From there just keep an index for the most recently appended item, and increment it when you add a new student:

struct student {
    string name;
    int id;
    string prog;
    double cgpa;
};

class student_collection {
private:
    student _students[50];
    int last;
public:
    student_collection(): last(0) {}

    void add(string name, int id, string prog, double cgpa);

    //...
};

void student_collection::add(string name, int id,string prog, double cgpa)
{
    if (last == sizeof _students / sizeof *_students) {
        throw "Oh noes!";
    }

    _students[last].name = name;
    _students[last].id = id;
    _students[last].prog = prog;
    _students[last].cgpa = cgpa;

    ++last;
}

Of course now you have to concern yourself over reaching the end of the array, and the size of the array on the stack becoming a problem for a large number of students. Ideally you'd dump arrays entirely in favor of std::vector, which handles dynamic allocation of memory on the heap and will adjust its size accordingly.

on93 commented: thanks +1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm not sure I understand what you're describing, can you be more specific?