deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is that really true?

The powerpoint says that the number m should always be odd, not that it must be. Hopefully the course that the powerpoint supplements would cover the reasoning for that. I assume it's due to simplified splitting and merging algorithms.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not enough information. The only thing that might be a problem in the code you posted is &theArray[10] potentially being an out of bounds index.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Child implicitly calls the default constructor for Parent, but Parent doesn't have a default constructor because you explicitly defined a single argument constructor. The quickest solution would be to modify your current constructor to have a default argument such that it also acts as a default constructor:

Parent(double n = 0)                                      
{
    num = n;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

http://en.wikipedia.org/wiki/Non-breaking_space

A little digging suggests something like variable.replace(/\xc2\xa0/g, '') as a solution with the way I assume you're handling encodings. You may need to tweak the code a bit as I'm not super hip with Javascript.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is the include directory where Car.h lives in your search paths? What's the directory structure of your project?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But when i compile and run it, it gives me errors.

What are the errors? Seriously, would you go to an auto mechanic and say "something's wrong" and expect them to find the problem you're thinking of?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You have a number of problems in the code. Please be patient for a moment while I list them out.

'#include<conio.h>'

Use of this library should be avoided except in cases where it really does offer the best solution (which is extremely rarely).

printf("How long is the string:");

This prompt is not guaranteed to be displayed before scanf() blocks for input, so the user might be greeted by nothing more than a blinking cursor and no hint of what to do. The reason for that is C only guarantees three times when the output buffer gets flushed to the destination device (the screen in this case):

  1. When the buffer is full.
  2. When a newline character is sent.
  3. When fflush() is called on the output stream.

In the case of #1 you have no control over it because the size of the buffer and its current contents are unknown. #2 isn't ideal for prompts because then you'd have a prompt on one line and the blinkie cursor on the next line; it's kind of ugly. So the only option for prompts is using fflush:

printf("How long is the string: ");
fflush(stdout);

I'm flushing stdout because that's the stream object that printf writes to.

scanf("%s",&num);

num is not a string, you need to use the %d specifier. Also, it's critically important to check user input for failure. scanf() returns the number of successful conversions, which in general if that number corresponds to the …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That's much more helpful. Now it's obvious that you're not properly qualifying the namespace that XMLElement lives in:

tinyxml2::XMLElement* GetElement(std::string name);

And you still need to include <string> for std::string.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Well, sadly i't ain't just a wrong header.

Well then you'll need to be more specific. What compiler, what OS, give us your exact code in a compact form so that we can reconstruct your project. Because from what you posted, the only answer is "include the correct header".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Even when i do include the header it still errors.

Then it's the wrong header.

Oh, and there is no error about an unrecognized type ;-)

Clearly you and I are reading different errors, because the one you posted says exactly that. "expected constructor, destructor, or type conversion" means a recognized type is expected but wasn't provided.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need to include a header with the declaration of XMLElement (not to mention <string> for the std::string class). Just because the library is linked doesn't mean the compiler won't complain about unrecognized types.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can undo it, no worries. ;)

pritaeas commented: Figured it out after posting. +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I don't really understand, why would the sorted array be advantageous?

Take a hash table, regardless of implementation, and print out all of the items in sorted order. You'll discover that it's not as simple as just traversing the table, and that's the first advanage that comes to mind for a sorted array, where it is as simple as just traversing the array.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Realistically, a sorted array would be advantageous if you want to get a sorted listing of items, or do a ranged search or anything except an exact match. If all you're doing is searching onesie twosie, the hash table is conceptually faster. In practice the array and a binary search may be faster due to caching though.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how do I keep on comparing it til the stack is null or the precedence don't hold true anymore?

Use a loop. ;) The algorithm should look something like this:

for each character
    if whitespace
        next
    else if not an operator
        append the current character to postfix
    else
        until the stack is empty or the top value has greater or equal precedence
            append the top item
        push the current character to the stack

until the stack is empty
    append the top item

Which when converted to C++ might look like this:

std::string InfixToPostfix(const std::string& infix)
{
    std::stack<char> st;
    std::string result;

    for (char ch : infix) {
        if (isspace(ch))
            continue;
        else if (!IsOperator(ch))
            result.push_back(ch);
        else {
            while (!st.empty() && ComparePrecedence(st.top(), ch) >= 0) {
                result.push_back(st.top());
                st.pop();
            }

            st.push(ch);
        }
    }

    while (!st.empty()) {
        result.push_back(st.top());
        st.pop();
    }

    return result;
}

This is all without handling parentheses, of course, but your code didn't either.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Love it! Such a simple thing, but seeing who solved it and when is handy info.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Anytime you get a large negative number, there are two things to check straight away:

  1. Any uninitialized variables in your math.
  2. Any out of bounds indexing or invalid pointers (they can corrupt other variables).

Either way the problem isn't in rand(), it's your code doing something funky.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You're using the %s specifier, which is whitespace delimited. It won't stop reading when it sees a comma. Try this:

#include <stdio.h>

int main(void)
{
    char buf[BUFSIZ];

    while (scanf("%[^,\n]%*c", buf) == 1)
        printf("'%s'\n", buf);

    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please post a small program that exhibits the problem and a sample of your CSV file. scanf() format strings can be tricky, and scanf() is usually unsuitable for true CSV, but if it's just a naive comma separated format without embedded commas, you can get away with using scanf().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Recursion is very inefficient for the computer, but in terms of programmer time it is efficient.

It depends. Pretty much any compiler these days will perform tail recursion optimization such that if you only recurse once and it's the last thing you do, the recursion will be converted to iteration internally. So this would likely be just as efficient as a non-recursive implementation because it uses tail recursion:

int factorial(int n)
{
    if (n <= 1)
        return 1;

    return n * factorial(n - 1);
}

For more complex recursive algorithms that cannot fully use that optimization, the overhead of stack frames may not be as significant as your teacher is implying. When recursion is used properly (binary search is a good example), the depth of recursive calls is kept to a minimum. The overhead is also far less significant than it used to be, such that recursion is often efficient enough except for the most persnickety of realtime applications.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

From what I understand is Java is mostly understanding, implementing, and manipulating algorithms.

That's universal, it's not unique to Java.

To me this seems like I just used a simple algorithm to obtain the desired result.

In my opinion, a good professional would start with the simplest algorithm that meets requirements and only make it more complex as necessary. While it might seem inefficient, for example, I'll often use a very simple linear search on collections that are unlikely to be large enough to warrant something "faster".

So in your professional opinion does this look professional and efficient?

Well, it's poorly formatted, so that makes it unprofessional. It's not inefficient, but it is one of the worst possible hashing methods[1], so I'd say it's an unprofessional function because it fails to meet the requirements of your typical hash function: fast and minimal collisions.

[1] See additive hashing on this website.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As far as what the requirements are: a good candidate is an expert in their field

That's not a requirement, though active members who meet the other requirements have a very strong tendency to also be experts who give excellent help rather than receive it. As an example, we were far more likely to single out someone like mike_2000_17 as a potential candidate because he is an expert. He posts very often with confidence and because of that was able to inadvertently show off traits that we look for in moderators.

It is an invite-only thing :)

Kinda sorta. I have it on good authority that Narue originally requested to become a moderator. The usual debates amongst the team ensued, but there wasn't an initial recommendation; Narue started the process by asking, and it turned out well. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, but this only tells me how to build it and install the result.

Wrong, read the whole page. Specifically the part under the heading "Include Boost headers and link with Boost libraries" that I kindly but it seems pointlessly jumped to directly with the link.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When creating a new article you can select both the type and the forum. So if you're posting from the C++ forum, the selected forum for the new article will be C++, but you can change it using the selection list before posting. I imagine that as a more general tutorial you'll want to post directly to the Software Development category.

Likewise, the "section" that the article goes to is defined by the article type. To post a tutorial, change the type from Discussion Thread to Tutorial.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but the second method is not working it is printing the last entered number infinitely..

My bad, I forgot to include the actual input request:

while (infile2.good()) {
    infile2 >> n;

    if (!infile2.good())
        continue;

    cout << '\n' << n;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I used eof() and good() function but there is no change in the output.

They're both wrong, so I'm not surprised. The problem is that both eof() and good() will only switch state after you've tried to read from the file and that attempt fails. So you need some way to check the state after reading but before processing what was read, otherwise you'll accidentally process the previously read item again.

Convention is this:

while (infile2 >> n)
    cout << '\n' << n;

The extraction operator will return a reference to the stream object, which when used in boolean context will give you the current state. Alternatively you can still use the eof() or good() option, but a conditional inside the loop is needed:

while (infile2.good()) {
    if (!infile2.good())
        continue;

    cout << '\n' << n;
}

That's redundant, verbose, and logically confusing, so I don't recommend it.

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

What compiler (or I suspect IDE) are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I gave you suggestions, did you apply them? The class you posted doesn't add any useful information.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your base class is undefined. Check your spelling, then check to make sure that you're including the appropriate files and have appropriate declarations in place. Your question lacks necessary information to help any further.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Everything you get from the keyboard is a character. To extract an integer you need to read multiple characters and append them to an integer value, or store them as a string and then subsequently convert that string to an integer value. Either way you're doing a string to integer conversion.

Since you're using printf, why not also use scanf? That way scanf will do all of the hard work in converting:

extern  scanf
extern  printf

global  main

section .data

; Don't forget terminating null characters to match C's expectation
ifmt: db "%d", 10, 0
ofmt: db "The number is %d.", 10, 0

section .bss

input:  resd    1       ; resd reserves N dwords, not 1 dword if N length

section.text

main:
    push    dword[input]    ; Variables can be added directly in NASM
    push    ifmt
    call    scanf
    add esp, 8

    push    dword[input]
    push    ofmt
    call    printf
    add esp, 8

    mov eax, 0      ; Return 0 for consistency with C programs
    ret
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have got an integer as 0010000001.

Just so you're aware, leading zeros are typically truncated as they don't contribute to the value and are conceptually infinite. So your number would be 10000001 unless otherwise formatted to ten digits with leading zeros. That doesn't affect your algorithm, but it's something to keep in mind.

Solution: i tried using num/10000 but i dont think this is feasible solution.

Why don't you think it's a feasible solution?

anukavi commented: aware abt the truncation of d leading zeros. In case if the number is 1234567890 and xpected result:123456.Division by 10000 is easier solution and just wanted to know if thr r any other way to get the first 6digits. +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Well, the specific syntax you posted is a GCC extension. but C99 does support variadic macros with more or less the same syntax as variadic functions. The big difference is how the argument list is retrieved (__VA_ARGS__ for C99 and args for GNU).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I can't walk you through it because what you're doing isn't meaningful. size is an instance member, which means you must have created an object somewhere and given size a value that warrants outputting it.

I don't think you should be printing size from printStatic() at all, so my suggestion would be to remove that line entirely. It's already being printed from print(), so what's the point?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

No, B is a class, not an object.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You try to print size from a static member function, but size isn't a static member. To access non-static members, you need an object, because that's where memory for non-static members is allocated.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Have you tried searching the forum? We regularly get people trying to install a Model T engine (Turbo C) in their 2012 Ferrari (modern Windows OS).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I guess my question should be "Why is it undefined?".

I'll raise your question with a counter question: what's stopping an implementation from using completely independent allocation pools and mechanisms for new/delete versus new[]/delete[]?

++K;
delete[] K; //Does that delete all instances of F?

No, that should crash because you're trying to delete a pointer that's different from the one given to you by new. If you didn't increment K then it would correctly free the memory, and you'd need to take into consideration that F and K both alias the freed memory.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why is it safe to use delete[] without getting a crash or something?

You consider undefined behavior "safe"?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And why would you rely on a simple C function in a managed language?

Because this thread was originally posted in the C forum and gerard probably didn't notice that the code was C#.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thats just how my instructor wants it.

You're misunderstanding the question. Your teacher wants you to use pointer arithmetic when accessing the "array" rather than subscripting with an index. For example:

smallest = *(f + i);

Is there any problem with my program

Well, your algorithm is completely wrong for what it's trying to do. I suspect you just jumped in and started writing code without working out all of the potential case examples in a small set on paper. If you don't understand the problem or how to solve it methodically, you can't write a program to do it for you. Here are the cases I'd use. First, the average case permutations:

  1. 1 2 3
  2. 1 3 2
  3. 2 1 3
  4. 2 3 1
  5. 3 1 2
  6. 3 2 1

Then obvious degenerate cases (such as an empty array or no second smallest number):

  1. <empty array>
  2. 1
  3. 1 1
  4. 1 1 1

Once you can lay out detailed steps to accomplish the goal in each of those cases on paper, you can start writing code to do it.

or is it just pointer in 'C' that have some problem??

Pointers in C have no problem, you're just using them incorrectly. Don't blame the language for your poor use of it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It has nothing to do with the "array" and everything to do with the wacky use of pointers for smallest and secondSmall. Just make those regular float variables, there's no need for pointers there.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is it generally a better practice to define all the functions you're going to use before you define main() ?

It depends on your prefered style, neither is better than the other. However, for larger projects you'll end up moving the declarations to a header anyway, and the definitions to an implementation file. So the choice of defining before or after main() only applies when you're not modularizing those functions.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What does this error mean?

It means you must declare something before you use it. Specifically, getMatrix() and displayMatrix() are defined (and declared) after main(), but you use them in main(). Add a prototype to declare each:

void getMatrix(int number);
void displayMatrix(int number);

int main(void)
{

More fundamentally, is my code inherently bad? As in, is it a poor way to achieve what I wanted to achieve?

Inherently? No, but it could be improved. However, those are nitpicks that are best left for later.

Why is coding so hard? :(

Programming is difficult. Welcome to reality.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

assert( ! inStream.fail() ); // make sure file open was OK

And if the file wasn't opened okay you'll hard stop the process with a cryptic error? Sounds like the pinnacle of user-friendliness. :rolleyes: assert() is intended to help the programmer debug impossible errors at development time; it's not meant to handle likely runtime errors that are out of the programmer's control (ie. user input, file existence, etc...).

I'm stuck on the second part of my program. Everytime I run this code I get "ConsoleApplication4.exe has stopped working".

Your process hard stopped with a cryptic error... It's a curse being right all of the time. ;) I have a better idea, check for a successfully open file with an if statement and print a useful diagnostic if it fails:

inStream.open( "dictionary.txt");

if (!inStream) {
    perror("Error opening dictionary.txt");
    return 0;
}

perror() will likely give you a more useful error message as to why the file couldn't be opened, but you'll need to include <cstdio> for its declaration.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Are these all parts of the draw methods?

http://msdn.microsoft.com/en-us/library/e06tc8a5.aspx

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

and if I try to read columns with this code snippet:

I notice that you're reading the value for the rows and columns into a and b, respectively. But you try to print rows and columns. Since you used the address-of operator for a and b, I can make a reasonable guess that they're not pointers referencing rows and columns. That's your immediate problem, but fixing it still won't produce the correct output (see below).

I also notice that you didn't pay close attention to the code example I gave you. %[^:] is the specifier, the scanset is not a modifier for the %s specifier. Your format string has %[^:]s, which means scanf() will look for a literal 's' in the stream, then any amount of whitespace, then a colon. There's no way your format string will work, because it depends on two mutually exclusive values for the next character. The scanset stops on a colon without reading it, but the next expected character is the literal 's'; it can't be both a colon and an 's'.

The following is correct, read it very carefully:

fscanf(infilep, "%[^:] : %d\n", storage,  &a);
printf("temp is %s, %d\n", storage, a);
fscanf(infilep, "%[^:] : %d\n", storage,  &b);
printf("temp is %s, %d\n", storage, b);

Remove the space just before the colon in the fscanf() format specifier string. Not sure it will fix it but worth a try.

It won't have any effect. Literal whitespace in a format string just …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I was wondering is it at all possible to programatically select an area of an image to display?

Of course. Though how you do so depends on what kind of processing you're doing. Sometimes a thumbnail will work, other times you can simply crop, still others might have you zoom in to a specific area (which may or may not be detected programmatically).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

My son said to ask if you've heard of (or seen) Wolfen Spice.

Yes indeed, assuming he means Spice & Wolf. Both seasons are excellent, but kind of heady if you try to follow the merchant explanations. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Some early thoughts...

Tonari no Kaibutsu-kun: Exactly as expected, so far so good.

Suki-tte Ii na yo: The art seems off for the female protagonist, especially the early range of facial expressions that belie her initial personality. I'm also not a fan of the selected voice actor's portrayal at this point. It may grow on me, but I saw her more as the more deadpan and cold portrayal shown by the female protagonist of Tonari no Kaibutsu-kun. It's kind of a shame because out of the two this one is the one I was most keen to see, but it's still early.