Wednesday, November 11, 2015

Funny C Program:Calculating the Factorial of A Large Number

Maybe you will think,"how simple to calculate the factorial of a number!"And you just write a program like the following.

//Calculating the Factorial of A Large Number v1
#include <stdio.h>
unsigned long long int Factorial(unsigned long long int j);
int main(void)
{
unsigned long long int i;
printf("Input a nonnegative integer:");
scanf("%llu",&i);
printf("%llu!=%llu\n",i,Factorial(i));
return 0;
}
unsigned long long int Factorial(unsigned long long int j)
{
unsigned long long int k,result=1;
for(k=2;k<=j;k++)
result*=k;
return result;
}

Since we use "%llu" to control the formats of these numbers,we have acturally considered the precision.So there is priviously a problem,"as i becomes larger and larger,can the factorial of i be printed accurately?"Of course no,for numbers are stored in a computer with specific ranges the compiler allows.Here is an example of wrong result.

Calculating the factorial of a number

It's obvious that last digit of 25! should be 0,but in this result not.
Then,are there any ways to avoid accuracy losses?
Remenber how we expressed a number in the program Wenquxing Guessing Game?Through an array with enough elements,we can easily store a large number and print it.Then the improved code is following.

//Calculating the Factorial of A Large Number v2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit<=N)
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
else
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
}

Through this program,I did calculate some factorials successfully.But it seemed that the program didn't run as expected when I input a number out of range.For example,when I input the number 100,the result was beyond expection.

Calculating the Factorial of A Large Number v2

How can we explain the wrong result?To find the bug,I read the code carefully.And finally I
 give the 3rd version.

//Calculating the Factorial of A Large Number v3
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
//N means the maximal number of digits allowed for the value of a factorial
void OutputFactorial(int j);
int main(void)
{
int i;
do
printf("Input a nonnegative integer less than or equal to %d(maximum of int type):",INT_MAX);
while (scanf("%d",&i)!=1||i<0);
OutputFactorial(i);
printf("\n");
return 0;
}
void OutputFactorial(int j)
{
int k,m,carry=0,digit=1,array[N] ;
array[0]=1;
for(k=2;k<=j;k++)
{
for(m=0;m<digit;m++)
{
array[m]=array[m]*k+carry;
carry=array[m]/10;
array[m]%=10;
}
while (carry>0&&digit<N)
{
array[digit++]=carry%10;
carry/=10;
}
}
if (digit>=N&&carry>0)
printf("Sorry.The factorial has more than %d digits.It's not intended to output the result.",N);
else
{
printf("%d!=",j);
while (digit>=1)
printf("%d",array[--digit]);
}
}


Do you know why I modified the program like this? I defined an array with N elements in the function OutputFactorial(j) but didn't check whether the subscript was out of range in the loop of 2nd version.So trouble came.
Although the 3rd version is improved,it remains some problems.For instance,if you input a character,not a number,then the program will fall into an endless loop.Actually,it's a problem caused by buffer.What's worse,theoretically the program could calculate the factorial of a number as large as INT_MAX,but the efficiency is extremely low so it's impossible for us to wait.In the meantime,It's assumed that the number inputted is not larger than INT_MAX,so the robustness of this program needs to be improved.
Do you have any ideas better than mine?Welcome to share!

Singles' Day:What Has It Brought Us?

Before the main body,I'd like to ask you a question,"do you know the festival Singles' Day?"If not,it might has brought you nothing.Even if you know something about it,it might hasn't brought you anything,either.Let's begin with the origin of Singles' Day.
It's commonly accepted that Singles's Day derived from campus culture of Nanjing University in China.There was a dormitory with four single men,who talked about how to get rid of the single state every night during a period.When talking,they imagined to organize an activity on the base of upcoming November 11th,which they called Singles's Day later on.From then on,this festival was widely spread and became more and more popular.Why they chose November 11th?Because there were four "1" on the day 11/11,which were like singles.
As time went by,the Singles' Day has been celebrated by more and more people,but Chinese only.
Firstly,the young hold parties for ridding themselves of single states,or to make new friends preparing for saying goodbye to singleness.Young people show their happy desires.
Secondly,so many shopping websites discount to promote sales on Singles' Day,since Taobao and Tmall being the first in 2009.It now has become the largest shopping day in the world.
There is no doubt that we benefit a lot from the Singles' Day.However,problems always come along with the benefits,so there are some problems meanwhile.
This festival,with strong atmosphere getting rid of singleness,makes some singles feel more alone.On the other hand,crazy shopping activities have some bad influences,such as busy work for couriers.What's worth,it has serious impacts to some other countries.It's reported that there is a shortage of milk powder in Australia because of the panic buying of many Chinese,which makes some local parents can't buy milk powder for their children.And there are some other problems,too.
What's your opinion to the Singles' Day?What has it brought us?Welcome to share your thoughts with others.

Monday, November 9, 2015

Funny C Program:Wenquxing Guessing Game

Wenquxing guessing game,the rules of which are simple,is not very easy for us.
In this game,computer gives a random number with four digits,each of which is different from the others,and we assumpt the top one can be 0.Then the player guesses it until he or she guesses rightly or guesses wrongly within a specific number of times determined by your inputing.Of course,there are hints after every guessing,which are like this,"xAyB".x is the number of digits guessed with right digits and positions,and there is/are y right digit(s) with wrong position(s).Do you want to write this game by yourself and play it?At least I do.

//Wenquxing Guessing Game
#include <stdio.h>
#include <time.h>
void MakeDigit(int a[4]);//Computer gives a random number
void InputDigit(int b[4]);//Player input a number
int IsRightPosition(int a[4],int b[4]);//Calculate how many correct digits guessed with right positions
int IsRightDigit(int a[4],int b[4]);//Calculate how many correct digits guessed with whether right or wrong positions
int main(void)
{
int level,count=0,rightPosition,rightDigit,a[4],b[4];
MakeDigit(a);
do
{
printf("How many times do you want to guess?Please input a positive integer:");
scanf("%d",&level);
}
while (level<=0);
do
{
printf("No.%d of %d times\n",count,level);
InputDigit(b);
count++;
rightPosition=IsRightPosition(a,b);
rightDigit=IsRightDigit(a,b)-rightPosition;
printf("%dA%dB\n",rightPosition,rightDigit);
}
while (rightPosition<4&&count<level );
if (rightPosition==4)
printf("Congratulations.You guessed the right number at No.%d.\n",count);
else
printf("Sorry.You didn't guess the right number.See you next time!\n");
printf("Correct answer is:");
for(count=0;count<4;count++)
printf("%d",a[count]);
return 0;
}
void MakeDigit(int a[4])
{
int i,j;
srand(time(NULL));
for (i=0;i<4;i++)
{
Repeata:
a[i]=rand()%10;
for (j=0;j<i;j++)
if (a[j]==a[i])
goto Repeata;
}
}
void InputDigit(int b[4])
{
int k,m,n;
do
{
Repeatb:
printf("Input your guess with four digits,each of which should be different from the others(top digit 0 allowed):");
scanf("%d",&k);
for (m=3;m>=0;m--)
{
b[m]=k%10;
k/=10;
for (n=3;n>m;n--)
if (b[n]==b[m])
goto Repeatb;
}
}
while (k<0||k>=10000);
}
int IsRightPosition(int a[4],int b[4])
{
int r,s=0;
for (r=0;r<4;r++)
if (a[r]==b[r])
s++;
return s;
}
int IsRightDigit(int a[4],int b[4])
{
int x,y,z=0;
for (x=0;x<4;x++)
for (y=0;y<4;y++)
if (a[x]==b[y])
z++;
return z;
}

Seems simple,right?Have fun with it!

Sunday, November 8, 2015

Funny C Program:Summary of Zhishen Lu Eating Steamed Bun

Hello,my friends.Through the series of "Funny C Program:Zhishen Lu Eating Steamed Bun",do you gain anything?I'm going to summarize in this article.
Firstly,I feel fortunate to have been so interested in solving this problem through C language.As the saying goes,"interest is the best teacher."Strong interest made me insist on thinking about this problem.And I am very happy to have finished this series.
Secondly,I did learn a lot from these small about 20-line programs.I enjoyed the process in spite of many mistakes and barriers."To find a problem is far more important than to solve the problem,"as Einstein said.And I had great senses of achievement after every step forward.
Thirdly,the key of this problem lies in how to write the loop control statements,for which I struggled many times.I knew the importance of problem understanding and algorithm design from this living example.
Last but not least,is "int a[10]={0};" valid or not?I'm expecting you readers to discuss it with me.Thanks.
Oh,no.It's necessary for me to say something more about this problem after discussing.The thought of it comes from Josephus problem,which is very famous.As for more details about it,you can search on the Internet.