what is the wrong with this function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mhrt2008
    New Member
    • May 2009
    • 6

    what is the wrong with this function?

    hallo >>>

    i am try to write function that do add in C
    ex:
    1= 1+0 ==>1
    2= 2+1+0 ==> 3
    3= 3+2+1+0 ==> 6
    ....
    ........
    ..............

    and i wrote this function ..that is working with 1 and 2 >> but it is don't work with the 3 ,4 .....??????? why

    Code:
    int sum(int X)
    
    {
       int sum=0;
      int j=0;
       while (j <= X)
     {    sum = sum + X;
          j++;
       X=X-1;
     }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are altering j and X resulting in your while condition becoming false halfway through the iterations.

    Write down the variable values for the loop iterations on paper for a simple not working case (say 3) and you will see.

    You can fix this by getting rid of the variable j altogether, it is not required to make this algorithm work the way you have coded it.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I think you do need a loop variable. It can either count up from 0 to X; or it can count down from X to 0.

      Comment

      • mhrt2008
        New Member
        • May 2009
        • 6

        #4
        Banfa ==> i wrote it the case (3) in paper ..and it supose to give me 6 but i found it 5 in the code..... if there is another way to do it write it to me


        donbock==> just give me example.... because i use while loop ..not for loop

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You can speed up your code by removing two iterations:

          1. No need to add 0 to the sum, so no need for the loop variable to ever actually be zero.

          2. Initialize the sum to the first loop value instead of to zero (1 if you're counting up; or X if you're counting down). But then you have to make sure your loop variable starts on the second value.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by mhrt2008
            Code:
            int sum(int X)
            {
               int sum=0;
               ...
            }
            By the way, as a matter of policy I wouldn't use the same name ("sum") for both the function and for an automatic variable within that function. Programming is complicated enough without adding extra opportunities for confusion.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by donbock
              I think you do need a loop variable. It can either count up from 0 to X; or it can count down from X to 0.
              Strictly speaking if counting down from X to 0 then you can use X as the loop variable removing the necessity for a separate loop variable (although that is not an approach I necessarily like).

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Sum(X) = 0 + 1 + 2 + ... + X

                I'm sure you were given this assignment so you could practice working with loop instructions. However, let's pretend for a moment that programming is your profession and you need to solve this equation as part of a project at work.

                The loop-based solution is fine, but it is obvious that the execution time will vary with X. The larger X gets, the more times your program iterates through the loop. In fact, this is an O(N) [pronounced "Order-N"] algorithm -- execution time is linearly related to the input variable. That's pretty good, but you can do better.

                Function Sum(X) is an arithmetic series; and it has a closed-form solution. That is, an O(1) ["Order-1"] algorithm that has a fixed execution time independent of the input value. I leave it to you to track down this algorithm if you're interested.

                If you intend to make programming your profession then you should familiarize yourself with mathematical simplications of this sort.

                (Don't use the closed-form algorithm for this assignment because your teacher is probably grading you on your skill with loop instructions.)

                Comment

                • mhrt2008
                  New Member
                  • May 2009
                  • 6

                  #9
                  thank for every one here >>>

                  i found my wrong .. and it is work fine now....

                  Comment

                  Working...