Home > OS >  C/C Bitwise operation university problem
C/C Bitwise operation university problem

Time:09-05

Problem

Given a sequence of N integer values(possible values of N: 3, 5, 7, 9, 11 ...). Using at most one conditional operator(or conditional ternary operation), the program must determine whether it is true that every three elements the sequence increases and then decreases(sine sequence). Loops in a program can only be used to enumerate elements of a sequence.

Example

  *   *
 * * *  *
*   *    *

What I think

int compare_num(int num1, int num2)
{
    return (int)(((num1   num2) - sqrt((num1 - num2) * (num1 - num2))) / 2);
}
bool_t is_increasing2(int arr[], int size)
{
    int tmin = arr[0];
    int res = 0;
    for (int i = 0; i < size - 1; i  )
    {
        tmin = compare_num(arr[i   1], tmin);
        res = tmin ^ arr[i   1];
    }

    return res;
}
int main(void)
{
    int arr[] = {10, 9, 8, 7, 6, 5};
    int arr2[] = {1, 2, 3, 4, 5};
    int res = is_increasing2(arr2, N);

    if (res == 0)
    {
        printf("Decreasing\n");
    }
    else
    {
        printf("Increasing\n");
    }
    return 0;
}

I use this code to check, that sequence is increasing or not. But now I need to use it to check, that my sine sequence is sine and I can't use more ternary or if/else operators

There is what I have to real problem

bool_t is_increasingSine2(int arr[], int size){
    int tmax = 0;
    int res = 0;
    for(int i = 0; i < size; i  = 3){
        for(int j = i; j < i   2 && j < size - 1; j  ){
            tmax = compare_num(arr[j], arr[j   1]);
            res = tmax ^ arr[j   1];
        }
     //There if res == 0 part of sine is increasing otherwise not, but what to do next???
    }

    return 0;
}

CodePudding user response:

You do not need any conditional operators for this problem, but we will use one in the printf since it is suggested.

Rephrasing the problem as a requirement that each of the first two elements must be less than the one that follows it and each of the next two must be greater than the one that follows it makes it fairly simple to test:

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


/*  Given array Array with N elements, return true iff each of the first two
    elements is less than the one following it, each of the next two is greater
    than the one following it, and then repeating in a cycle until the end of
    the array.
*/
static _Bool TestCriterion(int Array[], size_t N)
{
    /*  Iterate through each element of the array except the last (the one
        with index N-1), since there is no element following the last element
        to compare it to.
    */
    for (size_t i = 0; i < N-1;   i)
    {
        /*  Set phase to 0 for the first two elements, 1 for the next two,
            then 0, 1, 0, 1…  Note that while "i & 2" has the value 0 or 2,
            assigning it to a Boolean produces a value of 0 or 1.
        */
        _Bool Phase = i & 2;

        /*  For phase 0, test whether the element with index i is less than
            the element with index i 1, and return false (0) if it is not.  For
            phase 1, compare element i 1 with element i.
        */
        if (! (Array[i Phase] < Array[i 1-Phase]))
            return 0;
    }

    //  All elements passed the test, so return true (1).
    return 1;
}


/*  Compute the number of elements in an array by dividing the size of the
    array by the size of an element.
*/
#define NumberOf(a) (sizeof (a) / sizeof *(a))


int main(void)
{
    int Array[] = { 1, 2, 3, 2, 1, 2, 3, 2, 1 };

    printf("%s.\n", TestCriterion(Array, NumberOf(Array)) ? "Passes" : "Fails");
}
  • Related