Narue 5,707 Bad Cop Team Colleague

That would be correct if id were your key, but data is your key. getid will need to do a full traversal rather than a simple search, in which case r might be NULL several times before the correct node is found:

void MyBST::getid(treenode* r, int &stuid)
{
    if (r != NULL) {
        getid(r->left, stuid);

        if (r->id == stuid)
            cout<<"Found "<< r->id <<'\n';

        getid(r->right, stuid);
    }
}
Narue 5,707 Bad Cop Team Colleague

In getid, what happens when r is NULL?

Narue 5,707 Bad Cop Team Colleague

>Hey Narue, if we just change the paramenter passing to "pass by reference", the function would accept a File pointer?
Of course not. You simply didn't understand the question.

Narue 5,707 Bad Cop Team Colleague

>Consider that the files are attached.
I'd still ask you to be specific, and the char bug for fgetc is still present.

>Actually i wanted to ask whether this correct
That's quite a bit different than "tell me all of the differences between FILE* and ifstream". :icon_rolleyes: And no, it's not correct. For reasons that I won't go into, stream objects aren't copyable. You need to pass by reference:

void encode_file (ifstream& fin, ofstream& fout)

Then it should work.

>ie they read and write a single byte at a time only (if ch is char type)
That's correct, but fgetc can also return EOF. It's for this reason that fgetc returns int rather than char.

Narue 5,707 Bad Cop Team Colleague

>I want to know the differences in between two?
That's a tough comparison. Can you be a little more specific?

>also is this code equivalent
No, the C code has two bugs (an uninitialized FILE pointer, and using char instead of int for the return value of fgetc). The C++ code is correct due to ifstream's constructor, but it won't do anything because the stream isn't attached to a file.

Narue 5,707 Bad Cop Team Colleague

>hi narue i am unable to understand
What's to understand? It's possible to get an error message, but unlikely. Don't expect your compiler to warn you about doing obviously stupid things, you need to use a modicum of gray matter to program in C.

Narue 5,707 Bad Cop Team Colleague

>Am I correct?? I only want to verify.. Please answer.
Pretty much, yes.

>So in link list, am I supposed to follow the same idea?
The underlying concept doesn't change. However, note that while arrays give you fast random access, linked lists aren't nearly as forgiving. One benefit of merge sort is that you can sort streamed data in an efficient manner, and traversal of a linked list can be viewed as a stream. I'd suggest looking up information about a "natural" merge sort, which is better suited to sequential lists than the usual array-based merge sort (which is optimized for random access lists).

Narue 5,707 Bad Cop Team Colleague

Sounds like homework. Why don't you implement all of those algorithms and add logic to count the iterations? It's not hard. I'll even give you a link to a page with code for all of the algorithms you specified.

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

Provided you supply either an overloaded operator<, or a comparison predicate in the map constructor, you can use any user-defined type as the key.

Narue 5,707 Bad Cop Team Colleague

Your base class should have a virtual destructor.

Narue 5,707 Bad Cop Team Colleague

Separate how? Do you want them in two different arrays? Do you want to print them on two different lines? Do you want the negative numbers to sing and the positive numbers to dance? Be specific, or don't complain when nobody helps you.

Narue 5,707 Bad Cop Team Colleague

>If uwe use a array for sequence (a e i o u) I think code are more easy........
If you had bothered to read the original question, arrays are explicitly prohibited.

Narue 5,707 Bad Cop Team Colleague
Ancient Dragon commented: Nice tutorial -- thanks for posting that link :) +33
Narue 5,707 Bad Cop Team Colleague

It's the second one. Your first snippet is two if statements, one with and one without an else clause.

Narue 5,707 Bad Cop Team Colleague

>What do you mean by "All sizeof will give you for a pointer is the size of the pointer, not what the pointer contains" ?
I'll answer your question with another question: Will the following code print the same number twice?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    struct test {
        char data[256];
    } obj;
    struct test *p = &obj;

    printf("%lu\n", (unsigned long)sizeof obj);
    printf("%lu\n", (unsigned long)sizeof p);

    return 0;
}
Narue 5,707 Bad Cop Team Colleague

Don't bump your threads. It's rude.

Narue 5,707 Bad Cop Team Colleague

>can you please explain with example
What's wrong with the examples that were already given in this thread?

Narue 5,707 Bad Cop Team Colleague

If I understand your question correctly, the compiler is not allowed to reorder struct/class members between access specifiers. You can make the order unspecified like so:

struct Vertex
{
   public: Vector3 p;
   public: Vector3 n;
   public: Vector3 c;
}
Narue 5,707 Bad Cop Team Colleague

>Which one of these would be better?
Neither is better. Your first idea less flexible but offers a complete tree. Your second idea matches the linked version, but also has the disadvantages of an unbalanced tree. The former is what I'd expect for an array-based heap, and the latter strikes me more as a general binary tree structure implemented using an array.

Narue 5,707 Bad Cop Team Colleague

>up
Bumping is rude. Now I'll likely ignore all of your subsequent threads.

Narue 5,707 Bad Cop Team Colleague

Floating-point values are imprecise by nature. Trying to compare them for equality is fraught with peril. ;) A fuzzy comparison is usually the best approach. As long as the difference of the two values is less than a certain epsilon, you're good:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
    int number1 = 0;
    int number2 = 0;
    double user_answer = 0;
    double sum = 0;

    cout  <<  "Enter 2 Numbers to be Divided: \n"; 
    cin  >>  number1;
    cin  >>  number2; 

    sum = static_cast<double>(number1) / number2;   

    cout <<"Enter Your Answer: ";
    cin >> user_answer;

    if(fabs(user_answer - sum) < .001)
        cout << "Correct" << endl;
    else 
        cout <<  "Incorrect.  Answer is: " <<  setprecision(2) << fixed << sum << endl; 

    system("PAUSE");
    return EXIT_SUCCESS;
}
Narue 5,707 Bad Cop Team Colleague

The code isn't C++, it's Java.

[edit]
I'd recommend this site for your binary search tree needs. It's largely in C, but the conversion is much easier than from Java.
[/edit]

Narue 5,707 Bad Cop Team Colleague

>But what are man pages ....
You program on Linux and don't know how to consult the man pages? Open up a command prompt and type "man". :)

>What do you mean by implement...
I mean do it yourself. If the function doesn't exist, write it.

Narue 5,707 Bad Cop Team Colleague

>Can you describe an algorithm that is O(nm)?
>I'm guessing it would be something like traversing a two-dimensional array.

Good guess. That's an excellent way of describing O(nm).

>The two loops needed to do that is throwing me off however.
>It's making me think that it could be O(n^2).

If it helps, you can think of O(n^2) as O(nn), redundant as it may be. The difference here is that with O(nm), n and m don't have to be the same value. They can be, and in that case the time complexity is indeed quadratic.

Narue 5,707 Bad Cop Team Colleague

Check your man pages, strrev might actually be present. If not, it's a trivial function to implement for your personal library:

#include <string.h>

#define SWAP(T, a, b) \
    do { T save = (a); (a) = (b); (b) = save; } while (0)

char *reverse_string(char *s)
{
    size_t len = strlen(s);

    if (len > 1) {
        char *a = s;
        char *b = s + len - 1;

        for (; a < b; ++a, --b)
            SWAP(char, *a, *b);
    }

    return s;
}
Narue 5,707 Bad Cop Team Colleague

>I'm getting a warning message while compiling.
Judging by your thread title, the warning is something along the lines of "gets is unsafe". The usual recommendation is to prefer fgets because it allows one to provide a limit to the buffer whereas gets will just happily read until a newline (walking right off the end of the buffer if necessary).

>You can force gcc to hide warnings by compiling with the -w flag.
Note that while it's possible to hide warnings, most of the time it's a supremely stupid idea. Warnings are there for a reason. They don't keep a program from compiling, but they do notify you of potential problems at runtime.

Narue 5,707 Bad Cop Team Colleague

>then why there is no error message like
>"too many parameters are not allowed"

While a compiler could warn you at build time about a different number of actual arguments than the format string suggests, it's more complicated than you might think.

>do you mean that 1 act as parameter having format specifier and 2 and 3
>are acting as string values that will replace the %s in 1st parameter?

Correct. Only the first parameter to scanf acts as a format string.

Narue 5,707 Bad Cop Team Colleague

There's no operator for a factorial in C++, you need to perform the multiplications manually. You have the right idea though, now it's just a matter of details. Here are a couple of things to consider down the road:

  • This factorial function accepts a signed integer argument, which means n could be a negative value. A robust implementation would handle negative values sensibly.
  • Factorials are a great way to overflow your data type because they grow so quickly. An overflow check would be a fantastic idea in the case of an n that's too large for int.

>int product = 5;
Start the product at n rather than any hard value. Also, be sure to check for 0 as 0! is 1.

>for (n > 0)
Anything multiplied by 1 is itself, so you can stop the loop before n reaches 1.

>product = n!;
Obviously this should be product = product * n; . Otherwise a factorial function would be unnecessary. ;)

>n--;
product is set to n originally, so you need to decrement n before the multiplication step. As it is you'll start off with n * n rather than n * n - 1 .

Narue 5,707 Bad Cop Team Colleague

>It is frustrating how you have to "dance around" to do such a simple thing.
Stream input is unintuitive. As an implementer of the standard library, I consider myself to be above average in terms of proficiency. But the behavior of stream I/O still catches me off guard more than any other aspect of C. In fact, I'm often amazed that beginners can manage at all. ;)

Narue 5,707 Bad Cop Team Colleague

>I had no idea you can use a (void*) as a cast.
void* is the generic pointer type. You're pretty much guaranteed to be able to convert a pointer to and from void* without any problems. There's no such guarantee for any other arbitrary combination of pointer types. Performing a safe typecast is your first step in troubleshooting the problem.

>Nor have I seen the wording 'transient pointer'.
It's not official terminology. In fact, I made it up on the spot. ;)

Narue 5,707 Bad Cop Team Colleague

>char str[31], ch;
Let's start with this common error. EOF is guaranteed to be a negative quantity, but char may be unsigned. getc returns int for a reason, and that reason is to guarantee that EOF is representable regardless of the signedness of vanilla char.

>while((ch = getc(fp)) != '\n')
Your loops are contradictory and ultimately infinite. The outer loop waits for EOF, but the inner loop (which is your actual reading loop) doesn't stop when it sees EOF.

creeps commented: THE answer +1
Narue 5,707 Bad Cop Team Colleague

>I was thinking of implementing a hash table function as you mentioned, but the assignment is to make an array.
Hash tables are typically implemented as arrays. In this case, an array of linked lists. So the type of your array should be a pointer to the node type:

node *my_array[N] = {0};
Narue 5,707 Bad Cop Team Colleague

Okay, what do you have so far? It sounds like you're working toward a chained hash table implementation, because the array of linked lists for exact matches is a common collision resolution strategy in hash tables.

Narue 5,707 Bad Cop Team Colleague

>In a contact object I store a pointer, which may be for a BCorp or a BPrsn, so I cast it as a (double*).
I can't begin to fathom why you're using double* instead of void* for a safe transient pointer type. Could you explain your reasoning here?

Narue 5,707 Bad Cop Team Colleague

>You have to check for denominator and if it is zero, then throw exception.
I wouldn't use an exception in this case. Perhaps an assertion, because the user's input should be checked and sanitized long before the division takes place. In which case division by zero would be a programmer error in gracefully handling bad input rather than a truly exceptional runtime situation.

Narue 5,707 Bad Cop Team Colleague

...

I can't even laugh at this, it's too sad. I wish you the best of luck, because that's all you have left.

Narue 5,707 Bad Cop Team Colleague

Are those files in your current working directory? Try passing in a full path. Most likely the program isn't looking where you think it's looking for the specified file.

Narue 5,707 Bad Cop Team Colleague

>This is C section
Pardon. You don't know C, and randomly pasted shit into a text editor. The fact remains that you clearly don't have the prerequisite knowledge to write this program.

>my whole programs are combination of various code snippets
I strongly recommend you learn more about the basic language before jumping into Win32. That opens up a whole new can of worms, and if you're not pretty comfortable with straight C, it'll be a nightmare.

Narue 5,707 Bad Cop Team Colleague

What does your filename string look like?

Narue 5,707 Bad Cop Team Colleague

In other words, you don't know anything about C++ and just randomly pasted the contents of a tutorial into your text editor. What a surprise that it doesn't compile. :icon_rolleyes:

tux4life commented: Hehe, you hit the nail right on the head. :) +8
Narue 5,707 Bad Cop Team Colleague

Then I suppose you've got some reading to do, because every book on C I'm aware of will cover this. I'm happy to supplement your classes/books/tutorials with one-on-one tutoring, but I'm not going to replace them.

Narue 5,707 Bad Cop Team Colleague

Homework is designed to help you solidify your knowledge. If someone else does it for you, you don't learn anything except how to be helpless. So try it first, and if you have problems post your code so someone can help you correct the problems.

Narue 5,707 Bad Cop Team Colleague

>What is the use of writing an integer before d or any formal specifiers.
It gives you more control over the output than the defaults. You can override field width, padding, justification, precision, sign display, and whatnot. In this case, you're changing the field width. For example, if you're writing a fixed width report where a field has N columns assigned to it, you can say fprintf(out, "%*d", N, value); to print the value right justified with whitespace padding that fits the the fixed column[1]. This is as opposed to the alternative where you would need a solution like this:

size_t value_len = get_value_len(value);

/* Pad unused field area with whitespace */
for (i = 0; i < N - value_len; i++)
    putc(' ', out);

fprintf(out, "%d", value);

A more common example might be pretty printing a matrix of integers:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i, j;

    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++)
            printf("%4d", rand() % 100);
        puts("");
    }

    return 0;
}

Note that the last value on each row does not end with a space (which would happen if you used "%d " as the format string), and the columns are lined up nicely despite values of varying width.


[1] Of course, the default behavior is that if the value is larger than the field width, it's not truncated, so in the case of a fixed width report you …

Narue 5,707 Bad Cop Team Colleague

>but its has its own merits...
There are no merits to gets.

>say, the ease of accepting strings...
I've argued this point with authors before (Dan Gookin is one such example), and have yet to hear a sufficiently good explanation of how fgets(s, n, stdin) is so hard that it justifies the teaching of unsafe programming habits. Also, it's very common for programmers to prefer the first technique they learn for a task, and if the first thing they learn is gets...

Narue 5,707 Bad Cop Team Colleague

To create an array of objects without initialization, the object type must have a default constructor. The rule in C++ is that a default constructor is generated for you if you don't explicitly define any constructor explicitly. Your class has a three parameter constructor, so no default constructor is generated automatically. You need to do it explicitly.

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

What exactly do you mean by "verify mail"?

Narue 5,707 Bad Cop Team Colleague

>Can anyone help me on how to display my infix notation with parentheses?
It's simple enough, just print your parens at the operator level for sub-expressions you want to wrap (you probably want to exclude unary operators):

template <class Item>
void infix(exp_tree_node<Item>* root)
{
    if (root != NULL) {
        if (root->is_op() && !root->is_unary())
            cout.put('(');

        infix(root->left());
        cout<< root->value();
        infix(root->right());

        if (root->is_op() && !root->is_unary())
            cout.put(')');
    }
}

>when the expression grows, for some reason, its outputting the incorrect notations for the prefix and postfix
Can you provide an example?

Narue 5,707 Bad Cop Team Colleague

I want a publisher to let me write a book on C. But wanting something doesn't make it happen, especially when it involves other people doing work for you with no benefit to them.

Takeshi91k commented: Nice Post :D +0