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.
Sunday, November 8, 2015
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.
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.
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.
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!
#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.
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.
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.
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.
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.
Funny C Program:Zhishen Lu Eating Steamed Bun v5
"If the two conditions of judgement mentioned in the previous version are combined together,will the problem be solved?"I asked myself.Then I tried to make it again and again.
The process was tough.You might notice that in the previous version,expressions in brackets are both in contrast to the right conditions to continuously number off.As I tried to put them together,using "&&" or "||" was a thorny problem.I tried many times,but still in vain.Eventually,I noticed that the right condition is very certain.Why not number off directly as the expression meets the condition?As to other cases,consider them expectly.
Here comes the 5th version of this program.
//Zhishen Lu Eating Steamed Bun v5
#include <stdio.h>
int main(void)
{
int monk[101]={0},count=0,steabun=0,i=0;//Easy to count.monk[1]~monk[100] for the flower monk Zhishen Lu and other 99 monks,value 0 means they will participate in numbering off.count to number off,steabun for the number of steamed buns already divided and i as a simple counter.
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;
}
How about this version?Seems perfect?No!
The screen showed nothing at all when running this program.
But I couldn't discover the errors by myself.I looked for help from a friend,and the problem was solved successfully.But it's not convictive enough,I thought.Want to know more about the next version?It's coming soon!
The process was tough.You might notice that in the previous version,expressions in brackets are both in contrast to the right conditions to continuously number off.As I tried to put them together,using "&&" or "||" was a thorny problem.I tried many times,but still in vain.Eventually,I noticed that the right condition is very certain.Why not number off directly as the expression meets the condition?As to other cases,consider them expectly.
Here comes the 5th version of this program.
//Zhishen Lu Eating Steamed Bun v5
#include <stdio.h>
int main(void)
{
int monk[101]={0},count=0,steabun=0,i=0;//Easy to count.monk[1]~monk[100] for the flower monk Zhishen Lu and other 99 monks,value 0 means they will participate in numbering off.count to number off,steabun for the number of steamed buns already divided and i as a simple counter.
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;
}
How about this version?Seems perfect?No!
The screen showed nothing at all when running this program.
But I couldn't discover the errors by myself.I looked for help from a friend,and the problem was solved successfully.But it's not convictive enough,I thought.Want to know more about the next version?It's coming soon!
Subscribe to:
Posts (Atom)


