comparison code in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • strange
    New Member
    • Jan 2009
    • 2

    comparison code in C

    Consider the following code:

    int CheckForZero (int a[], int n)
    {
    int i;

    for (i = 0; i < n; i++) {
    if (a[i] == 0) {
    return (TRUE);
    }
    }
    return (FALSE);
    }


    This code works - but it does a check for every element in 'a' - i.e.
    it does "n" compares and bails early if it finds even one zero element.
    Our array 'a' generally does not have a zero.We need to optimize this code to do only one compare. use some mathematical
    operations such as ADD, SUB etc.

    Please reply to this question
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    hi,
    This seems like a homework question.
    Raghu

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I think you are attempting the impossible, you are not going to find a single comparison to test any entire array for any occurrences of a zero element.

      Replacing comparisons with mathematical operations is hardly likely to make a large difference to the execution speed of this function, and frankly this exercise has to be either some misguided attempt at code optimisation or a classwork assignment to see if you are familiar with the mathematical operators.

      In the former case you should be profiling your program not attempting to optimise this function in the later we can't do your work for you examine the four basic arithmetic operators and devise an algorithm that while doing a lot of calculation and still using a loop does enable the result to be obtained with a single conditional (if, obviously you would have to ignore the conditionals implicit in the for loop).

      Comment

      • strange
        New Member
        • Jan 2009
        • 2

        #4
        it is an interview question.I would like to know the answer.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Well like I said you just need one of the basic 4 arithmetic operators, getting your result is dependent on one of the basic most well know results of the operator you require.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            The old Convex (bought by HP) machines could do that: an entire vector or registers could be operated upon, just as if they were single simple numbers. Too bad that C code didn't easily 'vectorize' and an additional library wasn't much help either. APL was much better at that.

            kind regards,

            Jos

            Comment

            • aneeshazeez
              New Member
              • Jan 2009
              • 2

              #7
              Solution 1
              its very simple.
              add one single line
              a[0] = 0;
              only one comparison

              Solution 2
              for (i=0;i<n;i++)
              {
              Sum1 += a[i];
              Sum2 +=(a[i] - 1);
              }
              if((Sum1 - Sum2) != n)
              return TRUE;
              else
              return FALSE;
              only one comparison :)

              so simple so humble :) your PC is not a magician.
              Last edited by aneeshazeez; Jan 16 '09, 08:46 AM. Reason: more solutions

              Comment

              Working...