Our First Programs. Programs. Hello World 10/7/2013

Similar documents
Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting

C# Programming Tutorial Lesson 1: Introduction to Programming

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Testing. UW CSE 160 Winter 2016

Arrays in C++ Instructor: Andy Abreu

First of all, it is a variable, just like other variables you studied

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Lecture Transcript While and Do While Statements in C++

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Math Day 2 Programming: How to make computers do math for you

Fraction Arithmetic. A proper fraction is a fraction with a smaller numerator than denominator.

Pointers, Arrays and Parameters

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

Ex: If you use a program to record sales, you will want to remember data:

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Skill 1: Multiplying Polynomials

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

2.Simplification & Approximation

Text Input and Conditionals

Divisibility Rules and Their Explanations

Create a Login System in Visual Basic. Creating a login system. Start a new visual basic Windows Forms application project. Call it Login System

Repetition CSC 121 Fall 2014 Howard Rosenthal

Decisions, Decisions. Testing, testing C H A P T E R 7

Section Notes - Week 1 (9/17)

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

(Refer Slide Time 3:31)

(Refer Slide Time: 00:26)

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

A.4 Rationalizing the Denominator

CSCI 1103: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CS Lecture 19: Loop invariants

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

T H E I N T E R A C T I V E S H E L L

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Introduction to Computer Science Unit 3. Programs

Lesson 4.02: Operations with Radicals

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

CSE 115. Introduction to Computer Science I

CS 11 python track: lecture 3. n Today: Useful coding idioms

Table of Laplace Transforms

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Core Mathematics 1 Indices & Surds

Warm Up. Factor the following numbers and expressions. Multiply the following factors using either FOIL or Box Method

Reviewing all Topics this term

Algorithm. An algorithm is a computational process for solving a problem. computational: a computer must be able to perform the steps of the process

Lecture 7: the quotient topology

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

UNIT 7A Data Representation: Numbers and Text. Digital Data

CS 1110, LAB 1: PYTHON EXPRESSIONS.

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Storing Data in Objects

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

How to approach a computational problem

CS 1110: Introduction to Computing Using Python Loop Invariants

Intro. Scheme Basics. scm> 5 5. scm>

Lesson 6: Modeling Basics

Computer Programming

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CS1 Lecture 4 Jan. 23, 2019

MA 1128: Lecture 02 1/22/2018

(Refer Slide Time 6:48)

CS 101 Spring 2007 Midterm 2 Name: ID:

REU C Programming Tutorial: Part I FIRST PROGRAM

3-1 Writing Linear Equations

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

5.6 Rational Equations

Errors and Exceptions. EECS 230 Winter 2018

3 Nonlocal Exit. Quiz Program Revisited

Lecture 10: Introduction to Correctness

Introduction to Programming in Turing. Input, Output, and Variables

Unit 6 - Software Design and Development LESSON 4 DATA TYPES

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Welcome! COMP s1. Programming Fundamentals

Get to Know Your Calculator!

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

More about Binary 9/6/2016

Getting Started Values, Expressions, and Statements CS GMU

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

n = 1 What problems are interesting when n is just 1?

IEEE Floating Point Numbers Overview

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

AP Computer Science Unit 3. Programs

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Switching Circuits and Logic Design Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Hi. I m a three. I m always a three. I never ever change. That s why I m a constant.

cs1114 REVIEW of details test closed laptop period

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Class 1: Homework. Intro to Computer Science CSCI-UA.0101 New York University Courant Institute of Mathematical Sciences Fall 2017

Transcription:

Our First Programs C# Programming Rob Miles Programs In the practical session last week we created a number of programs In this session we are going to look at each program and see how it works/what it does We are also going to investigate how programs store and work with data Our First Programs 7-Oct-13 Rob Miles 2 Hello World class Hello Console.WriteLine("Hello World"); This program just prints Hello World Our First Programs 7-Oct-13 Rob Miles 3 1

Hello World class Hello Console.WriteLine("Hello World"); This is the statement that does the work Our First Programs 7-Oct-13 Rob Miles 4 This program reads in something, stores it and then writes it out again Our First Programs 7-Oct-13 Rob Miles 5 Write a message to ask the user a question Our First Programs 7-Oct-13 Rob Miles 6 2

Read the message and store it in a string variable called name Our First Programs 7-Oct-13 Rob Miles 7 This creates a variable a box in memory to hold strings The box is called name Our First Programs 7-Oct-13 Rob Miles 8 This reads a line of text that the user types in Our First Programs 7-Oct-13 Rob Miles 9 3

This is an assignment It takes the value on the right and assigns it to the variable on the left Our First Programs 7-Oct-13 Rob Miles 10 Write the word Hello followed by the contents of the variable called name Our First Programs 7-Oct-13 Rob Miles 11 How Programs Work The bit of the program that actually does the work is contents of the Main method This is a sequence of statements each of which does one particular step The program performs each statement in turn and then ends after it has completed the last statement in the program Our First Programs 7-Oct-13 Rob Miles 12 4

This program reads in two numbers, adds them together and then prints out the result Our First Programs 7-Oct-13 Rob Miles 13 Say Hello Our First Programs 7-Oct-13 Rob Miles 14 Ask for the number Our First Programs 7-Oct-13 Rob Miles 15 5

Read in a string of text (we can t read numbers directly) Our First Programs 7-Oct-13 Rob Miles 16 Use a method called Parse to convert the text into a numeric value Our First Programs 7-Oct-13 Rob Miles 17 Repeat for the second number Our First Programs 7-Oct-13 Rob Miles 18 6

Do the sum (anyone spot the bug?) Our First Programs 7-Oct-13 Rob Miles 19 Write out the answer Our First Programs 7-Oct-13 Rob Miles 20 Finding the Bug The bug does not cause the program to crash It will always produce an answer when given two numbers However, the answer is wrong, because the user is expecting to see an addition, and they are being given a multiplication Our First Programs 7-Oct-13 Rob Miles 21 7

int result = number1 + number2; To fix the program we just have to swap the * (multiply) for a + (add) Our First Programs 7-Oct-13 Rob Miles 22 Test Data The scary thing is that the program will work fine for some inputs: 0+0 is the same as 0*0 1+1 is the same as 1*1 2+2 is the same as 2*2 When we create a program we need to design some proper tests to prove that it works Our First Programs 7-Oct-13 Rob Miles 23 Adding 4 numbers int v1, v2, v3, v4; v1 = int.parse(console.readline()); v2 = int.parse(console.readline()); v3 = int.parse(console.readline()); v4 = int.parse(console.readline()); int sum = v1 + v2 + v3 + v4; This program calculates the sum of four numbers It works by scaling up the earlier solution Our First Programs 7-Oct-13 Rob Miles 24 8

Adding 4 numbers int v1, v2, v3, v4; v1 = int.parse(console.readline()); v2 = int.parse(console.readline()); v3 = int.parse(console.readline()); v4 = int.parse(console.readline()); int sum = v1 + v2 + v3 + v4; A program can create lots of variables at once by using a list Our First Programs 7-Oct-13 Rob Miles 25 Adding 4 numbers int v1, v2, v3, v4; v1 = int.parse(console.readline()); v2 = int.parse(console.readline()); v3 = int.parse(console.readline()); v4 = int.parse(console.readline()); int sum = v1 + v2 + v3 + v4; A program can feed the output of one method call into another Our First Programs 7-Oct-13 Rob Miles 26 Is this a good solution? We can see how it works We could add 1,000 numbers this way But it would be very tedious Is there a better way that avoids the need for v1, v2, v3 and the rest? Our First Programs 7-Oct-13 Rob Miles 27 9

A better solution int sum = 0; sum = sum + int.parse(console.readline()); sum = sum + int.parse(console.readline()); This makes the program much simpler We don t need all the v1, v2, v3 stuff We just increase the sum by each number as it is reads in Our First Programs 7-Oct-13 Rob Miles 28 A perfect solution? This is not a perfect solution To add 1,000 numbers we would still need 1,000 statements A better way would be to use a loop, which we will see next week We have learnt that sometimes simple solutions don t scale very well Our First Programs 7-Oct-13 Rob Miles 29 More Numbers int v1, v2, v3, v4; v1 = int.parse(console.readline()); v2 = int.parse(console.readline()); v3 = int.parse(console.readline()); v4 = int.parse(console.readline()); int sum = v1 + v2 + v3 + v4; To fix the program we just have to swap the * (multiply) for a + (add) Our First Programs 7-Oct-13 Rob Miles 30 10

Averages int average = sum / 4; We can use our program to work out the average of the values However, this doesn t end well when we use integers Our First Programs 7-Oct-13 Rob Miles 31 What is an integer? An integer is used for counting We could use it to count how many sheep are in a field We never need to store fractions of sheep, and so we don t need any fractions We can save computer memory and keep programs simpler by just storing the number part and leaving off the fraction Our First Programs 7-Oct-13 Rob Miles 32 Problems with integers int average = sum / 4; We get problems when we try to work out sums that need fractional parts For example the average weight of a sheep Integers are no good for us We can use floating point values instead Our First Programs 7-Oct-13 Rob Miles 33 11

Converting to floats float average = sum / 4; C# provides a type called float that we can use in just the same way as integer But it stores a fractional part as well This means that we can get more precise results if we need them Our First Programs 7-Oct-13 Rob Miles 34 Things to Remember Programs run one statement at a time They can store data in named boxes called variables The Parse method will convert a string of digits into a numeric value Integers do not hold fractional parts, but floats do Our First Programs 7-Oct-13 Rob Miles 35 12