0

So I have an array of N size. I want to first check if the array is of alternating sequence (We are assuming that the numbers are positive and the values are either 1 or 0)

101010 would return true
110101 would return false

After I check this I want to know how many of these numbers I need to change for it to be an alternating sequence for example:

1,1,0,1,1 would return 2 because you can reverse the first and fifth index to achieve 0,1,0,1,0

Currently I am checking for the alternating sequence like so:

#include <stdio.h>   // For I/O operations
#include <stdbool.h> // for 'true' and 'false'

int isArrayAlternating(int ints[], size_t N) {
    if (ints == NULL || sizeof(ints) % 2 != 0)
        return false;

    for (int i = 0; i < sizeof(ints) - 1; i++)
        if (ints[i] != ints[i + 1] * (-1))
            return false;

    return true;
}

int main(void) {
    int ints[] = {1, 0, 1, 0};
    size_t N = sizeof(ints) / sizeof(ints[0]);

    isArrayAlternating(ints);

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
ScuffedCoder
  • 376
  • 1
  • 5
  • 21

3 Answers3

3
  • You cannot use sizeof to determine size of passed array, so you have to pass that. (for more information, see C sizeof a passed array - Stack Overflow)
  • Multiplying -1 will turn 1 into -1 and 0 into 0. It cannot switch between 1 and 0, so the condition is wrong.
  • Your isArrayAlternating has 2 arguments, but you passed only one when you call it.

Code with these points applied:

int isArrayAlternating(int ints[], int N) {
    if (ints == NULL || N % 2 != 0) {
        return false;

    }
    for (int i = 0; i < N - 1; i++) {
        if (ints[i] != 1 - ints[i + 1]) {
            return false;
        }
    }
    return true;
}

int main() {

    int ints[] = {1, 0, 1, 0};
    isArrayAlternating(ints, sizeof(ints) / sizeof(*ints));
}

The next step may be adding some code to print the result.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • I have added the 2nd argument in the code in OP's code. – Rohan Bari Oct 05 '20 at 14:26
  • Thank you, so now the check is complete of seeing if the numbers are alternating. If the result were to be false and like i mentioned in the post that i want to check how many i need to change for it to be alternating. Where would i even start this or calculate which is required to change – ScuffedCoder Oct 05 '20 at 14:27
  • When the size is given, there are only 2 "alternating sequences": `1010...` and `0101...`. Try these 2 putterns, count elements that differ from these and take the smaller count. – MikeCAT Oct 05 '20 at 14:29
  • When you say "try these 2 patterns" in what sense do you mean this? Could you explain in a coding manner perhaps? i find it easier to wrap my head around code than words haha – ScuffedCoder Oct 05 '20 at 14:33
  • Something like `int c1 = 0, c2 = 0; for (int i = 0; i < N; i++) { if (ints[i] == (i % 2)) c1++; else c2++; } return c1 <= c2 ? c1 : c2;`. Note that all corresponding elements of these 2 putterns differs, so if an element matches one of the putterns, it always differ from another puttern. – MikeCAT Oct 05 '20 at 14:38
  • So would i be calling for this check in my for statement? Because at the moment if the statement returns false then it doesnt get to this seperate for loop? – ScuffedCoder Oct 05 '20 at 14:45
0

For starters the first function parameter should have the qualifier const because the passed array is not changed in the function. Also it is not a good idea to use the identifier ints for the array name because it makes the function less readable. And do not use identifiers for variables that consist from all upper case letters.

The function declaration could look like

int isArrayAlternating( const int a[], size_t n ) {

Secondly you are not using the second parameter within the function body. And you forgot to supply the corresponding argument. Instead of it you are using the expression

sizeof(ints)

that gives the size of a pointer because function parameter having an array type is implicitly converted by the compiler tp pointer to the array element type. That is these two function declarations

int isArrayAlternating( const int a[], size_t n );

and

int isArrayAlternating( const int *a, size_t n );

declare the same one function.

Within the function you should not check whether the passed pointer is equal to NULL or whether the number of elements is even. For example in your question you are using an array of an odd number of elements

1,1,0,1,1

And the condition in the if statement

if (ints[i] != ints[i + 1] * (-1))

does not make a sense.

The function definition can look like

int isArrayAlternating( const int a[], size_t n ) 
{
    size_t i = 0;

    if ( n )
    {
        int value = a[0];

        for ( ; i < n && a[i] == value; i++ )
        {
            value ^= 1;
        }
    }

    return i == n;
}

And the function can be called like

int alternative = isArrayAlternating(ints, N );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

This is an interesting problem to translate into Ada:

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type Bit_Array is array (Positive range <>) of Boolean with
      Pack;
   function Is_Alternating (Item : Bit_Array) return Boolean with
      Pre => Item'Length >= 2
   is
      Value : Boolean  := True;
      I     : Positive := Item'First;
   begin
      loop
         Value := Item (I) xor Item (I + 1);
         exit when Value = False or I = Item'Last - 1;
         I := I + 1;
      end loop;
      return I = Item'Last - 1 and Value = True;
   end Is_Alternating;

   A : Bit_Array := (True, False, True, False, True, False);
   B : Bit_Array := (False, True, False, True, False, True, False, True);
   C : Bit_Array := (True, True, False, True, True);
begin
   Put_Line ("A : " & Boolean'Image (Is_Alternating (A)));
   Put_Line ("B : " & Boolean'Image (Is_Alternating (B)));
   Put_Line ("C : " & Boolean'Image (Is_Alternating (C)));
end Main;

The output of this program is:

A : TRUE
B : TRUE
C : FALSE

This solution creates a packed array of boolean values. A packed array of boolean results in an array of bits, each bit representing a single boolean value. The array type is unconstrained, allowing each instance of Bit_Array to have a different length. Thus, the instances labled A, B, and C have different lengths.

Jim Rogers
  • 4,822
  • 1
  • 11
  • 24