rproffitt commented: Why? -3

İ can't understand this example ;how we found 4 paths?.

i excute it on a paper and i found 3 paths one for pow=-b. Seond for pow=b;and the third path for pow=0. But i can't understand the fourth path for what?.

OK, let's start off by pointing you towards the Daniweb Forum Posting Rules before proceeding, as you just broke several of them.

  • Do ensure that all posts contain relevant content and substance and are not simply vehicles for external links
  • Do not post the same question multiple times
  • Do provide evidence of having done some work yourself if posting questions from school or work assignments
  • Do post in full-sentence English
  • Do not write in all uppercase or use "leet", "txt" or "chatroom" speak

This last two are generally considered to also cover the practice of posting a series of fragmentary posts in the manner of a chat room or Twitter feed. As my own post demonstrates, you are not limited to 144 (or even 288) bytes of plain text, and posting sentences separately which could be part of a single post is irritating and inappropriate. Note also that you can edit posts up to 30 minutes after they have been submitted, so typos can be corrected after the fact as well (up to a point; we will forgive it if you simply didn't notice it until after that 30 minute window).

The rule about posting multiple times also applies to simultaneously cross-posting a question to several message boards (such as Daniweb, Dev Shed, and Stack Overflow), though it is more a matter of timing - you should post in one at a time, giving the forum time to answer the question before cross-posting elsewhere if they can't.

As a new member, you will be forgiven - mostly - for these mistakes, but you really don't want to make one of the regulars remind of these again.

I will see to addressing the actual problem at hand shortly, but I cannot promise that I or anyone else here will be able to give you any recommendations.

Let's start by re-posting the pseudo-code from the Stack Overflow post (at least, I hope it was meant to be pseudo-code, as it isn't quite legal C) in a somewhat more readable fashion.

begin
    int a, b, pow;
    float c;

    input(a, b);

    if (b < 0)
        pow = -b;
    else
        pow = b;

    c = 1;
    while (pow != 0) {
         c = c * a;
        pow = pow - 1;
    }

    if (b < 0)
        c = 1 / c;

   Output(c);
End

Let me also post the datapath chart for reference purposes:

Now we can address your question. Let's start by pointing out that the number of data paths is equal to the number of branches in the path; so any time you have a conditional statement (if(),while(),for(), do..while(), orswitch()), you have an additional path. If a case analysis (an if() or a switch()) has more than one path - for example, an else clause, the multiple case clauses in a switch(), or the exit condition of any of the loops - then each of those needs to be counted. So, for this code, the first conditional is

    if (b < 0)
        pow = -b;
    else
        pow = b;

which has two paths, the if() and the else. Note that most C programmers would use the abs() function instead, and count this as one path, but as given, it is two paths.

The next conditional is a while(),

    while (pow != 0) {
         c = c * a;
        pow = pow - 1;
    }

so you need to count the loop condition as a branch, where one path follows the body of the loop and the other doesn't. Thus, this adds one more path aside from the default, so right now we are up to four already (the two paths from the first case analysis, the while()'s looping path, and same while()'s exit path). You may need to reconsider if the total really is four for the whole function.

This should be enough information to proceed, though feel free to ask more questions if you need to.

Comments and corrections by other regulars here are welcome. I know that there may be some disagreement on what I said, especially on the question of whether the exit paths of iterations counts as a separate path or not.

Thank you very much for this wonderful explanation but I have a last question on the subject .. Is the last condition in the question was calculated within the four paths ,i mean " if(b<0) , c=1/c "??. I know that the power number calculation includes positive, negative and zero but i do not know how the fourth path appeared? Is there an explanation of the issue?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.