deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It may seem clever but I find it condescending ("Don't worry about how it's done. It's magic - whoo") and unprofessional.

As for your reasoning, I don't completely agree:

  • Condescending: That's a problem with your sensibilities, not the word choice. Changing it to "automatically" doesn't change the fact that you're glossing over how something works because you don't think it's important that the listener need to know.

  • Unprofessional: Slang in general can be unprofessional, depending on the audience. However, understanding slang is a different matter. On a website you don't control who reads the content, so there could certainly be people from cultures where the slang is either completely unknown or offensive. For that one needs to be careful.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

fgets() includes the newline that terminated input for a successful call. If it's there you need to remove it:

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

char *fgets_trim(char *s, int n, FILE *in)
{
    char *p = fgets(s, n, in);

    if (p) {
        s[strcspn(s, "\n")] = '\0';
    }

    return p;
}

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

    if (fgets_trim(s, sizeof s, stdin)) {
        printf("'%s'\n", s);
    }

    return 0;
}

However, note that the newline is important information. If it's not present after fgets() returns then that means you may have read a partial line. If you continue to push through with your normal logic then you may get garbage results. So take care in just automatically removing the newline without considering other error handling strategies.

A more thorough approach might be with a dynamically allocated string that grows to meet input needs:

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

char *dgets_trim(FILE *in)
{
    char *p = NULL, *temp;
    size_t size = 0, end = 0;
    char buf[BUFSIZ];

    while (fgets(buf, sizeof buf, in)) {
        size += strlen(buf);
        temp = (char*)realloc(p, size + 1);

        if (!temp) {
            break;
        }

        p = temp;
        strcpy(p + end, buf);
        end = size;

        if (p[end - 1] == '\n') {
            p[end - 1] = '\0';
            break;
        }
    }

    return p;
}

int main(void)
{
    char *s = dgets_trim(stdin);

    if (s) {
        printf("'%s'\n", s);
        free(s);
    }

    return 0;
}

But ultimately your problem is the trailing newline after each fgets() call. Remove that and your output will look …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Both...not at the same time though. I'm a tea snob, but I also love a nice cup of joe.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Ok lets make this more hypothetical, if you completely redesigned a computer and re-wrote the entire operating system could you have a computer that doesn't need ram. From my understanding ram is the middle man, you transfer data from your hard drive to your ram and then to your cpu and then your cpu transfers data to your ram and then your hard drive, so couldn't you just remove the middle man. Could you potensially make a computer that doesn't run off of ram.

Hypothetically, yes. But such a computer would either be extremely limited without some alternative strategy to RAM, or extremely slow, given the current alternative to insufficient RAM: virtual memory.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how do i keep track of the names entered ?

They're in an array...

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

thanx bro but i have to create a dynamic array not static array

How the array is defined was the least of your problems. Please learn from my example, and feel free to modify it to suit your needs.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There are a number of problems with your code. Most notable are the sort doesn't actually sort, you're not actually using an array, and inputFromUser() is storing data in a local variable that gets leaked away on every iteration of the loop. Worse, you're in SortArray() assuming that this not array has 10 items and it results in stomping all over memory you don't own.

Compare and contrast:

#include <iostream>
#include <string>

using namespace std;

struct student {
    string first_name, last_name;
};

int load_students(student a[], int n);
void show_students(student a[], int n);
void sort_students(student a[], int n);

int main()
{
    student a[10];
    int n = load_students(a, 10);

    sort_students(a, n);
    show_students(a, n);
}

int load_students(student a[], int n)
{
    int i;

    for (i = 0; i < n; i++) {
        cout << "Enter student #" << i + 1 << "'s first and last name (EOF to quit): ";

        if (!(cin >> a[i].first_name) || !(cin >> a[i].last_name)) {
            break;
        }
    }

    return i;
}

void show_students(student a[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << i + 1 << ": " << a[i].first_name << ' ' << a[i].last_name << '\n';
    }
}

void sort_students(student a[], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = n - 1; j > 0; j--) {
            if (a[j].last_name < a[j - 1].last_name) {
                student temp = a[j];
                a[j] = a[j - 1];
                a[j - 1] = temp;
            }
        } …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

void permit a function without return type....

void is the return type. You just can't do much with it.

Just to add, it is not considered good programming to write void functions

That's not true at all. Please don't conflate your own personal preferences with generally accepted good practice. Status codes are certainly a good thing (assuming you've rejected other error handling strategies), but only when their use is appropriate. Simply returning a status code for the sake of not returning void is the opposite of a good practice.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This blog is created for the students of computer field and sharing the programming concept to the people of IT.

That's a shame. In a perfect world, people would learn before trying to teach. Kudos for going to the effort, though.

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

says that there are many errors. :(

Like a lot of the PHP I've seen. That book must be really popular. :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

May I ask why you need to convert that code to C? Seems suspicious.

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

Ask him to draw it for you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

was it 3 hours before the big update you did a while back or was i just plain wrong?

Nope. If memory serves me it may have once been 15 minutes, but was never more than 30.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You clearly didn't read the reference links I so kindly gave you earlier. Here it is again. AH tells the interrupt what to do, and DL is the argument.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Flat html isn't to bad in my opinion.

Obviously I don't know anything about your experience, but this statement suggests you've only worked with what I'd consider to be "small" web pages. Any small program isn't too bad, but it gets out of hand almost immediately.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Sometimes it takes several seconds to fully submit the post and refresh the thread. How long did you wait before concluding that the post was not submitted?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

See some interesting code here.

When experts in a language try to write impenetrable code. ;) IOCCC entries are actually beautiful in a perverse way because it takes a great deal of skill and effort to obfuscate what are normally straightforward programs into something that requires expert-level abilities to figure out.

Compare this with plain bad code, which is ugly because the lack of skill and effort is apparent at a glance.

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

Check out fseek() to smartly maneuver around a file.

Note that arbitrary seeking on a stream opened in text mode is technically undefined. To use anything except an offset of 0 or the result of a previous ftell() call, the stream must be opened in binary mode.

Also, how does ungetc put back a character?? Explain.

If you're feeling adventurous enough to look at an actual implementation (it's written with clarity in mind), check out these links:

FILE structure
ungetc
fgetc

You'll see that a separate unget buffer is used to hold pushed back characters, where the buffer takes priority over the primary character buffer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does the function fgets() increment the file pointer internally to point to the next line??

Yes, all of the standard I/O functions adjust the file position accordingly.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does anyone have an idea what the worst habbit they have seen is?

Zero indentation, no contest.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Also, there is a big difference between winning a case in court for damages / trauma (which requires the establishment of a civil tort), and something being illegal (i.e., a crime)

Apologies, I meant the former and said the latter on the (apparently faulty) assumption that my meaning would be understood.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Which post? If you mean the OP, then that would delete the thread and be unfair to anyone who replied. It's also doesn't break any rules, so I can't justify deleting it anyway.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Illegal? Really? It's a stupid context to choose for the question, but what's illegal about it?

If the interviewee chose to be offended and filed a lawsuit, I can all but guarantee that they'd win on grounds of sexual harrassment and/or discrimination.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just to be clear, are you using Turbo C++ because you're forced to by a teacher or employer, or because you've chosen to use it? Because if it's the latter, I'd strongly recommend installing a compiler that isn't over 20 years old.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Get Ellis and Stroustrup's ARM

Pulication date: 1990. Not exactly a modern C++ reference.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

like why they named it as main only and not other???

That's an easy one. C inherited the name of the starting point from B.

and why we have to write main() compulsory in prgoram

A program must have a starting point. In assembly this constitutes an address offset that denotes where instructions will begin executing. In some higher level languages execution simply starts at the top of the file. Other languages like C don't allow execution statements at the top lexical level, so they define the starting point as a function with a known name and signature that the compiler or interpreter can call directly.

Ultimately, it's all about answering the question "how do I start this program?"

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You might want to get all of the details for the problem then. Unless he sent you an exact copy paste from the problem, you're getting a paraphrased version that may be leaving out very important information.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I've been using Windows 8 exclusively at home for a few months now and haven't experienced any problems in IE, Chrome, or Firefox. The problem you're describing sounds a lot like a zoom issue. Reset your zoom (usually with something like ctrl+0) and see if that fixes the "awkward".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Clearly not an interview question as such a question would be illegal (in both your country and mine).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your loop is incorrectly placed. It should wrap all of the prompts, input request, computation, and output for each temperature:

Set Temperature = 0
While Temperature != -999
    Write “enter a temperature number or press -999 to quit”
    Input Temperature
    Set F = 9 * Temperature / 5 + 32
    Write “The Temparture in Fahrenheit is”, F
End While
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but some fool is looking for help to run the code shown

Thanks for notifying us of a thread that fell through the cracks.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In for other replies. I've been using google and the standard for so long that I'm no longer familiar with the latest and greatest books.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But at over 1100 pages I'm not sure it's a quick reference.

C++ isn't a small language. Any reasonably complete reference, even a quick reference, will be fairly long compared to many other language references.

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

but there is no way to assign a function to a specific option.

That's not true. I simply chose not to complicate the example by making each selection its own function since it appeared that your issue was menu item selection, not menu item execution.

As a further example, consider that if you have the row of the item you want to execute, and an array of function pointers where each index matches the row you want, you can modify execute_selection() like this (everything else remains the same):

void add() { std::cout << "add()" << std::endl; }
void remove() { std::cout << "remove()" << std::endl; }
void display() { std::cout << "display()" << std::endl; }

typedef void (*action_t)();

action_t selection_actions[] = {
    nullptr,
    &add,
    &remove,
    &display
};

void execute_selection(int selection)
{
    selection_actions[selection]();
}

That's a quick and dirty example to show that it's possible to use the selected row as a key of some sort for calling a function. With a little effort you can no doubt come up with something better suited to your specific needs. Note that my example wasn't meant to be a complete solution to your problem, just something to get you started in the right direction.

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

So the 2* x + 4 is the value that is returned to the function (fx), right?

Yes.

The int does it stand for integer as in the numbers 1,-1,2,-2,2,-3,...?

Yes.

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

Inline assembly is very compiler dependent. Are you using the same compiler to build this program as your teacher used to write it?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If the stream is at the end of the file, fgetc() will always return EOF regardless of how many times you call it. In other words, unless you have a breaking condition that checks for EOF or ensure that end-of-file won't be reached in the loop, it will be infinite.

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

Avoid using system calls in your program.

Too vague, and Walt's rationale (from the link) is largely moot:

  1. "It's not portable." The system() function is inherently non-portable because its argument is dependent on available system console commands. Though it's wise to acknowledge this when choosing to use it, I wouldn't claim portability as a reason to avoid something unless there's an equally good and portable solution. Walt's stated solution is the portable way to simulate the behavior of system("PAUSE"), but it's not equally good because you have to consider extraneous characters in the input stream.

  2. "It's a very expensive and resource heavy function call." That's easily the dumbest reason for avoiding system("PAUSE") if you take half a second to think about what system("PAUSE") does. As an argument against system() in general it's appropriate, but not for system("PAUSE") specifically.

  3. "You must include a header you probably don't need: stdlib.h or cstdlib" Oh no, you must include a standard header that contains commonly used functions and types. The horror! ;) Who is Walt to say that the header isn't needed for other things? Further, if you choose to use the system() function then that header is needed anyway, so it's not really an argument against system(), it's just fluff to make the short list longer.

I'd say avoid using the system() function in favor of a system call that you know won't execute a malicious program of the same name. Walt doesn't address this at all, but it's by …

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 replied to your other thread. Did you take my advice?

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

Hi, someone challenged me, that the execution of this program should not take more then .3 seconds.

Okay, so what is the purpose of this program, and who is this person to tell you how long it should be taking? Sounds like someone talking out of their ass to be honest.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Turn anything that looks like this:

while(ch!='\n')

Into this:

while(ch != EOF && ch!='\n')