deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You have a cin >> yearsLived between the two getline() calls, which means the second call succeeds immediately on the newline left in the stream by the intervening formatted input. It's basically the problem described here.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'll give you a hint: check your array indices where the element is being set to 0. You're overrunning one of your arrays and count happens to be at that address.

nitishok commented: thanks. it works with a return; added in if(i==8) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

My friend did the same program in less than half as many lines.

The number of lines aren't indicative of the quality of a program. I'm not saying there's anything wrong with your friend's program, but just because it's shorter doesn't mean it's better.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have a better idea. How about you cut out all of the extraneous crap from your program such that only the absolute bare bones code is needed to illustrate the problem you're having and the question you want answered. Then post the code directly.

Since you want to attach a file, I'm guessing it's relatively huge and nobody wants to read all of it. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

All function parameters are passed by value in C++ (the argument could be made that references are passed by value too), which means a copy of the value is made and put into a new local variable (the function parameter) for use inside the function body. By passing a reference, you're essentially aliasing the original object and thus any changes made to the parameter will be reflected in the object that you passed as an argument.

Pass by value.
Pass by reference.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

when the limits seem to get reset to 1 and 100 after they get sent back to int main()

That's because you're working with a copy of the variables from main(), not the original objects. Try passing references instead:

int setLimits(int computerGuess, int& lowLimit, int& highLimit)
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why are || and && sequence points? Rhetorical, no need to explain - I can see why they could be just not why they should be...

Actually, that's a good question because it's not immediately obvious.

I'm sure you guessed that it's because of the short circuiting behavior. The operands are expected to be executed in a specific order, and a sequence point is required to enforce that order. If there weren't a sequence point for &&, for example, then the right hand expression couldn't expect the side effects of the left hand expression to have completed.

Is enforcing full completion of the expression necessary to make short circuiting work in all cases? To be honest, I'm having trouble thinking of an example that's practical in the real world, but that doesn't mean there aren't any.;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is a classic example of binary searching. The appropriate solution for your computer is to guess at half the range every time. So the guess is always half way between the previous guess and the end of the range. When the guess is lower, set the bottom of the range to one plus the guess, and when the guess is higher, set the top of the range to the guess.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm not entirely sure what you're asking, but it sounds like you're questioning how malloc() works. In fact, it requests free memory from the operating system. Assuming the OS grants that request, malloc() now owns the memory and can do what it wants. Likewise, if malloc() succeeds then your program also owns the number of bytes you requested (which is almost assuredly less than what malloc() requested from the OS), starting at the pointer that you're given.

So the trick here is that malloc() doesn't work with memory that's assigned to your process, it works with memory specifically allocated using an appropriate system call. As a simple and pure example of deferring work to the OS, take a look at the malloc() implementation here and the underlying _sys_heapalloc() function here. Sorry, but you'll have to search as I didn't break the files down into a file per function.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can someone help me with simple code? I made software and I want to add option that can change languages.

Yeah, that's not simple. Internationalization and localization are rather difficult, especially if you want to support widely variant languages like English and Arabic.

Give this resource a look through and see if it helps.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

How do you become a good tennis player? By studying good tennis players, doing endless drills, and playing lots of tennis at a high enough level to give you a challenge. Programming is no different, though in my experience tennis drills don't really help the coding skills.

Do you have a more specific question on how to start learning a programming language? I get the vibe that while my tennis analogy is accurate, it's also not quite what you were looking for.

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

For now, my lecturer gave me an idea to improve existing OCR by creating some machine learning languages to improve the accuracy of OCR tools. This however is on dual mode where both 'research/development' is involved.

Were you under the impression that computer science research didn't involve some development? If all you want to do is sit in a chair and think, or write out a few mathematical proofs, or search existing research, that limits your options quite drastically.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Does the former mean incrementing the address of the pointer ?

When pointers are involved there are four distinct values:

  1. The address of the pointer object itself.
  2. The value of the pointer, which happens to be an address.
  3. The address of the pointed to object.
  4. The value of the pointed to object.

Let's say you have this:

int i = 123;
int *p = &i;

The value of the pointed to object is 123 and the value of the pointer is the address of i. You can say &p to get the address of p, which you'll take care to note is not the same thing as the value of p; they're two distinct addresses referring to two different objects.

With that said, when you say p++ you're incrementing the value of p. It's still incrementing an address, but it's important to make the distinction between the address of a pointer and the address stored by a pointer.

The whole *p++ vs. *++p vs. ++*p dealie is a matter of operator associativity (or precedence in the case of postfix increment). Postfix increment has higher precedence than indirection, so *p++ is equivalent to *(p++). Prefix increment and indirection have the same precedence, but their associativity is right to left, so the one further to the right will occur first. Therefore *++p will increment the value of p first, then dereference the new addres while ++*p will dereference p first and as a result increment the value …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Oops. Sorry, my mistake. It isn't undefined.

That's why I forgot that it's undefined behavior. Because it isn't. ;)

How is it undefined.

As Walt said, it's not. At first glance one might make that mistake though, because there's a rule about modifying a single object more than once between sequence points being undefined. However, the || and && operators introduce a sequence point, so the statement is well defined. I myself gave it a double take to make sure everything was kosher before answering. ;)

thanks to JAMES SIR in advance.

...

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Could you please advise if I am doing some thing wrong here?

You're doing something wrong by including file1.c in file2.c. Note that Narue was talking about visibility in the translation unit, not source files. A translation unit is the resulting intermediate file after the preprocessor has run, which means that by including file1.c, file1.c and file2.c combined are a single translation unit. Thus all functions are properly defined regardless of their visibility.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is a parsing problem, not a simple tokenizing problem. Do you have more details as to the format of the instructions? It looks like you're attempting to write a program that reads assembly-like instructions and break them down into component parts. For that I wouldn't use strtok() as the only solution.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That would be a good start.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You should be using floating point variables if you need precision below 0, otherwise all of your weights are going to be 0.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

WaltP and deceptikon are refusing to help me.

If you actually accepted help instead of rejected every suggestion, we'd be more inclined to make a serious attempt at helping you. But since you stubbornly refuse to listen to any advice and act like a know-it-all, we aren't going to bother. Well, at least, I won't. I can't really speak for Walt, but I suspect he feels the same way.

It's a shame too, I could help you improve this program and your C++ abilities drastically. But I know a lost cause when I see one.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm guessing you didn't comprehend the entirety of my post. I addressed that problem by talking about braces and even gave you a snippet of the correct code. Here's a complete working program, since you seem to have trouble cutting and pasting snippets into existing code:

#include "stdio.h"

int main(void)
{
    int iRows;

    printf("Enter a number :");
    fflush(stdout);
    scanf("%d",&iRows);

    if(iRows>0)
    {
        int iResult, iCount;

        for(iResult=0; iResult<=iRows; iResult++) {
            for(iCount=0; iCount<=iResult; iCount++)
                printf("*");

            printf("\n");
        }
    }

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

Give me a clear explanation.

I fail to see what's unclear about the hypocrisy here. You claim to be a beginner, someone who by definition isn't perfect and will make a greater than average number of mistakes that go unnoticed. You also claim that your program has no problems and cannot be improved despite being shown otherwise.

This alone doesn't make you a troll, but refusing to listen to anyone who points out flaws raises red flags.

As I said; I am a beginner who wants to learn C++ better.

Bullshit. You've rejected any attempts to teach you and stubbornly insist that your code is without flaws. With that attitude, the clueful people won't want to help you learn C++ better. I certainly have no incentive to review your code for you because I'm reasonably sure it would accomplish nothing and be a huge waste of my time.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i think my condition for the inner loop is wrong?

Why do you think it's wrong?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I am finding it tuff to locate where exactly you have used this?

I don't use casting in this case, rather I'm using the _real4_t and _real8_t unions. You can search for those type names to see all of the places they're used.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You keep resetting iResult in the inner loop when it shouldn't be modified. This is correct:

int iResult, iCount;

for(iResult=0; iResult<=iRows; iResult++) {
    for(iCount=0; iCount<=iResult; iCount++)
        printf("*");

    printf("\n");
}

Also notice that I added braces around the outer loop. While you can omit the braces if the entire body of the loop consists of only one statement, your outer loop contains two: the inner loop is one statement and the printf() call is a second. Failing to use braces here means that newlines won't be displayed at the correct time and you'll just have one long line of asterisks.

I recommend that beginners always use braces to avoid this kind of error.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i isn't 0, so the first part of the logical AND succeeds. i is then incremented. Next j isn't 0, so the second part of the logical AND succeeds. j is incremented. The OR part of the test isn't performed due to short circuiting and m is set to a true value.

You can change how things work by making either i or j zero initially such that the AND part of the test fails. This will force the OR part of the condition to be executed. Changing i to zero will result in an output of "2 1" because i is now incremented twice.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

can I get simple yes no answers and a small description to both answers?

I can certainly give you simple yes and no answers, but they'd be wrong because it's not that simple. Different variable locations result in different storage areas, and thus different RAM usage.

when we launch the program after compiling, does the program take 3 bytes of RAM space?

At least 3 bytes, yes. Exactly 3 bytes, no.

the more declarations ,the more momory it takes in RAM??

No. Let's say you get a big box and toss a few small things into it, if you want to add more things, do you get a bigger box? No, you just toss the new things into the mostly empty big box. That's how the process stack works.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm starting to think that you're a troll. The utter senselessness of continually claiming that you're a beginner but also rejecting all advice on the grounds that you're perfect suggests an attempt to get a rise out of people.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Assuming that ptr is a pointer to a function, there's no difference. There are only two things you can do with a function: take its address and call it. Because of this limited operation set, the compiler can guess what you want to do from the context. If you're not calling the function then you're taking its address, and if you're not taking its address then you're calling it.

Because of that guessing, all kinds of address-of, indirection, and parentheses operators that are required for object pointers are unnecessary for function pointers.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The first warning tells you that isNULL is a pointer yet you're using it like it's not. This suggests a bug wherein you forgot to dereference the pointer. You're also using strcmp() incorrectly; it returns a comparison result rather than a boolean, where 0 means equality:

if (strcmp(data, "NULL") == 0)
    *isNULL = 1;
else
    *isNULL = 0;

But since you're making a boolean test and also assigning to a boolean variable, you can eschew the if statement and do the assignment directly:

*isNULL = (strcmp(data, "NULL") == 0);

C is cool like that. :)

The next two warnings are telling you that you forgot to include stdlib.h and malloc() has no declaration. When a function has no declaration, C assumes that it's a function taking an unknown number of arguments and returning int.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

My program doesn't have mistakes, because I already solved them. :-D

This is the difference between a beginner and someone with experience. You think you've made no mistakes and your program has no bugs. We know we've made mistakes and are grateful when someone finds a bug.

Drop the ego dude, you're never going to get beyond the beginner stage if you think you're perfect.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but why is it punned to char only ? can't it be used in int only ?

It's punned into a pointer to char. A char is equivalent to a byte, so a pointer to a byte (or an "array" of bytes if you prefer) is the appropriate way to access each individual byte in a multibyte object without using bitwise operations.

secodnly, please give me good links to type punning.

I'd just point you to Wikipedia. In the project I showed you previously, type punning is used here and here to break a floating point value into its integer bit pattern for bitwise operations and component parts for cleaner access. I think that's a good use of punning (obviously, since I wrote it ;)).

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what :O .O.o ????

That's not a meaningful question. I'd like to think that you understood at least some subset of the words I used, so please specify which part of the explanation doesn't make sense and why. I'm happy to elaborate, if I have a clue of what to elaborate on.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The strict definition is probably close to the same for both, but I see type punning as breaking the type system to view component parts of an object. In this case the integer is being punned into an "array" of bytes that make up the integer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Firstly, We haven't discuss these type of things at any time.

Clearly you don't remember, but I do because I spent a lot of time explaining to you how arrays and pointers work. And I'm quite sure that I described the relationship both ways.

Secondly, You have used a hindi word "guru" ;) you are aware of hindi also ?

I'm aware of hindi, but "guru" is common in English too.

so what c[0],c[1],c[2] and so on are representing here ?

The bytes of the integer. Do some research on a concept called type punning. That's what's being done here.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but matrix method is not well explained

Then I'd wager you need to focus on understanding matrix multiplication independent of calculating fibonacci numbers, because that's really all it is. The given algorithm in that link (converted to actual C) is just this:

void fib_r(int n, long mat[2][2])
{
    long f[2][2] = {{1, 1}, {1, 0}};
    long tmp[2][2];

    if (n > 1) {
        fib_r(n / 2, mat);
        fib_multiply(tmp, mat, mat);
        fib_copy(mat, tmp);
    }

    if (n % 2) {
        fib_multiply(tmp, mat, f);
        fib_copy(mat, tmp);
    }
}

long fib(int n)
{
    long mat[2][2] = {{1, 0}, {0, 1}};

    fib_r(n - 1, mat);

    return mat[0][0];
}

Another way to do it without recursion might look like this:

long fib(int n)
{
    long f[2][2] = {{1, 1}, {1, 0}};
    long mat[2][2] = {{1, 0}, {0, 1}};
    long tmp[2][2];

    for (; n; n /= 2) {
        if (n % 2) {
            fib_multiply(tmp, mat, f);
            fib_copy(mat, tmp);
        }

        fib_multiply(tmp, f, f);
        fib_copy(f, tmp);
    }

    return mat[0][0];
}

In both cases the lion's share of work is done by the matrix multiplication, and understanding how that comes to the conclusion with a good paper run would be a good idea:

void fib_copy(long dst[2][2], long src[2][2])
{
    int i, j;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++)
            dst[i][j] = src[i][j];
    }
}

void fib_multiply(long dst[2][2], long a[2][2], long b[2][2])
{
    int i, j, k;

    /* Clear the destination */
    for (i = …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can you tell me one thing that c is just a pointer, then how is it treated like an array ?

...seriously?

We've been over this so many times over the course of a few months now, I'd expect you to be a pointer guru at this point. :P

A pointer is not an array, but array indexing essentially becomes the dereference of an offset from a pointer. a[i] is equivalent to *(a + i), so it doesn't matter if a is a pointer or an array, you can use indexing syntax with both.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thanks for replying to my question with negative reputation, that totally makes me want to continue helping you. :rolleyes:

The "topic" you want is basically the whole of computer science and engineering, so you're not going to find a single source of information. You need to pick a specific topic and then search for that, then pick another specific topic and search for that. Just searching for "computer organization" will get you results, but not terribly useful results if you're expecting some sort or brain dump of everything there is to know.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The 2 byte bit pattern of 512 is 0000 0010 0000 0000. The 2 byte bit pattern of 514 is 0000 0010 0000 0010. The one byte bit pattern of 2 is 0000 0010. You do the math.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It wouldn't work without an emulation layer or cross compiling with a Windows target. The format of object code files is different between Linux and Windows.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so anyway no option to get stream from the keyboard?

No portable option. There are non-portable options, but we kind of have to know what OS and compiler are being targeted before any viable options can be suggested.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

the more declarations ,the more momory it takes in RAM??

The usual situation is that each process is given a certain amount of stack space that the program can use for things like local variables and function call frames. Assuming those integers are stack objects then they'd be rolled up into the stack size and wouldn't increase the memory footprint of the program at runtime.

But there are other types of objects. Notable is static data, where the size is rolled up into the executable itself and if large, could have an impact on how much memory the running process uses when the executable is loaded.

what decides the capacity of a compiled program (exe).

Code instructions and static data (the so called text and data segments) are the two biggies for the size of an executable. Also note that static linking vs. dynamic linking will affect the size of the file because with the former libraries are stored within the executable.

You might consider looking into the Portable Executable (PE) format for Windows and the Executable and Linkable Format (ELF) format for POSIX to get an idea of how operating systems structure an executable file.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can C pass by reference?

No, but you can fake it with pointers.

Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.

C requires that structure names be qualified with the struct keyword. So while you can do this with C++:

struct foo { int x; };

foo my_var;

In C it would need to be this:

struct foo { int x; };

struct foo my_var;

C++ implicitly wraps the equivalent of a typedef around the structure so that you don't need the struct keyword. Conveniently enough, you can do the same thing explicitly in C to the same effect. This code would work the same way in both C and C++:

typedef struct foo { int x; } foo;

foo my_var;

Does C have 'for' loops?

Yes.

I created a new node:
node = new animalTree(data);

with a constructor that initializes data and the left right nodes.

how do I do that in C

As far as allocating memory, you'd use malloc:

node = malloc(sizeof *node);

C doesn't have constructors, so you're left with writing an initialization function, an allocation and intialization function, or ad hoc initialization after you allocate memory. I'd prefer the second, because it's a bit closer to how a constructor would work:

animalTree *make_node(void *data)
{
    animalTree *nn = malloc(sizeof *node);

    if (nn) {
        nn->data = data;
        nn->left …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I wouldn't use bool for a bitset of error flags, but that approach isn't bad. In fact, iostream implementations will typically use it for the error state of a stream.

What kind of errors are these? Have you considered exceptions?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Oh, and you can also simplify things by using a specific locale that has the grouping for numbers in the way you want. For example, my local locale has a grouping of "/003", so I can simply do this for the same effect:

#include <iomanip>
#include <iostream>
#include <locale>

using namespace std;

int main()
{
    cout.imbue(locale(""));
    cout << fixed << setprecision(2) << 10000.00 << '\n';
}

But that might also be tricky because now you get all of the other locale-specific stuff that comes along with it and not just grouping and thousands separators. If all you want is a specific number grouping and not a full locale change then my first code would be preferrable.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm also unable to reproduce the problem. I hate intermittent issues, they're so hard to troubleshoot. :(

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's pretty easy, if a tad obscure for those unfamiliar with locales in C++:

#include <iomanip>
#include <iostream>
#include <locale>

using namespace std;

int main()
{
    struct group_facet: public std::numpunct<char> {
    protected:
        string do_grouping() const { return "\003"; }
    };

    cout.imbue(locale(cout.getloc(), new group_facet));

    cout << fixed << setprecision(2) << 10000.00 << '\n';
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The Fibonacci sequence. It is as np_complete said, with the addition that by definition Fib(0) == 0, Fib(1) == 1.

It's been established that nitin is going for the O(log n) approach using matrix exponentiation. I think (hope!) he already understands the "usual" implementation.

WaltP commented: Questionable ;o) +14
I_m_rude commented: awesome guess! ;) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The file just isn't in the right place. Move it to your project folder along with the .cpp file and try again. If that doesn't work, move it into the debug folder with the .exe file for your program.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If I comment out one or the other I still do not get my .txt file to show.

The file you typed is never the file that gets read. Ever. Period. Why? Because you only use fin to read lines, and fin always uses "Show.txt". So is Show.txt the file that you actually have and want to read? If so, then it's not located in the correct place (as Walt said), and you need to do some error checking. Otherwise you need to stop opening Show.txt and use file to read lines instead.

perror() may work for you to get some additional error information:

#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string filename;

    cout << "Enter a file name: ";

    if (getline(cin, filename)) {
        ifstream in(filename.c_str());

        if (!in) {
            perror("Error opening file");
        }
        else {
            cout << "The file opened successfully\n";
        }
    }
}