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.

Saturday, November 7, 2015

Funny C Program:Comparison Between Zhishen Lu Eating Steamed Bun v5 and v6

Acturally,I mentioned the difference between the 5th version and 6th one in Funny C Program:Zhishen Lu Eating Steamed Bun v6,so I am not going to repeat it.Let's discuss whether to assign values to every array element one by one through a loop statement or not.To find the reasonable explanation,I tested a few programs on C4droid.

//Testing program 1
#include <stdio.h>
int main(void)
{
int i,array[101]={0};
for(i=1;i<101;i++)
printf(" %d",array[i]);
return 0;
}

Following is the result.


Testing program 1


What?It depends on the scale of the array?
I was puzzled,and modified the code like testing program 2.

//Testing program 2
#include <stdio.h>
int main(void)
{
int i,array[3]={0};
for(i=0;i<3;i++)
printf(" %d",array[i]);
printf("\n");
return 0;
}

The result is following.


Testing program 2

It seemed that scale of the array did influence the result.To convince myself,testing program 3 came into being.Of course there were many other testing programs which I'm unable to show to you one after another.

//Testing program 3
#include <stdio.h>
int main(void)
{
int i,array[6]={0};
for(i=0;i<6;i++)
printf(" %d",array[i]);
printf("\n");
return 0;

}

And the result is following.


Testing program 3


As expected,not all the elements equal 0.
How can we explain it?Maybe it's because different compilers deal with "int a[10]={0};" variously,I think.To avoid mistakes,why not write a loop statement?I'm sorry that I didn't want to write another circle statement,so the mistake appeared.But I feel lucky for learning so much from it.
Of course,if you understand the exact explanation of this mistake,welcome to post a comment or email me at jimzhou001@gmail.com.Thank you!

Friday, November 6, 2015

Funny C Program:Zhishen Lu Eating Steamed Bun v6

Because of my puzzle,I'd like to share the modified code straightly,hoping you can explain why to alter like this.

//Zhishen Lu Eating Steamed Bun v6
#include <stdio.h>
int main(void)
{
int monk[101],count=0,steabun=0,i;//Easy to count.monk[1]~monk[100] for the flower monk Zhishen Lu and other 99 monks,count to number off,steabun for the number of steamed buns already divided and i as a simple counter.
for (i=1;i<101;i++)
monk[i]=0;//Initialization of the array.Value 0 means they will participate in numbering off.
i=0;
while (steabun<99)
{
if (++i<=100&&!monk[i])//Make sure that the monk hasn't got a steamed bun yet
{
if (++count%5==0)
{
monk[i]=1;//Get a steamed bun
steabun++;
}
}
else if (i>100)
i=0;//Make sure to mumber off continuously
}
for(i=1;monk[i];i++) ;//Do you know why ";" exists?
printf("The position number of Zhishen Lu is %d.\n",i);
return 0;
}

Find any differences between the 6th version and the 5th one?
Right,we initialised monk[1] to monk[100] through a "for" circle in the latest version while not when defining the array.
But I learned from the textbook that an array could be defined and initialized like this:"int a[10]={0};"which means every array element equals 0.Then what's wrong with the 5th version?If interested,pay attention to my posts.