go_away

Author Topic: comparing arrays, easy one but im a student, C++  (Read 4209 times)

0 Members and 1 Guest are viewing this topic.

Offline sseb4Topic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
comparing arrays, easy one but im a student, C++
« on: August 29, 2007, 05:15:34 AM »
hey i need a way of comparing say 1 array eg n[10] to predetermined arrays that matches n[10]. n[10] will be scanned/input into the program. How could i do this in C++. would strcmp or memcmp work? i dont really know how to use memcmp as i dont hae a copy of a compiler for C++ at home. Please give me an example of how to do this

Offline Nyx

  • Robot Overlord
  • ****
  • Posts: 204
  • Helpful? 0
Re: comparing arrays, easy one but im a student, C++
« Reply #1 on: August 29, 2007, 06:14:20 AM »
I'm pretty sure std::vector defines an operator ==.... So if you use that container, you can just compare all elements at once with that. Otherwise, just use a for loop and compare each individual element. If one does not match, then the arrays don't have the same content.

Offline MadMax

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: comparing arrays, easy one but im a student, C++
« Reply #2 on: September 11, 2007, 01:32:05 PM »
My solution would be as following

Code: [Select]
bool CompareArrays() {
  for(int x = 0; x < 10; x++) {
    if(array1[x] != array2[x]) {
      return false;
    }
  }
  return true;
}

void main() {
  if(CompareArrays == true) {
    print("wooohoooo");
  }
  //not sure about the 'print' command, I'm a C++ coder, so I use cout<< "blabla";
}

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: comparing arrays, easy one but im a student, C++
« Reply #3 on: September 12, 2007, 03:23:38 PM »
Quote
would strcmp or memcmp work?
definitely :)

Make sure though that your both arrays use the same data type. No use in doing a compare if one of your arrays contains 2-byte shorts and the other contains 1-byte chars.

Quote
MEMCMP(3)                           NEWLIB                           MEMCMP(3)

NAME
       6.7 `memcmp'--compare two memory areas

SYNOPSIS
            #include <string.h>
            int memcmp(const void *S1, const void *S2, size_t N);

DESCRIPTION
       This function compares not more than N characters of the object pointed
       to by S1 with the object pointed to by S2.

RETURNS
       The function returns an integer greater than, equal  to  or  less  than
       zero    according  to  whether  the  object pointed to by S1 is greater
       than, equal to or less than the object pointed to by S2.

PORTABILITY
       `memcmp' is ANSI C.

          `memcmp' requires no supporting OS subroutines.

SEE ALSO
       memcmp is part of the libc library.  The full documentation for libc is
       maintained  as  a  Texinfo  manual.   If  info  and  libc  are properly
       installed at your site, the command

              info libc

       will give you access to the complete manual.

NEWLIB                            2006 Aug 09                        MEMCMP(3)
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,657
  • Helpful? 169
    • Society of Robots
Re: comparing arrays, easy one but im a student, C++
« Reply #4 on: September 15, 2007, 01:03:47 PM »
Code: [Select]
if(array1[x] != array2[x]) {
      return false;}
hmmm i would have just done a for loop cause im a noob like that . . .
does that also work in C, or just C++?

Offline snow

  • Full Member
  • ***
  • Posts: 73
  • Helpful? 0
Re: comparing arrays, easy one but im a student, C++
« Reply #5 on: September 21, 2007, 02:21:25 AM »
Noob or not... that is the only way you can compare two arrays. In loop of somekind (for, while, do-while).

Simple stuff like loops, conditionals, arrays are the same in C and C++.

From wikipedia:
Quote
Compared to the C language, C++ introduced extra features, including declarations as statements, function-like casts, new/delete, bool, reference types, inline functions, default arguments, function and operator overloading, namespaces and the scope resolution (::) operator, classes (including all class-related features such as inheritance, member functions, virtual functions, abstract classes, and constructors), templates, exception handling, runtime type identification, and the overloaded input (>>) and output (<<) operators for input and output respectively.

So as long you dont use this stuff in C++ you basicly have C :)

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: comparing arrays, easy one but im a student, C++
« Reply #6 on: September 22, 2007, 10:01:12 AM »
Further analysis of an array (based on the specifications) shows that these are allocated as a contiguous memory area.
A loop is OK, but as I've previously mentioned, a memcmp will work as efficiently.
Less code to write in fact, and as always less is always more.

Noob or not... that is the only way you can compare two arrays. In loop of somekind (for, while, do-while).
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

 


Get Your Ad Here