how to compare integers in an array

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

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!

Question by:jurij
LVL 86

Accepted Solution

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


/* 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" );


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


Expert Comment

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.

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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