Created
May 12, 2012 11:59
-
-
Save sinannar/2666172 to your computer and use it in GitHub Desktop.
GYTE 11/05/2012 bil102 Lab 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BIL 108 - Computer Programming | |
LW – 10 | |
11.05.2012 | |
In this week we will write recursive functions working on integers and sets represented by integer arrays. | |
******************************************************************************************************************************************* | |
PART 1(2 Pts) Write a recursive function “isElement” which determines if a given integer is an element of a given set. | |
The set will be represented by an integer array. | |
******************************************************************************************************************************************* | |
PART2 (1Pts) Write a recursive function “isDisjoint” which determines if given 2 sets are disjoint. | |
The sets will be represented by integer arrays. | |
******************************************************************************************************************************************* | |
PART3 (1Pts) Write a recursive function ‘getNumOfDigits’ which returns the number of digits of a given integer. | |
******************************************************************************************************************************************* | |
PART4 (1Pts) Write a recursive function “intRecReverser” which returns the reversed form of a given positive integer. | |
The function will require the number of digits of the integer, so it will have the following prototype: | |
int intRecReverser(int number, int numOfDig); | |
Because this function needs extra information other than required to solve the problem, | |
it is not good function from the perspective of the caller. Similar cases happen frequently when working with recursive functions. | |
Wrapper functions which call the recursive functions with proper arguments and provide a better interface | |
to the user are employed in such cases. | |
Write a wrapper function ‘intReverser’ with the following prototype: | |
int intReverser(int number); | |
which obtains the number of digits from ‘getNumOfDigits’ and calls intRecReverser with proper parameters. | |
******************************************************************************************************************************************* | |
BONUS (1 Pts) Write a recursive function “isSet” which determines if a given integer array can represent a set. | |
As you know, each element can occur only once in a set. Therefore, your function will check any duplication. | |
******************************************************************************************************************************************* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
FILE NAME : lw10.c | |
CREATED by : Sinan NAR | |
NOTE : All method written recursively,if its not possible,write wrapper method | |
NOTE2 : makefile is attached if you do not know make file usage | |
compile it in your way.It include math library | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
int isElement(int *arr,int size,int target); | |
int isDisjoint(int*arr1,int size1,int*arr2,int size2); | |
int getNumOfDigits(int num); | |
int intRecReverser(int number, int numOfDig); | |
int intReverser(int number); | |
int isSet(int *arr,int size); | |
int main(void) | |
{ | |
int array[10]={0,1,2,3,4,5,6,7,8,9}; | |
int array2[10]={10,11,12,13,14,15,16,17,18,19}; | |
int array3[10]={11,12,13,14,15,16,17,18,19,20}; | |
int array4[10]={11,11,13,14,15,16,17,18,19,20}; | |
int array5[10]={11,13,13,14,15,16,17,18,19,20}; | |
printf("\n\n\tisElement TEST STATEMENTs\n"); | |
printf("isElement Test...printout should be 1...>% d \n",isElement(array,10,7)); | |
printf("isElement Test...printout should be 0...>% d \n",isElement(array,10,11)); | |
printf("\n\n\tisDisjoint TEST STATEMENTs\n"); | |
printf("isDisjoint Test...printfout should be 1...>%d \n",isDisjoint(array,10,array2,10)); | |
printf("isDisjoint Test...printfout should be 0...>%d \n",isDisjoint(array3,10,array2,10)); | |
printf("\n\n\tgetNumOfDigits TEST STATEMENTs\n"); | |
printf("getNumOfDigits Test...printfout should be 1...>%d \n",getNumOfDigits(1)); | |
printf("getNumOfDigits Test...printfout should be 2...>%d \n",getNumOfDigits(11)); | |
printf("getNumOfDigits Test...printfout should be 3...>%d \n",getNumOfDigits(111)); | |
printf("getNumOfDigits Test...printfout should be 4...>%d \n",getNumOfDigits(1111)); | |
printf("getNumOfDigits Test...printfout should be 5...>%d \n",getNumOfDigits(11111)); | |
printf("\n\n\tintReverser TEST STATEMENTs\n"); | |
printf("intReverser Test...printfout should be 54321...>%d \n",intReverser(12345)); | |
printf("intReverser Test...printfout should be 654321...>%d \n",intReverser(123456)); | |
printf("intReverser Test...printfout should be 7654321...>%d \n",intReverser(1234567)); | |
printf("intReverser Test...printfout should be 87654321...>%d \n",intReverser(12345678)); | |
printf("\n\n\tisSet TEST STATEMENTs\n"); | |
printf("isSet Test...printfout should be 1...>%d \n",isSet(array,10) ); | |
printf("isSet Test...printfout should be 1...>%d \n",isSet(array2,10) ); | |
printf("isSet Test...printfout should be 1...>%d \n",isSet(array3,10) ); | |
printf("isSet Test...printfout should be 0...>%d \n",isSet(array4,10) ); | |
printf("isSet Test...printfout should be 0...>%d \n",isSet(array5,10) ); | |
return 0; | |
} | |
/* | |
Check target found in array that given with size recursively | |
return 1 or 0 | |
*/ | |
int isElement(int *arr,int size,int target) | |
{ | |
if(size == 0) | |
return 0; | |
if(arr[0]==target) | |
return 1; | |
return isElement(&arr[1],size-1,target); | |
} | |
/* | |
Check two array that given with their sizes,are they disjoint or not recursively | |
return 1 or 0 | |
*/ | |
int isDisjoint(int*arr1,int size1,int*arr2,int size2) | |
{ | |
if(size1 == 0) | |
return 1; | |
if(isElement(arr2,size2,arr1[0]) == 1) | |
return 0; | |
return isDisjoint(&arr1[1],size1-1,arr2,size2); | |
} | |
/* | |
Calculate the number of digits of positive integer recursively | |
return number of digits | |
*/ | |
int getNumOfDigits(int num) | |
{ | |
if(num<10) | |
return 1; | |
return 1 + getNumOfDigits(num/10); | |
} | |
/* | |
Find the reverse of any positive integer number | |
return reverse of number | |
NOTE:that is wrapper method,it call a recursive method | |
*/ | |
int intReverser(int number) | |
{ | |
return intRecReverser(number,getNumOfDigits(number)); | |
} | |
/* | |
Find the reverse of any positive integer number recursively | |
return reverse of number | |
*/ | |
int intRecReverser(int number, int numOfDig) | |
{ | |
if(number < 10) | |
return number; | |
return (number%10)*pow(10,numOfDig-1)+intRecReverser(number/10,numOfDig-1); | |
} | |
/* | |
Check arr that given with size is set or not recursively | |
return 1 or 0 | |
*/ | |
int isSet(int *arr,int size) | |
{ | |
if(size == 0 || size == 1) | |
return 1; | |
return (isElement(&arr[1],size,arr[0]) == 0 && | |
isSet(&arr[1],size-1) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC = gcc | |
ALL : main | |
$ clear | |
$ ./main | |
main: main.o | |
$ (CC) -o main main.o -lm | |
main.o: main.c | |
$ (CC) -c -ansi -pedantic-errors main.c | |
CLR: main main.o | |
$ rm main main.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/* pow dont needed for reversing */
int intReverser(int number){
return intRecReverser(number,0);
}
int intRecReverser(int number, int reversed){
if(number == 0)return reversed;
return intRecReverser(number/10, reversed*10+number%10);
}