?
Solved

how to compare integers in an array

Posted on 2002-05-27
2
Medium Priority
?
327 Views
Last Modified: 2010-04-02
Lets say I have 2 arrays:
----------------------------------------
int* a=new int[9]
int* b=new int[9]

a=[1,2,3,4,5,6,7,8,9]
b=[1,2,3,4,6,4,3,2,1]
----------------------------------------
compare(a,b,first_5) => false

Now.. how can I compare first 5 (or first n) integers, without having to use a while loop (or for)?
Is there a faster way at all (then to use a loop)?
Thank you!

Jurij
0
Comment
Question by:jurij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 total points
ID: 7037023
You can use 'memcmp()':

memcmp ( a, b, 5 * sizeof ( int));

This will do a binary compare for the first 5 elements in the arrays, which is equal to compare each of the elements.

Here's a smple from the VC++ docs

Example

/* MEMCMP.C: This program uses memcmp to compare
 * the strings named first and second. If the first
 * 19 bytes of the strings are equal, the program
 * considers the strings to be equal.
 */

#include <string.h>
#include <stdio.h>

void main( void )
{
   char first[]  = "12345678901234567890";
   char second[] = "12345678901234567891";
   int result;

   printf( "Compare '%.19s' to '%.19s':\n", first, second );
   result = memcmp( first, second, 19 );
   if( result < 0 )
      printf( "First is less than second.\n" );
   else if( result == 0 )
      printf( "First is equal to second.\n" );
   else if( result > 0 )
      printf( "First is greater than second.\n" );
   printf( "Compare '%.20s' to '%.20s':\n", first, second );
   result = memcmp( first, second, 20 );
   if( result < 0 )
      printf( "First is less than second.\n" );
   else if( result == 0 )
      printf( "First is equal to second.\n" );
   else if( result > 0 )
      printf( "First is greater than second.\n" );
}


Output

Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '12345678901234567890' to '12345678901234567891':
First is less than second.

0
 

Expert Comment

by:GregToombs
ID: 7037836
I think the suggested call to memcmp is the fastest route, simply because it has been optimized by previous programmers. Conceptually speaking, keep in mind that any such comparison, including memcmp itself, needs to use a loop of some description.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question