Programming by Example Version 1.1. Objectives  Take a small computing problem, and walk through  the process of developing a solution.  Investigate.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Computer Programming w/ Eng. Applications
Program Design. Objectives Students should understand the basic steps in the programming process. Students should understand the need for good design.
Computer and Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Programming by Example Version 1.0. Objectives Take a small computing problem, and walk through the process of developing a solution. Investigate the.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
1 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
Computer Science 1620 Programming & Problem Solving.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
1 Lab-1 CSIT-121 Fall 2004 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
(c) Bob McKillop, 2006Tutorial #1 T1-1 Tutorial #1 - your first compiled code  the learning objectives for this tutorial are very modest q compile and.
A First Program Using C#
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Module 1: Introduction to C# Module 2: Variables and Data Types
Fortran 1- Basics Chapters 1-2 in your Fortran book.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Input, Output, and Processing
C++ Programming: Basic Elements of C++.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
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.
Variables. Todays Lesson  In todays lesson you are going to:  Learn to use variables  Learn to ask for user input  Learn to save the users response.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Additional Pair Programming Exercises 1. Problem 1 For a wheel of radius r feet, how many revolutions are required for that wheel to travel 1 mile? What.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
COMPUTER PROGRAMMING Year 9 – Unit 9.04 Week 3. Open the Python INTERPRETER Can you use the interpreter to solve these maths problems? 156 add
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Program Design.
Chapter 1.2 Introduction to C++ Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Using the Console.
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Chapter 2 Assignment and Interactive Input
Introduction to Robots and the Mind - Methods -
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PROGRAM STRUCTURE CSC 111.
Advanced Programming Lecture 02: Introduction to C# Apps
Chapter 3 – Introduction to C# Programming
IS 135 Business Programming
Presentation transcript:

Programming by Example Version 1.1

Objectives  Take a small computing problem, and walk through  the process of developing a solution.  Investigate the structure and syntax of a C# Program.  Use the Visual Studio code editor and compiler.

Problem Move robot from position a to position b. Move it in the most direct route possible. a b

We could run ahead 5 spaces

Then turn 90 and move ahead 3 spaces o

But it would be faster to …

To make this move, we need to * Calculate how much to turn, and * Calculate how far to move

Looks like a right triangle The path we want to take

6 How do we find the angle that we must turn the robot? 4

? 6 4 We know the length of the side opposite the angle and the length of the side adjacent to the angle (rise over run). θ tan θ = opposite adjacent so … θ = tan 4 6 = o

? 6 4 Now, how do we compute the distance to move? θ = o

? 6 4 There are several ways we can do this. Let’s use The Pythagorean theorem. θ = o c = a + b 2 2

? 6 4 There are several ways we can do this. Let’s use The Pythagorean theorem. θ = o c = a + b = 52 = = 7.211

We have come up with the algorithm required! Let’s write it down step by step. 1.Compute the angle to turn 2. Compute the distance to move

In order to write our program, we need a little more detail. 1. Compute the angle to turn a.Find the length of the opposite side (rise), a b. Find the length of the adjacent side (run), b c. Divide a by b d. Find the angle whose tangent = a/b

In most programming languages there are libraries of mathematical functions to do things like … find the angle whose tangent is a/b.

Microsoft provides an extensive set of resources To help us with C# development. On the web, Go to

2. Compute the distance to move a. Square the length of side a b. Square the length of side b c. Add them together d. Take the square root of the result

When solving a programming problem like this, we often draw an “Activity” diagram, to show the steps of the program pictorially.

get lengths of a and b

get lengths of a and b divide a by b

get lengths of a and b divide a by b find atan (a/b)

get lengths of a and b divide a by b find atan (a/b) square a

get lengths of a and b divide a by b find atan (a/b) square a square b

get lengths of a and b divide a by b find atan (a/b) square a square b add the squares

get lengths of a and b divide a by b find atan (a/b) square a square b add the squares take the square root

get lengths of a and b divide a by b find atan (a/b) square a square b add the squares take the square root print results

You can also write down your algorithm design in Pseudo-Code – English like phrases that describe each step

1. Get lengths of a and b 2. Divide a by b 3. Find arctangent of (a/b) 4. Square a 5. Square b 6. Add the squares of a and b 7. Take the square root of the result 8. Output the results

Now, we are ready to write the program. Don’t worry about the details of each C# statement in the following slides. The objective of the next few slides it to give you an overview of what a C# program looks like. Pay attention to the overall organization and structure of the code.

// This program takes two values, and x and a y as real numbers // The program computes the hypotenuse of a right triangle whose // base is x and whose height is y. It also returns the angle between the base // and the hypotenuse. // Author: Joe Coder // Course: CS 1400 section 002 // Date Last Modified: July 2, 2009 // Version 1.0 We begin all programs with a file prologue. The file prologue explains what is in the file. The forward slash marks “//” mark this line as a comment. The compiler will ignore this line when compiling the program.

using System; Next, we have to tell the compiler about any namespaces that we will use. For all Programs that we will write this semester We will use the System namespace.

All of the code that we write in a C# program will be enclosed in one or more classes. Initially we will just use one class. Although we can name this class anything that we want to, we will call it Program. The code within a class is enclosed in curly braces like this static class Program {... }

Every C# program must include a method whose name is “Main”. When your program runs, the computer looks for the method named Main, and begins execution at that point. Everything in the Main method will be between a pair of curly braces. static void Main( ) { … }

// declare variables we will use in our program double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; We now declare any variables that we will use in this program. We need A place in memory to hold the length of each side of the triangle and a place to hold the size of the angle. data type (size and shape) variable names

The Atan function returns the size of an angle in radians. We will need a constant to change radians into degrees. We need to declare this constant and set its value. The Math class contains a constant for PI that we will use. // declare a constant to do conversion // from radians to degrees const double CONVERSION_FACTOR = 180 /Math.PI; C# statements all end in a semicolon. static class Math{ }

Console.WriteLine( " This program computes the hypotenuse of a right triangle. " ); Console.Write( " Please enter in the base of the triangle: " ); get lengths of a and b These statements provide a user prompt. That is, they help the user of the program know what to do next. The Console class represents the display on the computer. The WriteLine method writes the text in quotation marks to the display and moves to the next line. static class Console{ }

string _stgWidth = Console.ReadLine( ); width = double.Parse(_stgWidth); or width = double.Parse(Console.ReadLine( ) ); get lengths of a and b This statement gets the user’s input and saves it in the variable named base. The ReadLine method reads a string from the keyboard and returns in-place of itself a temporary string variable. The double.Parse method converts the string into a double which we save in the variable base.

Console.Write( " Please enter in the height of the triangle: " ); height = double.Parse(Console.ReadLine( ) ); Now, prompt the user to type in the height of the triangle and store it in the variable height. get lengths of a and b

double square = (width * width) + (height * height); hypotenuse = Math.Sqrt(square); theta = Math.Atan ( height / width) * CONVERSION_FACTOR; Do the calculations necessary to compute the length of the hypotenuse and the angle. multiply divide add

Console.WriteLine( " The hypotenuse of the triangle is {0} ", hypotenuse); Console.WriteLine{ " The angle between the hypotenuse and the base is {0:f2} ", theta); Print out the results The {0:f2} is a placeholder. The string value of theta gets put into this place when the data is written to the console. f2 outputs value as floating point string With 2 digits after the decimal point.

Console.ReadLine( ); This statements completes the program. This statement keeps the DOS console window open, until the user presses the Enter key, so that the user can see the program’s output.

Here is the complete program …

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 // Version 1.0 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); }//End Main() }//End class Program

Desk Check the Code Play the role of the computer. Go through the code step by step and see if the results at each step are correct and make sense.

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); }//End Main() }//End class Program width 0.0 height Write down the variable names hypotenuse theta

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } Use a calculator To check this conversionFactor

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } width height 0.0 Write down the value Let’s assume the user types 3 hypotenuse 0.0 theta 0.0 conversionFactor

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } width height 0.0 Write down the value Let’s assume the user types 3 hypotenuse 0.0 theta 0.0 conversionFactor

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } width height Write down the value Let’s assume the user types 4 hypotenuse 0.0 theta 0.0 conversionFactor

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } width height Check this with a calculator hypotenuse 0.0 theta 0.0 conversionFactor Square 25

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } base height Check this with a calculator hypotenuse theta 0.0 conversionFactor square 25 5

// Robot Mover Example // Author: Joe Coder // Course: CS // Date last modified: April 8, 2009 using System; static class Program { const double CONVERSION_FACTOR = 180 / Math.PI; static void Main() { double width=0.0, height=0.0, hypotenuse=0.0, theta=0.0; Console.WriteLine("This program moves a robot from the origin to"); Console.WriteLine("a point that you specify."); Console.Write("Please enter the x-coordinate of that point: "); width = double.Parse(Console.ReadLine()); Console.Write("Please enter the y-coordinate of that point: "); height = double.Parse(Console.ReadLine()); hypotenuse = Math.Sqrt(width * width + height * height); theta = Math.Atan(height / width) * CONVERSION_FACTOR; Console.WriteLine("Turn the robot {0:f2} degrees", theta); Console.WriteLine("and move the robot {0:f2} units.", hypotenuse); Console.ReadLine(); } base height Check this with a calculator hypotenuse theta conversionFactor square

Compiler Demonstration Using Visual C# Express, type in the code for the program that we just developed. Compile and run it.

Test the program with the values used in the desk check. Does it produce the same answers?

Summary of what we did 1. We analyzed the problem to be solved a. Gather information – write down what you know b. Write down what you need to find out c. Write down the steps to get from a to b, in detail 2. Draw an activity diagram 3. Write the code 4. Desk check your code – fix it if necessary (refactor) 5. Compile your code – fix compiler errors (refactor) 6. Test your code – fix it if necessary (refactor)

Another Example

An Electrical Engineering Problem Ohm’s law states that the voltage across a resistor is equal to the current through the resistor times the value of the resistance, or … v = i * R

Furthermore, if two or more resistors are connected in series, the equivalent resistance is equal to the sum of the individual resistances. R eq = R 1 + R 2 + … R n

Consider the following circuit: 2Ω2Ω 8Ω8Ω 12A Develop a program that will compute the voltage drop across the two resistors. Allow the user to specify the values for the resistors and the current.

1.Analyze the problem – a.Write down what you know b.Write down what you want to find out 2. Write down the steps involved in the program 2. Draw an activity diagram 3. Based on the previous example, see if you can write the code that will solve this problem.

Remember the basic structure of a C# Program // file prologue using System; static class Program { // declare constants static void Main( ) { // declare local variables // prompt the user for input // get input // calculate the answer // output the answer }//End Main() }//End class Program

Another Example

telephone #1 telephone #2 telephone #3telephone #4 A directly connected telephone service is one in which each telephone is directly connected to every other telephone in the system. There is no central switching station. The number of lines required to connect n telephones = n (n-1)/2.

Write a program that computes the number of lines required to connect n telephones, where the user supplies the value of n.

1.Analyze the problem – a.Write down what you know b.Write down what you want to find out 2. Write down the steps involved in the program 2. Draw an activity diagram 3. Based on the previous example, see if you can write the code that will solve this problem.

Remember the basic structure of a C# Program // file prologue using System; static class Program { // declare constants static void Main( ) { // declare local variables // prompt the user for input // get input // calculate the answer // output the answer }//End Main() }//End class Program

Another Example

When the Mormon pioneers crossed the plains. The built a simple odometer to help them figure out how far they traveled each day. The idea is simple: just count the number of times the wheel rotates, and then given the size of the wheel you can determine how far you traveled.

Write a program that, given the diameter of a wagon wheel, will compute the number of rotations the wheel would make in one mile.

1.Analyze the problem – a.Write down what you know b.Write down what you want to find out 2. Write down the steps involved in the program 2. Draw an activity diagram 3. Based on the previous example, see if you can write the code that will solve this problem.

Remember the basic structure of a C# Program // file prologue using System; static class Program { // declare constants static void Main( ) { // declare local variables // prompt the user for input // get input // calculate the answer // output the answer }//End Main() }//End class Program