Solved

how to compare integers in an array

Posted on 2002-05-27
2
323 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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 99
Which IDE to use to begin C++ training? 5 70
No module found pypyodbc, 3 45
visual C++ 1 16
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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