473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jan 14 '09 #1
6 1964
gpraghuram
1,275 Expert 1GB
hi,
This seems like a homework question.
Raghu
Jan 14 '09 #2
Banfa
9,065 Expert Mod 8TB
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).
Jan 14 '09 #3
it is an interview question.I would like to know the answer.
Jan 14 '09 #4
Banfa
9,065 Expert Mod 8TB
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.
Jan 14 '09 #5
JosAH
11,448 Expert 8TB
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
Jan 14 '09 #6
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.
Jan 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
6
by: Doug | last post by:
Hello, Can anyone recommend an inexpensive source code comparison utility for .NET. I broke my code an now I need to compare my older version w/ the new one and could use the help of line...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) -...
7
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. ...
7
by: matt | last post by:
hello, i have been given a challenging project at my org. i work on an inventory management web application -- keeping tracking of parts assigned to projects. in the past, i built an in-house...
11
by: Steven D'Aprano | last post by:
I'm writing a class that implements rich comparisons, and I find myself writing a lot of very similar code. If the calculation is short and simple, I do something like this: class Parrot: def...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
5
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.