75 lines
2.5 KiB
C
75 lines
2.5 KiB
C
#include <stdio.h>
|
|
|
|
#define N 20
|
|
#define ZERO 0
|
|
#define ONE 1
|
|
#define TWO 2
|
|
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
|
|
#define BOOLEAN unsigned short
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// Equality
|
|
// --------
|
|
//
|
|
// General : Checks whether two numbers are equal in value.
|
|
//
|
|
// Parameters :
|
|
// number1 - first number (int)
|
|
// number2 - second number (int)
|
|
//
|
|
// Return value : If there is an equality between the two numbers (BOOLEAN).
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
// Programmer : Cohen Idan
|
|
// Student No : None
|
|
// Date : 21.10.2019
|
|
//---------------------------------------------------------------------------------------
|
|
BOOLEAN Equality(int number1, int number2)
|
|
{
|
|
int sub = number1 - number2;
|
|
BOOLEAN answer = (ONE / (ABS(sub) + ONE));
|
|
return answer;
|
|
}
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
// Exercise 4
|
|
// ----------
|
|
//
|
|
// General : The program checks several pairs of consecutive characters that are
|
|
// equal to the last pair of characters in the array.
|
|
//
|
|
// Input : 20 chars.
|
|
//
|
|
// Process : The program checks several pairs of consecutive characters that are
|
|
// equal to the last pair of characters in the array.
|
|
//
|
|
// Output : The number of consecutive character pairs that equals the last
|
|
// pair of characters in the array (unsigned short).
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
// Programmer : Cohen Idan
|
|
// Student No : 211675038
|
|
// Date : 23.10.2019
|
|
//---------------------------------------------------------------------------------------
|
|
void main(void)
|
|
{
|
|
char array[N];
|
|
unsigned short loop_counter,
|
|
count = ZERO,
|
|
new_size_array = N - TWO;
|
|
printf("Enter char 20 chars: \n");
|
|
for (loop_counter = ZERO; loop_counter < N; loop_counter++)
|
|
{
|
|
scanf("%c", &array[loop_counter]);
|
|
}
|
|
|
|
for (loop_counter = ONE; loop_counter < new_size_array; loop_counter++)
|
|
{
|
|
count += Equality((int)array[loop_counter - ONE], (int)array[N - TWO]) *
|
|
Equality((int)array[loop_counter], (int)array[N - ONE]);
|
|
}
|
|
|
|
printf("Count: %hd\n", count);
|
|
|
|
} |