-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflective_Pair.c
More file actions
99 lines (88 loc) · 3.42 KB
/
Reflective_Pair.c
File metadata and controls
99 lines (88 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
// declaration of recursive function for finding all the reflective pairs
int find_Reflective_Pair(int array[],int x,int numberOfElements);
////////////////////////////////////////
//////// MAIN OF THE PROGRAMM //////////
////////////////////////////////////////
int main()
{
system("PAUSE");
char choice='y';
do // "do while" for iterative use of this app
{
// the program asks from the user to enter the number of elements in the array
int numberOfElements,number,i;
printf("Please, give me a number of inputs that you want: ");
scanf("%d",&numberOfElements); // reading the user's choice
// dynamic memory allocation using malloc function
int *matrix = malloc(numberOfElements*sizeof(int));
// the user will be asked to enter the numbers he wants
printf("\nPlease start entering %d numbers:\n",numberOfElements);
for(i=0;i<numberOfElements;i++) // inserting the elements that the user enters to the array
{
printf("\nPlease enter input number %d:",i+1);
scanf("%d",&number);
matrix[i]=number;
}
/********************************/
/*******PRINTING THE ARRAY*******/
/********************************/
printf("\nThe following list includes the integers that you entered\n[");
for(i=0;i<numberOfElements;i++)
{
if(i!=(numberOfElements-1))
{
printf("%d ",matrix[i]);
}
else
{
printf("%d",matrix[i]);
}
}
printf("]\n");
system("\nPAUSE");
/***********************************************
CALLING RECURSIVE FUNCTION INSIDE MAIN &
PRINTING THE 1ST ELEMENT OF THE REFLECTIVE PAIR
************************************************/
printf("\nPosition of first element of reflective pair with ideal sum is = %d\n",find_Reflective_Pair(matrix,0,numberOfElements));
system("\nPAUSE");
printf("\nDo you want to re-run the program?\nPress y/Y for yes, or anything else to terminate.\n"); // asking the user for relaunching the program
choice=getch(); // reading the user's answer
if (choice=='y' || choice=='Y')
{
free(matrix); // free allocated memory
system("cls");
}
}while (choice=='Y' || choice=='y');
return 0;
}
/////////////////////////////
////////END OF MAIN//////////
/////////////////////////////
int find_Reflective_Pair(int array[],int x,int numberOfElements) // recursive function find_Reflective_Pair
{
int a = (numberOfElements/2);
if(x <= a)
{
/*************************************************************
CHECKING IF THE NUMBERS IN SYMMETRIC POSITIONS HAVE SUM EQUAL
TO THE 1ST ELEMENT OF THE ARRAY :)
**************************************************************/
if((array[x]+array[numberOfElements-x-1])==array[0])
{ // Case 1 : Reflective pair found
printf("A reflective pair was found with values %d and %d\n",array[x],array[numberOfElements-x-1]);
return x;
}
else
{ // Case 2 : Reflective pair didn't found
return find_Reflective_Pair(array,x+1,numberOfElements);
}
}
else
{ // Case 3 : There was no reflective pair in the array
printf("No ideal reflective pair was found. Sorry!!!\n");
return -1;
}
} /////END OF find_Reflective_Pair/////