码迷,mamicode.com
首页 > 其他好文 > 详细

Let us learn C in Code <11> flowchart while

时间:2014-05-04 09:01:20      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:c   flowchart   while loop   

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

bubuko.com,布布扣

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,....

bubuko.com,布布扣

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!

Let us learn C in Code <11> flowchart while,布布扣,bubuko.com

Let us learn C in Code <11> flowchart while

标签:c   flowchart   while loop   

原文地址:http://blog.csdn.net/johnnyhan/article/details/24845183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!