Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 33 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
33
Dung lượng
592 KB
Nội dung
Programming Concepts (Part A) ENGR 10 Introduction to Engineering What is a program? WHITE CAKE RECIPE It’s like a recipe! Preheat oven to 350 degrees F (175 degrees C) Grease and flour a 9x9 inch pan In a medium bowl, cream together the sugar and butter Beat in the eggs Stir in the vanilla Combine flour and baking powder, add to the creamed mixture and mix well Stir in the milk until batter is smooth Pour or spoon batter into the prepared pan Bake for 30 to 40 minutes in the preheated oven In Summary… A computer program is an ordered list of instructions A math example… y = [ × ( a × a + 7) ] / b + a2 a2 + × (a2 + 7) = 3a2 + 21 (3a2 + 21) / b (3a2 + 21) / b + Sequential Solving following Math conventions for correct answer y = [ × ( a × a + 7) ] / b + Consider the sequential execution of the above expressions to find the value of y: • • • • • p = a x a; q = p + 7; r = x q; s = r / b; y = s + 4; An Empty EasyC program Your program goes here Your program goes here Flow chart C program Logic flow An instruction in C C program Variables • A “variable” is a place where we keep a value A = 10 ; // The value 10 is stored in A • Any statement after the sign “//” is NOT part of the program It is a comment made by the programmer WSalary = 800 ; // Any statement can go here • A semicolon is required at the end of each C instruction Variables, cont Every variable in the program needs to be declared in the beginning of the program Declaration of the variable tells the program its name and its type int speed ; The word “int” indicates that the variable ‘speed’ is an integer variable Commonly Used Variable Types Variable Type Description Range Int Stores integer values EX: 5, -3200 -32,768 to +32,767 Long Stores integer values with extended range EX: 56, 6000, -4,234,128 Stores values with decimal point EX: 1.245, -4.2341 Stores characters* EX: A, B, @, # -2,147,483,648 to +2,147,483,647 float char [-10^+38, -10^-38] [10^-38, 10^+38] _ • Global Variable: This variable is accessible from anywhere within your program • Local Variable: This variable is only accessible from a “local area” within your program (“functions” will be discussed later) IF-ELSE Statement • IF-ELSE statement should be used where there are only two possible cases Note: ( ) not { } If (score 22) { A=B; B = A; } else { B = A; A = B; } (A) A=12, B=21 (B) A=9, B=12 (C) A= 9, B=9 (D) A=12, B=12 (E) A=12, B=9 WHILE Statement • In C, the WHILE statement is useful for repeating a set of instructions Suppose we have to add the first 50 positive integers 1+2+3+4+……………………+48+49+50 First Approach: Use a single statement: int SUM ; // integer variable for the result SUM = 1+2+3+……………… +48+49+50; Second Approach: int SUM ; //variable SUM is declared as integer SUM = ; //SUM is assigned the value SUM = SUM + ; // Now SUM = (0 + 1) = SUM = SUM + ; // SUM = (1 + 2) = SUM = SUM + ; // SUM = (3 + 3) = SUM = SUM + 49 ; //SUM = (1176 + 49) = 1225 SUM = SUM + 50; //SUM = (1225 + 50) = 1275 Third approach: Use “while” statement This condition is checked first If it is true, the block of instructions enclosed by the curly brackets { } is executed This block of instructions is executed repeatedly until the condition is not true While Statement - Example • To find factorial of a number N What is a factorial? Factorial of a number N is N! = 1x2x3x………………x(N-1)xN • 1! = Factorial (1) = • 5! = Factorial (5) = 1x2x3x4x5 = 120 • 7! = Factorial (7) = 1x2x3x4x5x6x7 = 5040 Q3: What is the final value of A int A; int i; A = 0; i = 0; while (i < 3) { A = A + i; i = i + 1; } (A) (B) (C) (D) (E) 10 Solution Initially i = 0, A = First iteration: condition 0 Log in as a guest • -> Enroll key: engineering Infinite Loop • In the previous examples we have employed condition checking in order to control the flow of execution – We have made the loop to repeat only a finite number of times • We can also make the loop to repeat infinite number of times Next example: • The status of a bumper switch is continuously monitored and depending on its value the movement of the robot is decided Infinite Loop The status of a bumper switch (via Digital input port #5) is repeatedly read needlessly When a hit (status==1) is detected, the robot is stopped while (1==1) { status = GetDigitalInput(5); // read bumper if (status ==1 ) // bumper hits something { SetMotor(2,127);} // Stop the robot else { SetMotor(2,0); } } // Move forward Infinite Loop A common mistake – reading sensor OUTSIDE of the loop status = GetDigitalInput(5); // read bumper while (1==1) { if (status ==1 ) // bumper hits something { SetMotor(2,127);} // Stop the robot else { SetMotor(2,0); } } // Move forward IMPORTANT NOTE Review this example carefully ... equal to ” statement • A = 10; // means assign 10 to A • A = B; // means assign the value of B to A • A = A+1; //means assign the value A+1 // back to A, i.e., increase A by Assignment Operator:... Example • To find factorial of a number N What is a factorial? Factorial of a number N is N! = 1x2x3x………………x(N-1)xN • 1! = Factorial (1) = • 5! = Factorial (5) = 1x2x3x4x5 = 120 • 7! = Factorial... integer A = 10; // value 10 is assigned to variable A B = (24+16)/2; // 20 is assigned to variable B A = A + 15; // value of (A+15) is first evaluated and then assigned to A So now A= (10+ 15)=25