Narue 5,707 Bad Cop Team Colleague
for(int i=0; i<count; i++)
{
    data.push_back(input);
}

This is your input loop, but there's no input going on. You need to give the user time to input a value, then add the value to your vector:

for (int i = 0; i < count; i++)
{
    if (cin>> input)
        data.push_back(input);
}
Narue 5,707 Bad Cop Team Colleague

Performance is application-specific. If the library is too slow for your application, ditch it. It's really that simple, but I strongly suspect you're worrying about nothing. Unless you can prove that there's a noticeable and truly detrimental performance hit, spending time looking for ways to squeeze out more speed is unproductive.

Narue 5,707 Bad Cop Team Colleague

>It's standard, if not for beginners.
Which is the single most annoying problem with how C is taught. Pointers are an integral part of the language, and really very simple conceptually. It's my opinion that they should be introduced as early as possible.

>Three ampersand (star) programmers are generally mocked a bit, however.
Three is uncommon, but fine. Four or more might raise some eyebrows though. I've never used more than five in production code.

Narue 5,707 Bad Cop Team Colleague

>The C Programming Language...
No, The C Programming Language (also known as K&R) is a fabulous book for learning C90 if you already know how to program in another language. It's not very friendly to absolute beginners though. C Programming - A Modern Approach by K. N. King is often recommended as a first book on C. I'm partial to Pointers on C by Kenneth Reek, but it's no longer in print and likely to be expensive.

>which better
>learn c in 21 days or c for dummies 2nd edition

They're both bottom of the barrel and shouldn't be considered.

Narue 5,707 Bad Cop Team Colleague

It's pseudocode, there aren't any rules to follow, just make sure your logic is sound. Some people find flowcharts more intuitive, so you could start with that part of the assignment and derive the pseudocode from your result.

To be perfectly frank, this smells like a Programming 101 assignment. I understand that it can be overwhelming when starting out, but presumably you learned how to do all of this in class. I have a hard time believing that you don't know how to start unless you weren't paying attention in class (in which case I have no reason to help you because you won't help yourself first).

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

No.

Narue 5,707 Bad Cop Team Colleague

Yes and no. It depends upon whom you ask, but a common distinction is that the software engineer is more involved in the design aspects than the programmer, who simply churns out code according to an already specified design (ie. a code monkey).

Narue 5,707 Bad Cop Team Colleague

>What are the default values of the fundamental types?
That's a fun little can of worms. :)

Narue 5,707 Bad Cop Team Colleague

You're overwhelming yourself. Start by reading the file and printing out each character, one-by-one. Break the problem down into smaller problems that are easier to tackle individually.

Little successes are important, and having something simple that works is much better for keeping your spirits up while working on a problem than something complex that doesn't work. It all snowballs from there. :)

Narue 5,707 Bad Cop Team Colleague

>Better than crying about fouls, why the moderators / owner of
>this forum is not taking steps to prevent these things happening?

You think it hasn't come up countless times? There are a lot of smart people around here, and all of the big names I can think of have offered ideas for improving matters.

I've concluded that Dani simply doesn't care about the quality of the forum, only the activity. More members means more money; driving away the trash and dead weight is counterproductive to that goal.

Narue 5,707 Bad Cop Team Colleague

>The solution that i gave was not for the Palindrome problem
>It was to bypass converting upper and lowercase.

I don't understand. Are you still trying to defend your suggestion? Let's pretend stricmp is portable for a moment. Please show me what solution you were thinking of for testing palindromes that prompted the suggestion of stricmp.

Narue 5,707 Bad Cop Team Colleague

>it is assumed that the user is entering single digit strings (such as "a 1 2 3 d"(enter))
If all you want is single character input, you don't even need scanf. Just use getchar and test for whitespace manually:

int ch;

while ((ch = getchar()) != '\n' && ch != EOF) {
    if (isspace(ch))
        continue;

    printf("Processing '%c'\n", ch);
}

However, I have yet to see such a program that doesn't grow into more than single character words. In that case, scanf can be used without any problems provided the assumption holds that whitespace or end-of-file terminates each word.

One problem in this case is that scanf doesn't differentiate between newlines and other whitespace. It's all the same to scanf. But because scanf stops reading at whitespace, you can read the next character and see which one it was:

#include <stdio.h>

int main(void)
{
    char word[512];
    char next;

    while (scanf("%511s%c", word, &next) >= 1) {
        printf(">%s<\n", word);

        if (next == '\n')
            break;
    }

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

I'll wait to see what the other people reading this thread have to say before giving you a good solution. :)

Narue 5,707 Bad Cop Team Colleague

Did you include the <string> header? Do you have a using namespace std or using std::string somewhere prior to your usage of the string name?

Narue 5,707 Bad Cop Team Colleague

>Obviously, the raw array is faster than the dynamic array of ublas and multi_array.
Not by much, and the small difference can easily be attributed to the overhead of member function calls vs. direct indexing. So ask yourself one question: Is the difference a deal-breaker for using Boost? You really shouldn't worry about optimizing unless there's a need for it and you can justify the effort involved in implementing the optimizations.

Narue 5,707 Bad Cop Team Colleague

>use stricmp() to compare the charachter.
That's neither a solution nor a good one even if it were. stricmp is typically implemented to match strcmp except with case insensitivity. It's not suitable for a palindrome test, which on top of requiring a reverse direction comparison also has to take zero-weight characters (ie. whitespace) into account.

stricmp is also not a standard function, which means even if it were a viable solution, it's not available on all compilers. You're making an unwarranted assumption about the OP's environment.

Narue 5,707 Bad Cop Team Colleague

>BST::~BST();
How is it non-recursive when you try to call the destructor recursively (not a good idea)? Personally, I'm a fan of unraveling the tree into a vine to simplify destruction. Then it just becomes a linked list destruction algorithm:

BST::~BST()
{
    BinNodePtr save;

    for (BinNodePtr it = myRoot; it != NULL; it = save) {
        if (it->left == NULL) {
            save = it->right;
            delete it;
        }
        else {
            // Rotate the left link into a right link
            save = it->left;
            it->left = save->right;
            save->right = it;
        }
    }
}
Narue 5,707 Bad Cop Team Colleague

Two things:

  1. tolower doesn't take a reference parameter, it returns the converted character if such a conversion exists. The immediate problem in your code is that you aren't saving the converted character back into ret[[I]x[/I]].
  2. tolower will accept any value in the range of unsigned char or EOF. If there's a lower case conversion, that value will be returned, otherwise the value you pass will be returned. As such, there's no reason to call isupper first. You can simply do this:
    ret[r] = tolower(ret[r]);
    ret[l] = tolower(ret[l]);

    It's generally best practice to cast the argument to unsigned char, but there are exceptions to that guideline, so I won't get into it now. It's not a big enough issue that you can't wait until a more appropriate time to learn about the various idiosyncrasies of char.

Narue 5,707 Bad Cop Team Colleague

>Now may a compiler optimize that to constant value so that it'll be the same as following?
Yes. Will it? Probably not. The only meaningful optimization here is memory footprint, and then only for the handful of types that are likely to be smaller than a reference. It's not worth the extra complexity of optimizing for such a small gain.

Narue 5,707 Bad Cop Team Colleague

>while((fscanf(ifile,"%s%d",m.ip,&m.num))!=EOF)
I'd start with the fact that when fscanf is called, m.ip is an uninitialized pointer.

On a side note, using EOF as the test against fscanf isn't wrong, but it's not completely right either. One test case where your condition fails is if the string reads successfully, but the integer fails. In that case fscanf will return the number of successful conversions. As such, it's best practice to test for the number of expected conversions:

while (fscanf(ifile,"%s%d", m.ip, &m.num) != 2)

This also covers the case where fscanf returns EOF, because EOF is guaranteed to be a negative quantity.

Narue 5,707 Bad Cop Team Colleague

Wow. While I'm sure this isn't a record, one lazy student asking for "help"[1] on an assignment followed immediately by another lazy student hijacking the thread asking for "help" on an unrelated assignment is a wonderful example of the present progression toward the fall of Daniweb.


[1] "Help", of course, being loser's slang for "do it for me so I don't have to think".

Narue 5,707 Bad Cop Team Colleague

>The largest number of elements an array can hold in C is whatever your implementation defines size_t as...
65535 bytes is the minimum size for an object, so you're guaranteed to have an array of sizeof(array)/sizeof(array[0]) <= 65535. Beyond that you enter the realm of non-portable where hitting runtime stack limitations becomes a possibility. You're not guaranteed to have an array of size_t elements, and in practice that's very unlikely to be a successful declaration.

If you want a large array, my recommendation is to do it with dynamic memory rather than actual arrays.

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

>I observe that the output it gets done only when cin.get(...) is being executed dunno why.
Because cout is tied to cin. When you make an input request on cin, cout is automatically flushed.

>please tell me a way to empty(forcefully output) a buffer at will.
There are a number of ways, but the two most common are:

cout<<endl; // Print a newline and flush
cout<<flush; // Just flush
Narue 5,707 Bad Cop Team Colleague

>The following code swaps values between 2 variables without using a temp variable.....
Actually, it invokes undefined behavior and could really do anything. If you add sequence points between the operations, then the code is technically correct:

a ^= b;
b ^= a;
a ^= b;

Figure out the bit value for a and b, then work it out yourself using the truth table for XOR:

INPUT 	OUTPUT
A 	B 	A XOR B
0 	0 	0
0 	1 	1
1 	0 	1
1 	1 	0

So let's say a == 1010 and b == 0101:

1010 XOR 0101 = 1111 (a = 1111)
0101 XOR 1111 = 1010 (b = 1010)
1111 XOR 1010 = 0101 (a = 0101)

End result, a == 0101 and b == 1010, which is an inversion of the original state. The XOR swap is an archaic trick to avoid the need for a temporary variable. It's still relevant if you're writing assembly or on a severely memory constrained platform, but for the most part not recommended because

  1. As you've proven, it's not as obvious what's going on.
  2. Tricks like this can actually hinder a compiler's optimizer, resulting in less efficient machine code.
  3. It doesn't always work. For example, if you XOR an object with itself, the result is 0 rather than the expected unchanged value. When a common implementation uses pointers, it's not an unexpected situation and needs a special case (which further complicates and slows …
Narue 5,707 Bad Cop Team Colleague

Why not use Visual C++ Express? It's free.

Narue 5,707 Bad Cop Team Colleague

Array perhaps?

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

You're not updating the root node:

node *insert(node *parent, node *child)
{
    if (parent == NULL) {
        parent = child;
        return parent;
    } 
    if (child -> val < parent -> val) {
        parent->left = insert(parent -> left, child);
    } else {
        parent->right = insert(parent -> right, child);
    }

    return parent;
}
root = insert(root, curr);

The code you have fails because printout doesn't take into account that tree might be a null pointer.

Narue 5,707 Bad Cop Team Colleague

>If it was C++ shouldn't the includes be
Not necessarily. The standard C headers are still supported by standard C++, just deprecated. It's unlikely that they'll be removed from the C++ standard as well, because that would destroy code compatibility with C (a guiding principle from day one).

Narue 5,707 Bad Cop Team Colleague

Please don't create a new thread with the same question. Cross posting is just as bad as posting in the wrong place (if not worse). Instead, flag your own thread as bad with the note "this belongs in so and so forum, please move it" for the moderators. Then they can clean up your mess.

Narue 5,707 Bad Cop Team Colleague

If I was creating this simple binary tree I would create my structure like so

struct tree_el 
{
  int *val;
  struct tree_el *right; 
  struct tree_el *left;
};

With an int pointer...That way you can test if the pointer exists with a simple statement like

struct tree_el *root = (struct tree_el *)malloc(sizeof(struct tree_el));

if (root->val)
{

}

Um, perhaps you could elaborate on the logic of this for us slower kids? Because your idea as stated is both illogical and broken.

>@Narue
>i did not get you. It does compile.

Yes, it does compile. That was my point. gerard was wrong in saying that the code is not C++.

Narue 5,707 Bad Cop Team Colleague

>You know this is written in C not C++...
You know that this compiles as C++ too...oh, apparently not. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

I get that, but you're clearly confused by the presence of the variables x and y. Using a function, if only as a temporary crutch, helps you understand what's going on and why. Then you can merge the function into main and strictly adhere to the requirements of the assignment.

Use some creativity, man. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

It's evaluated as ((3 + (2 * 3)) + 2), not ((3 + 2) * (3 + 2)). If you don't wrap your macro arguments in parentheses, you may not get the evaluation order you wanted.

Narue 5,707 Bad Cop Team Colleague

>Our faculty has policy of asking questions in assignment which are not been taught in class.
You'd have to have learned it somewhere or they wouldn't ask the question. Teachers don't require students to magically know things.

Narue 5,707 Bad Cop Team Colleague

For various reasons, strings don't work like you expect in C. It's really best to simply see strings as arrays with a special value at the end, and treat them accordingly. For comparison, the strcmp function in <string.h> is what you would typically use to compare two strings:

if (strcmp(x, "test") == 0)
  printf("Great...\n");
else
  printf("What's wrong?\n");

Of course, you could also do it yourself with a loop, but that's silly when the standard library does it for you.

Narue 5,707 Bad Cop Team Colleague

>So there must be some difference.
If there must be a difference, why does the question offer "no" as one of the possible answers? Anyway, if you weren't paying attention in class, you're SOL. So just say "no" as an answer and if you get it wrong, you'll have learned two things:

  1. Pay more attention to what you're taught because it generally comes back to haunt you.
  2. What your teacher thinks the difference is between a "sorted" and "ordered" in a linked list.
Narue 5,707 Bad Cop Team Colleague

>DOES PERFORMING DELETE OPERATION ON A DANGLING POINTER LEAD TO INSTABILITIES?
When you delete an indeterminate pointer, demons fly out of your nose and kill a kitten.

Narue 5,707 Bad Cop Team Colleague

It'll help if you pass the address of the two objects to a function:

void swap(int *a, int *b)
{
  // Swap the objects pointed to by a and b
}

int main()
{
  int x = 10;
  int y = 20;

  swap(&x, &y);
}

This way you won't be confused by the presence of the original variables themselves.

Narue 5,707 Bad Cop Team Colleague

While people can use terminology however they like, I've never seen "sorted linked list" and "ordered linked list" mean two different things. They've always been interchangeable.

Narue 5,707 Bad Cop Team Colleague

Seeing as how it just started, fine. Though I have heaps of work to do and approaching deadlines, so I'm anticipating a stressful day of work followed by an equally stressful weekend of work.

Narue 5,707 Bad Cop Team Colleague

A loop invariant is an assertion that doesn't change between the beginning of the loop and each iteration. In your code, one such assertion is that for any a[x] where x < i, target does not exist. This invariant is true prior to executing the loop because x would be less than 0, which is outside of the bounds of the array. The invariant is true during execution of the loop because you 1) use found as a flag to break the loop when a == target, and 2) do not increment i if target is found. Finally, the invariant is true after the loop because of how the loop is terminated. Either i == n and target does not exist in the entire array, or a is equal to target and target does not exist prior to a (otherwise the loop would have terminated sooner).

int i = 0;
bool found = false;
// Invariant (TRUE): -1 is an invalid index
while (i < n && !found)
{
  // Invariant (TRUE): The loop breaks without incrementing i when target is found
  if (a[i] == target)
    found = true;
  else 
    i = i + 1;
}
// Invariant (TRUE): i == n OR a[i] == target

Pre and post-conditions are not necessarily invariants, but they can be.

timb89 commented: Great Help! +3
Narue 5,707 Bad Cop Team Colleague

>The errors are that IdNode has no member named 'end', 'begin' and 'push_front'. Help guys.
The errors are correct. IdNode indeed has no member named end, begin, or push_front. Your list class does though, so you probably wanted idList to be an object of the list class rather than the IdNode class.

Narue 5,707 Bad Cop Team Colleague

>Does that mean the stl guarantee that they use quicksort for std::sort?
No, though you'll find that introsort (a variation of quicksort) is the preferred implementation.

Narue 5,707 Bad Cop Team Colleague

>Is there any way to inline just some selective calls to a particular function not all of them?
Manually inline it. Of course, that defeats the purpose of a function, so I wouldn't generally recommend it.

>how much authority for making a function inline or not is there for the programmer under C++ standards
Zero. You can either define a member function in the class definition, or qualify it as inline explicitly. But both of those are nothing more than hints; the compiler is not obligated to actually make the function inline.

Narue 5,707 Bad Cop Team Colleague

Perhaps if you split up the file reading code and the tree insertion code, you'll see the problem. I really do suggest you read the tutorial I linked to, because you clearly don't understand the logic of inserting into a binary search tree. There's a loop involved there too. Presently the only loop you're using is reading items from the file.

Narue 5,707 Bad Cop Team Colleague

You're essentially using a recursive algorithm without the recursion. Might I suggest this tutorial?

timb89 commented: Helpful and added link which aided in correct answer! +3