Advertisement

06.28.2008 at 04:17PM PDT, ID: 23524401
[x]
Attachment Details

Implementing a CheckForZero function

Asked by tmitch68 in C Programming Language

Tags: C

Hello,

I've been studying some questions in preparation for an interview.  One of them has given me some trouble.

Since it's an interview prep question, I don't want anyone to just give me the answer.  

I believe I might be headed in the right direction; I would just like to know if I am anywhere in the ballpark, and how to go about implementing this if I am.

This is the question:

5.  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. Optimize this code to
      do only one compare. Feel free to instead use some mathematical
      operations such as ADD, SUB etc.


What I think might be the solution is to create another array of the same length that is completely composed of 1s.

Then, use this array as a sort of mask.  Is there some way to multiply or to AND entire arrays in a single fell swoop?

Thanks in advance,
TimStart Free Trial
 
Loading Advertisement...
 
[+][-]06.28.2008 at 04:34PM PDT, ID: 21892363

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.28.2008 at 04:38PM PDT, ID: 21892380

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 04:40PM PDT, ID: 21892386

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 04:44PM PDT, ID: 21892397

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 05:18PM PDT, ID: 21892481

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 05:22PM PDT, ID: 21892487

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 05:45PM PDT, ID: 21892537

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 05:46PM PDT, ID: 21892542

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.28.2008 at 05:47PM PDT, ID: 21892548

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: C
Sign Up Now!
Solution Provided By: martin_kongen
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.28.2008 at 09:43PM PDT, ID: 21892992

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.29.2008 at 01:33PM PDT, ID: 21895364

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628