Wednesday, 2 April 2014
Friday, 28 March 2014
Induction v/s Deduction
Induce and Deduce
Induce : a form of reasoning under which a conclusion is drawn on the basis of a large number of positive examples. For example, after seeing a large number of cows, we conclude a cow has four legs, white colour, two horns symmetrically placed on the head etc. Inductive reasoning, though usually leads to correct conclusions, yet the conclusions may not be irrefutable.
Deduce/Infer : to derive as a conclusion from something known or assumed. It is an irrefutable form of reasoning. By irrefutable form of reasoning we mean that the conclusion arrived at through deductive (i.e., any irrefutable) reasoning process is always correct, if the hypotheses (or given facts) are correct. This is the form of reasoning which is dominantly applied in Mathematics.
Induce : a form of reasoning under which a conclusion is drawn on the basis of a large number of positive examples. For example, after seeing a large number of cows, we conclude a cow has four legs, white colour, two horns symmetrically placed on the head etc. Inductive reasoning, though usually leads to correct conclusions, yet the conclusions may not be irrefutable.
Deduce/Infer : to derive as a conclusion from something known or assumed. It is an irrefutable form of reasoning. By irrefutable form of reasoning we mean that the conclusion arrived at through deductive (i.e., any irrefutable) reasoning process is always correct, if the hypotheses (or given facts) are correct. This is the form of reasoning which is dominantly applied in Mathematics.
String Search and Replacement function in C Language
/* A case-sensitive string replacement program
The Basic principle behind this program depends upon three cases
Case 1: If replacement string equals to 'find' string in length
then no. more space is required so character are simply replaced
Case 2: If replacement string is less than 'find' string in length
then some is created after replacing the characters
so another loop is required for removing that holes
Case 3: If replacement string is greater than 'find' string in length
then more space is required to be created between characters for
replacement string, after that replacement is done.
*/
//Note: This program can be modified by using list and stack
// to make it more efficient, robust and flexible.
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* replace(char[],char[],char[]);
void main ()
{
char str[50]="Look Hook Nook";
clrscr();
printf("Before: %s",str);
printf("\nAfter: %s", replace(str,"k","am"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"Loo","S"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"Hoo","Dre"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"ooam","ame"));
getch();
}
// This function can work with max. of 100 occurence of 'to find' string
// due to static declaration of memory limit.
char* replace(char str[],char find[],char rep[]){
int occ[100],i,j,loc,c=0,next,start;
int lens,lenf,lenr;
int diff,noOfBytes;
lens=strlen(str);
lenf=strlen(find);
lenr=strlen(rep);
//find the no. of occurence of 'find' string
for(i=0;i<lens;i++) {
loc=i;
for(j=0;j<lenf;j++){
if(str[i+j]!=find[j]){
loc=-1;
break;
}
}
if(loc!=-1){
occ[c]=loc;
c++;
}
}
//check difference between length of 'find' and 'replace' strings
diff=lenr-lenf;
//Case 1: When length of both strings is equal
if(diff==0){
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j]=rep[j];
}
}
}
//Case 2: When length of replace string < length of find string
else if(diff<0){
//replace characters
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j]=rep[j];
}
}
//fill holes
for(i=0;i<c;i++){
next=lens+diff*c;
if(i<c-1) next=occ[i+1]-1;
for(j=occ[i]+lenr-i;j<=next;j++){
str[j]=str[j-diff*(i+1)];
}
}
//append null character at end of string
for(i=lens-1;i>=lens+diff*c;i--){
str[i]='\0';
}
}
//Case 3: When length of replace string > length of find string
else if(diff>0){
//make space
next=lens-1;
for(i=c-1;i>=0;i--){
for(j=next;j>=occ[i];j--){
str[j+diff*c-c+i+1]=str[j];
}
next=occ[i];
}
//replace characters
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j+i]=rep[j];
}
}
}
return str; //return result
} //End of function
//END OF PROGRAM
The Basic principle behind this program depends upon three cases
Case 1: If replacement string equals to 'find' string in length
then no. more space is required so character are simply replaced
Case 2: If replacement string is less than 'find' string in length
then some is created after replacing the characters
so another loop is required for removing that holes
Case 3: If replacement string is greater than 'find' string in length
then more space is required to be created between characters for
replacement string, after that replacement is done.
*/
//Note: This program can be modified by using list and stack
// to make it more efficient, robust and flexible.
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* replace(char[],char[],char[]);
void main ()
{
char str[50]="Look Hook Nook";
clrscr();
printf("Before: %s",str);
printf("\nAfter: %s", replace(str,"k","am"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"Loo","S"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"Hoo","Dre"));
printf("\nBefore: %s",str);
printf("\nAfter: %s", replace(str,"ooam","ame"));
getch();
}
// This function can work with max. of 100 occurence of 'to find' string
// due to static declaration of memory limit.
char* replace(char str[],char find[],char rep[]){
int occ[100],i,j,loc,c=0,next,start;
int lens,lenf,lenr;
int diff,noOfBytes;
lens=strlen(str);
lenf=strlen(find);
lenr=strlen(rep);
//find the no. of occurence of 'find' string
for(i=0;i<lens;i++) {
loc=i;
for(j=0;j<lenf;j++){
if(str[i+j]!=find[j]){
loc=-1;
break;
}
}
if(loc!=-1){
occ[c]=loc;
c++;
}
}
//check difference between length of 'find' and 'replace' strings
diff=lenr-lenf;
//Case 1: When length of both strings is equal
if(diff==0){
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j]=rep[j];
}
}
}
//Case 2: When length of replace string < length of find string
else if(diff<0){
//replace characters
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j]=rep[j];
}
}
//fill holes
for(i=0;i<c;i++){
next=lens+diff*c;
if(i<c-1) next=occ[i+1]-1;
for(j=occ[i]+lenr-i;j<=next;j++){
str[j]=str[j-diff*(i+1)];
}
}
//append null character at end of string
for(i=lens-1;i>=lens+diff*c;i--){
str[i]='\0';
}
}
//Case 3: When length of replace string > length of find string
else if(diff>0){
//make space
next=lens-1;
for(i=c-1;i>=0;i--){
for(j=next;j>=occ[i];j--){
str[j+diff*c-c+i+1]=str[j];
}
next=occ[i];
}
//replace characters
for(i=0;i<c;i++){
for(j=0;j<lenr;j++){
str[occ[i]+j+i]=rep[j];
}
}
}
return str; //return result
} //End of function
//END OF PROGRAM
Thursday, 27 March 2014
C Progamm to convert binary number into Octal System
/* To convert a number from binary to octal system,
no. of digit in binary no. should be multiple of three(3),
if not, then append 0's at front of the number,
e.g. 1001101 converted 001 001 101
then these group are converted to number as
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
long binaryToOctal(int[],int);
void main(){
int b[28],c,i;
long dec;
clrscr();
//Enter binary no.
printf("Enter a binary no.(max.27 digits longs):");
c=0;
while(c<28){
b[c]=getch();
if(b[c]==13) break;
b[c]=b[c]-'0';
if(b[c]==0||b[c]==1) {
printf("%d",b[c]);
c++;
}
}
//Octal no.
printf("\nOctal equivalent of binary no. ");
for(i=0;i<c;i++)
printf("%d",b[i]);
dec=binaryToOctal(b,c); //Conversion
printf(". is %ld",dec);//Result
getch();
}
/*This function receives an array of binary digits
and convert them into a octal number */
/* Note: binary number's digit (e.g. 1001101) from right to left are stored in array from 0 to n-1*/
long binaryToOctal(int bn[],int len){
int i,position; //i for position of binary digit, position indicate position of octal-digit under conversion
long oct,digit; //oct for octal number, digit for octal-digit under conversion
i=len-1,oct=0;
position=0;
while(i>=0) {
//Group of 3 binary digits are processed from left
digit=bn[i--]; // 1st one from left
if(i>=0) digit=2*bn[i--]+digit; // 2nd One from left(if exists)
if(i>=0) digit=4*bn[i--]+digit; // 3rd One from left(if exists)
oct=digit*pow(10.0,position)+oct; // Octal no.
position++; // next digit to right
}
return oct; //return octal no.
}
//end of function
//End of Programm
no. of digit in binary no. should be multiple of three(3),
if not, then append 0's at front of the number,
e.g. 1001101 converted 001 001 101
then these group are converted to number as
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
long binaryToOctal(int[],int);
void main(){
int b[28],c,i;
long dec;
clrscr();
//Enter binary no.
printf("Enter a binary no.(max.27 digits longs):");
c=0;
while(c<28){
b[c]=getch();
if(b[c]==13) break;
b[c]=b[c]-'0';
if(b[c]==0||b[c]==1) {
printf("%d",b[c]);
c++;
}
}
//Octal no.
printf("\nOctal equivalent of binary no. ");
for(i=0;i<c;i++)
printf("%d",b[i]);
dec=binaryToOctal(b,c); //Conversion
printf(". is %ld",dec);//Result
getch();
}
/*This function receives an array of binary digits
and convert them into a octal number */
/* Note: binary number's digit (e.g. 1001101) from right to left are stored in array from 0 to n-1*/
long binaryToOctal(int bn[],int len){
int i,position; //i for position of binary digit, position indicate position of octal-digit under conversion
long oct,digit; //oct for octal number, digit for octal-digit under conversion
i=len-1,oct=0;
position=0;
while(i>=0) {
//Group of 3 binary digits are processed from left
digit=bn[i--]; // 1st one from left
if(i>=0) digit=2*bn[i--]+digit; // 2nd One from left(if exists)
if(i>=0) digit=4*bn[i--]+digit; // 3rd One from left(if exists)
oct=digit*pow(10.0,position)+oct; // Octal no.
position++; // next digit to right
}
return oct; //return octal no.
}
//end of function
//End of Programm
C program to find out perfect numbers from 1 and 50
/*Perfect no. is a +ve integer that is equal to the sum of its +ve divisors
e.g. 28 is divible by 1,2,4,7,14 (excluding number itself)
1+2+4+7+14=28
*/
#include<stdio.h>
#include<conio.h>
void print_perfect(int,int); //function declration
void main()
{
clrscr(); //Clear Screen
print_perfect(1,50); //This Function print perfects number
getch();
}
void print_perfect(int start,int end)
{
int div,sum; //div for divisor, s for summation
int num=start; //num for number under checking
while(start<=end) //loop from 1 to 50
{
div=1;sum=0; //factor starts from 1,summation from zero
while(div<num) //divisor must be less than number itself
{
if(num%div==0) //it checks for divison
{
sum=sum+div; //if it remainder is zero, divisor is added
}
div++; //next divisor
}
if(sum==num) printf("perfect no =%d\n",num); //if summation equal to number itself, it is perfect no. so print it
num++; // next number
}
}//end of function
//End of the program
e.g. 28 is divible by 1,2,4,7,14 (excluding number itself)
1+2+4+7+14=28
*/
#include<stdio.h>
#include<conio.h>
void print_perfect(int,int); //function declration
void main()
{
clrscr(); //Clear Screen
print_perfect(1,50); //This Function print perfects number
getch();
}
void print_perfect(int start,int end)
{
int div,sum; //div for divisor, s for summation
int num=start; //num for number under checking
while(start<=end) //loop from 1 to 50
{
div=1;sum=0; //factor starts from 1,summation from zero
while(div<num) //divisor must be less than number itself
{
if(num%div==0) //it checks for divison
{
sum=sum+div; //if it remainder is zero, divisor is added
}
div++; //next divisor
}
if(sum==num) printf("perfect no =%d\n",num); //if summation equal to number itself, it is perfect no. so print it
num++; // next number
}
}//end of function
//End of the program
Subscribe to:
Posts (Atom)