Let us learn C in Code <11> flowchart while

So many days passed since the last C tutorial about the flowchart, this chapter we will go on  the flowchart and  take several examples about flowchart in C program.

1) now compare two integer numbers, then find the large one and print it

As this suppose ,we know that we must at least declare two variables and a temporary variable , we must compare the two integer variables then print the large on to the display.  if you forget the symbols are used to flowchart  or don't know how to express the flowchart you can linked here to review them 点击打开链接. Ok , let us go on

analyze this question, we can find here we can use the input or output symbol to input two integers and output the large one, compare symbol to compare which number is large ,start and end symbols (must) to express the start or end the program. Now, draw the flowchart as below


Ok, i think this flowchart really like the map in our daily life . if i go some strange place or find some place first time , i often check the destination in a map or use my gps in my phone to find it. Actually, the flowchart is the same , now let code it . Oh, i almost forget, the input function is a new function, we have not introduce it before. No matter, we have learnt the output function printf(). and the input function is called scanf(); but there are at least two parameters  one called  format  which represents what data type you want to input  , other parameter is an address which you want to hold the input variable, and we must focus on that the data type is the same with the format. For example scanf("%d",&integer_var);  here"%d" is the format express you must input the integer number, and the integer_var is just a integer variable you defined. and the& is an address which represent the input integer number storage to this address (surely this declared variable). 

Now , we have introduce the input and output functions ,they are printf() and scanf() , this functions are defined in a standard header named "stdio.h", as in our C program we can use the library function in our code just need add the header files at the top of our program like this #include <stdio.h> . The library includes some functions we often used in our program, in our program we just use these functions and surely we don't need to code them by ourselves ,we will introduce the header files lately , ok now we just finish this comparing program.

#include <stdio.h> 

main()
{
  int a = 0;
  int b = 0;
  printf("Please input one integer number\n");
  scanf("%d",&a);
  printf("Please input other integer number\n");
  scanf("%d",&b);
  if( a > b)
  {
    printf("Large one is %d\n",a);
  }
  else
  {
     printf("Large one is %d\n",b);
  }
}

 2) Find the Fibonacci sequence less than 500 (the first two numbers are 0 and 1).

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2, here F0=0,F1=1. Before draw this flowchart we just list the integer sequence as below

0,1,1,2,3,5,8,13,21,34,55,....


Here we have draw the flowchart of calculating the Fibonacci  sequences , but here is another important issue we have not learnt for the newbie C programmer , that is the loop. As the red arrow shows above diagram.  The loop just like our status of everyday  "sleep -> wake up ->breakfast -> work -> lunch -> work -> supper ->sleep -> wake up -> breakfast..." , Do something again and again. Here if you come along with the red arrow's direction, you will see the same sentence -0- equation -1- compare -2- print -3- assignment then again -0- -1- -2- -3-...  

In C program, we have two method to express the loops in C , the FIRST one is while(), the SECOND one do{} while(),the THIRD one is for(;;). Each of them can let you program do(repeat) something again and again. This chapter , i just introduce the while loop . for the do while and for loop ,  we will learn them lately in our code. 

For the basic while loops , while(condition){  block };  if the condition is true , the braces block will be executed. So here we just keep the condition is true, then the Fibonacci sequence will be printed repeatedly. Before we have learnt the if condition , the will condition just like the if condition.  So we just write like this while(fr < 500).

Let us code them as below

/* Fibonacci sequence less than 500
 *
 */

#include <stdio.h>

main()
{
  int fb = 0;
  int fa = 1;
  int fr = 0;
  fr = fb + ba;
  while( fr < 500)
  {
     printf("%d\n",fr);
     fb = fa;
     fa = fr;
     fr = fb + fa;
  }
}


Ok , time limited , May 1 Festival has passed, everybody need a rest. Finish here, good night!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值