Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

1.02 C Work Plans 1 The secret for success!. Why use a work plan? It is important to use a work plan to help meal preparation run smoothly. To implement.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Introduction to Programming
Functional Text I can interpret details in functional text to follow directions, solve problems, follow procedures, and answer questions.
NOTE SHEET 1 – Designing Controlled Experiments. Why?
Basics of Computer Programming Web Design Section 8-1.
Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
1.02 I Preparation Tasks List
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
By James Creamer Cupcake Recipe. Ingredients 2 1/4 cups all purpose flour 1 1/3 cups sugar 3 teaspoons baking powder 1/2 teaspoon salt 1/2 cup shortening.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Math Math Math Why do we study math? First of all, it’s all around us! First of all, it’s all around us!
By: Shaelyn Laurie How To Bake A Cake. Step 1: The Recipe Ingredients 2 cups (500 mL) sugar 4 eggs 2-1/2 cups (625 mL) all-purpose flour 1 cup (250 mL)
Transition Words Cake Recipe.
Cake Recipe. Ingredients Utensils 1 st Step Pre-Heat oven to 180 degrees Celsius.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Programming Concepts Part B Ping Hsu. Functions A function is a way to organize the program so that: – frequently used sets of instructions or – a set.
Baking Cookies English Club eats chocolate chip cookies!
RECIPE. NAME: Three chocolates cake. INGREDIENTS 3 cups 1 1/2 tsp 3/4 tsp 1 1/2 tsp 2 2/3 cups 1 cup + 2 Tb 1 cup + 2 Tb 1 cup + 2 Tb 5 large or 4 Jumbo.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Made by Doglyad Helen. Mafioso Chocolate Cake with La Famiglia Chocolate Frosting is just as rich as the Don himself. Surrounded by the best ingredients.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1.02 C Work Plans 1 The secret for success!. Why use a work plan? Helps meal preparation run smoothly Implement these steps: 1.02CWork Plans2 Steps 1-
Algorithms A sequence of instructions 1. Preheat oven to 350 degrees F (175 degrees C). Grease and flour a 9x9 inch pan or line a muffin pan with paper.
Dominika Lehocká.  2 cups all-purpose flour  1 cup white sugar  3/4 cup chocolate chips  1/2 cup unsweetened cocoa powder  1 teaspoon baking soda.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
How to make a BIG sponge cake! By Gina kinsman.. Ingredients! You will need: ½ cup of milk 2 teaspoons & table spoons of unsalted butter 8 large eggs.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
FROM SCRATCH UTENSILS NEEDED Original recipe makes 1 - 9x13 inch cake 9X13 Dish Pan Large Bowl.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Pineapple Upside Down Cake Katherine Babbini. Collect all your ingredients.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
FACTORS OF PRODUCTION What is it made of??. LEARNING TARGET I can explain the factors of production I can describe the production process I can identify.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
CPS120: Introduction to Computer Science Decision Making in Programs.
Introduction to design and analysis algorithm
COMP Loop Statements Yi Hong May 21, 2015.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
1 Introduction to design and analysis algorithm. 2.
Cooking Recipe Visualizers Chaitanya Kulkarni, Grader CSE5544.
Sour Cream Pound Cake. 2 cups butter, softened 3 cups white sugar 6 eggs 1 teaspoon vanilla extract.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Pierre Kerboriou Chocolate Fairy Cake Less than 30min preparation time.
How to Make a Cake Mrs. Janasik.
Sweet Potato Casserole
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
REPETITION CONTROL STRUCTURE
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Pink Cake.

Arrays, For loop While loop Do while loop
NOTE SHEET 1 – Designing Controlled Experiments
Building Java Programs
Computer Science Core Concepts
ICT Programming Lesson 3:
NOTE SHEET 2 – Designing Controlled Experiments
1.02 C Work Plans The secret for success! 1.02C Work Plans.
Presentation transcript:

Programming Concepts (Part A) Ping Hsu

What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9 inch pan. 3.In a medium bowl, cream together the sugar and butter. 4.Beat in the eggs. 5.Stir in the vanilla. 6.Combine flour and baking powder, add to the creamed mixture and mix well. 7.Stir in the milk until batter is smooth. Pour or spoon batter into the prepared pan. 8.Bake for 30 to 40 minutes in the preheated oven. A computer program is an ordered list of instructions. It’s like a recipe! In Summary…

A math example… y = [ 3 × ( a × a + 7) ] / b + 4 a2a2 a × (a 2 + 7) = 3a (3a ) / b (3a ) / b + 4 Sequential Solving following Math conventions for correct answer.

Consider the sequential execution of the above expressions to find the value of y: p = a x a; q = p + 7; r = 3 x q; s = r / b; y = s + 4; y = [ 3 × ( a × a + 7) ] / b + 4

An Empty EasyC program C program Flow chart Your program goes here.

C program Logic flow An instruction in C

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. int speed ; Declaration of the variable tells the program its name and its type The word “int” indicates that the variable ‘speed’ is an integer variable.

Commonly Used Variable Types Variable Type DescriptionRange Int Stores integer values EX: 5, ,768 to +32,767 Long Stores integer values with extended range EX: 56, 6000, -4,234,128 -2,147,483,648 to +2,147,483,647 float Stores values with decimal point EX: 1.245, [-10^+38, -10^-38] 0 [10^-38, 10^+38] char Stores characters* EX: A, # _

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 (will be discussed later).

Global Variable Declaration To declare a global variable, click on the tab “Global” in the Main program.

Global Variable Declaration (A close-up view)

Assignment Operator In C language “ = ” is an assignment operator, not an “ is 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 1.

Assignment Operator: Examples: int A; // a variable named A of type integer int B; // a variable named B of type 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 B = A + B ; // Now A = 25, B = (25+20)=45 A = B – 40; // Now A=(45-40)=5, B=45 A = A * B; // Now A=(5*45)=225, B=45 B = A / 9; // Now A=225, B=25

Q1: What is the value in B at the end of this program? int A; int B; A = 12; B = 15; A = A + (B/3) ; B = A + B – 7 ; (A)12, (B)15, (C)17, (D)20, (E)25

Decision Making The ability to make decision is the most basic form of intelligence. A linear (sequential) execution of instructions can only perform a simple task that does not involve decision making. The ‘if’ instruction gives a C program decision making ability

IF Statement: Simple Example if (Number == 0) { PrintToScreen (“The Number is Zero”); } if (Number < 0) { PrintToScreen (“The Number is negative”); } This == symbol is used for checking ‘equal’ condition, not for value assignment. Only if this condition is true, this instruction is executed.

IF-Else Statement Consider the following program: If (score <60) { PrintToScreen(“You failed the test”);} If (score == 60) // ‘==‘ means ‘if equal to’ { PrintToScreen(“You passed the test”);} If (score > 60) { PrintToScreen(“You passed the test”);}

IF-Else Statement IF-else statement should be used where there are only two possible cases. If (score <60) { PrintToScreen(“You failed the test”);} else { PrintToScreen(“You passed the test”);}

Q2: What is the value of A and B at end of this program? A = 9 ; B = 12 ; if ((A + B) > 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 ……………………

First Approach: It can be implemented by a single statement: int SUM ; // integer variable for the result SUM = ……………… ;

Second Approach: int SUM ; //variable SUM is declared as integer SUM = 0 ; //SUM is assigned the value 0 SUM = SUM + 1 ; // Now SUM = (0 + 1) = 1 SUM = SUM + 2 ; // SUM = (1 + 2) = 3 SUM = SUM + 3 ; // SUM = (3 + 3) = 6 SUM = SUM + 49 ; //SUM = ( ) = 1225 SUM = SUM + 50; //SUM = ( ) = 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 1x2x3x………………x(N-1)xN Factorial (1) = 1 Factorial (5) = 1x2x3x4x5 = 120 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)2 (B)3 (C)4 (D)6 (E)10

Solution Initially i = 0, A = 0. First iteration: condition 0<3 is true A = 0+0=0, i = 1 Second iteration : condition 1<3 is true A = 0+1=1, i = 2 Third iteration : condition 2<3 is true So A = 1+2=3, i = 3 Fourth iteration : condition 3<3 is false So the while loop stops. Final Value of A is 3.

Infinite Loop In the above example programs we have employed a condition checking in order to control the flow of execution. i.e., 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. In the following example program, the status of a bumper switch is continuously monitored and depending on its value the movement of the robot is decided.

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 }