-1

We have two arrays A and B, each of 10 integers. Write a function that tests if every element of array A is equal to its corresponding element in array B. In other words, the function must check if A[0] is equal to B[0], A[1] is equal to B[1], and so forth. The function is to return true if all elements are equal and false if at least one element is not equal.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/* run this program using the console pauser or add 
   your own getch, system("pause") or input loop */

bool array(int ptr1[], int ptr2[]);

int main(int argc, char *argv[]) {

    int array1[] = {11, 33, 34, 25, 16, 2, 24, 57, 86, 66};
    int array2[] = {11, 33, 34, 25, 16, 2, 24, 57, 86, 66};

    int i;

    printf("Array A\t\tArray B\n");

    for(i = 0; i < 10; i++)
        printf("%d\t\t%d\n", array1[i], array2[i]);

    printf("\nResult of comparision: \n");

    bool result = array (array1, array2);

    if(result == 1)
        printf("false\n");
    else
        printf("false\n");
    getch();

    return 0;
}

The errors that I get are:

main.c(.text+0xfa): undefined 'array'
[Error]  Id returned 1 exit status
recipe for target '"Problem' failed
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
Nick D
  • 1
  • 1
  • 1
    `bool` is defined in `stdbool.h` for C99. – Galen Dec 12 '17 at 03:09
  • Possible duplicate of [C99 boolean data type?](https://stackoverflow.com/questions/4767923/c99-boolean-data-type) – Galen Dec 12 '17 at 03:11
  • 4
    I see where you give a (forward) *declaration* for `array()`, but I don't see where you *define* it (i.e., write it's body). Is there another source file you need to include when compiling? (or use `-c` to compile without linking?) – lockcmpxchg8b Dec 12 '17 at 03:17
  • You were given this function prototype: `bool array(int ptr1[], int ptr2[]);`, and you should now implement this function. Your code assumes that it already exists somewhere, but it doesn't - that's why you were given this task. – goodvibration Dec 12 '17 at 07:07

1 Answers1

1

The problem is you have declared array:

bool array(int ptr1[], int ptr2[]);

but not defined it. When you link, the linker looks for the definition of function array and cannot find one, prompting the error:

main.c(.text+0xfa): undefined 'array'
[Error]  Id returned 1 exit status
recipe for target '"Problem' failed

To define the function at same point your declare it, you must add the body of the function. In that case, there is no need for a forward declaration. It has been declared before its use in main, e.g.

int arraycmp (int ptr1[], int ptr2[], size_t size)
{
    return memcmp (ptr1, ptr2, size);
}

(note: regardless of whether you use memcmp or simply loop over each element and perform a single comparison for each, you must pass the number of elements as a parameter)

If on the other hand, you do not define the function until after main(), then the forward declaration is required so that the function is usable in main().

Putting it altogether, you can do something similar to the following:

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

/* run this program using the console pauser or add 
   your own getch, system("pause") or input loop */

int arraycmp (int ptr1[], int ptr2[], size_t size)
{
    return memcmp (ptr1, ptr2, size);
}

int main (void) {

    int array1[] = {11, 33, 34, 25, 16, 2, 24, 57, 86, 66},
        array2[] = {11, 33, 34, 25, 16, 2, 24, 57, 86, 66},
        i;

    printf("Array A\t\tArray B\n");

    for(i = 0; i < 10; i++)
        printf("  %d\t\t  %d\n", array1[i], array2[i]);

    printf("\nResult of comparision: %s\n", 
        arraycmp (array1, array2, sizeof array1) ? "false" : "true");

#if defined (_WIN32) || defined (_WIN64)
    getchar();
#endif

    return 0;
}

(note: if there is any chance your arrays differ in number of elements, you should check before calling arraycmp and handle the error as you wish. Either return exiting that point or pass the size of the smaller and only compare that initial number of elements)

Example Use/Output

$ ./bin/arrcmp
Array A         Array B
  11              11
  33              33
  34              34
  25              25
  16              16
  2               2
  24              24
  57              57
  86              86
  66              66

Result of comparision: true

Changing 57 to 56 in array2,

$ ./bin/arrcmp
Array A         Array B
  11              11
  33              33
  34              34
  25              25
  16              16
  2               2
  24              24
  57              56
  86              86
  66              66

Result of comparision: false

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85