Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Lecture 2 Introduction to C Programming
Introduction to C 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
JavaScript, Third Edition
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Exam 1: Review 1.History of computers 2.Fundamentals of computer architecture 3.Developing algorithms and programs 4.Variables and data types 5.User I/O.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
Lecture 4 Input/Output Conditional Statements 1. Data Types 2. Input/Outputs 3. Operators 4. Conditionals 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Type of Information Why variables matter… Any information has a “Data Type” 1.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Asking the USER for values to use in a software 1 Input.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Asking the USER for values to use in a software 1 Input.
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.
Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer Developing algorithms.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Matlab for Engineers Matlab Environment Chapter 2.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Basic Elements of C++ Chapter 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1

Definition A variable is a name to a memory location. The data stored in that location can, of course, VARY at any time Catia Project MATLAB Software Variable named area Variable named base

An important distinction A variable has a name, a value (or multiple values), a type, and a location. When you refer to a variable, you typically refer to its name, but you actually mean the value that it currently has. – Example: y = x + 3 This really means “take the current value of x, add 3, and store that value in a variable named y” 3

Example 4 Area of triangle Problem: Solve the area of a triangle where the base is 12.3, one side is 8.9, and the angle between those two sides are 34 degrees. 1) Summarize the problem – Givens base = 12.3 side1 = 89 angle between two sides = 34 degrees – Find area of triangle

Example 5 Area of triangle 2) Diagram 3) Assumptions lengths are in meters 4) Theory area = ½ * base * height height = side * sin(angle) h

Example 6 Area of triangle 5) Solve 6) Verify Accuracy Hard to do but sind(34) is a little more than 0.5, so h would be a little more than 4.5. Using 4.5 would give an area of roughly 28, so something a little more than 28 makes sense. 7a) Algorithm Define base, side and angle Calculate area Show result

BAD code Why would this be a bad code?  Absolutely NOTHING is re- usable 7

Rules to follow when naming variables AND script files 1.It cannot contain spaces: Use_the_underscore, or capitalizeTheFirstLetterOfAdditionalWords 2.It cannot start with a digit. However, it can have digits in the rest of it. 3.It cannot contain any special characters (other than the underscore of course!) 4.It should not be any keywords MATLAB knows, or 5.It should not be the filename of any saved files Also: All variables are case sensitive. AGE is not the same as age. Strong Advice: Avoid single letters, especially i and j. The name should simply represent its content. 8

Best habit to naming a variable The name should simply represent its content. 9

Avoid single letters 10

Final Script File 11

CAUTION CAUTION: This isn’t math anymore sind() calculates the sine of an angle in degrees MULTIPLICATION is NOT implied in MATLAB – in math: (2)(5)(5.5) = ? – in MATLAB: 12

Basic Data Manipulation How about solving equations? Normal algebra ≠ programming – Assume the following mathematic equation: z = x + y In algebra: when z is 10 and y is 7, what is x equal to? 13 z = x + y 10 = x + 7  Solve for x, obtain _____ Look at MATLAB:

Basic Data Manipulation 14 Assign values to variables z and y

Basic Data Manipulation 15 Assign values to variables z and y Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

Basic Data Manipulation In MATLAB, all the known variables must be on the right side of the equal sign before executing the command. At any time, only one variable can be on the left. 16 Assign values to variables z and y Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

“A scalar” A single value. Example: the number 7 is a scalar. age = 17 %creates a scalar variable weight = %another scalar variable 17

Basic Data Understanding How exactly is the data stored in the memory?  binary (i.e. machine) language: 0 and 1’s How is the number 2 represented then? Remember that the symbol we see is NOT the value represented by it. For example: what does this equal? ||||+= (in Futurama Alien Language 1)

Definition – a DATA TYPE Simply put: "The type-of-data stored in a variable" 19

Size and Weight Data-types are means of – controlling how much memory is used per “unit", and – Specifying/defining what kind of operations apply to a variable Why do data-types matter? Device“Hard Drive Size” Apollo 11 Computer 2 KB Cell phoneA couple GB (4, 8, 16) TabletsAround 32GB LaptopsAround 640 GB DesktopUp in the Terabytes (TB) now! 20

Can’t MATLAB figure it out? Why do I need to know what type of data a variable holds? Can’t MATLAB figure that out? – Yes, when it knows up front how much space a value will take up, but you won’t be using hard-coded values all semester. 21

User Interface Instead of the programmer specifying the values, the program uses an external interface where the values are entered by the user 22 (No longer hardcoded) Use of input: Here we use the word “input” to mean “information coming into the solver from outside” – input doesn’t always come from the user.

Keyboard interface There are built-in functions to get information from the user. These functions typically ask the programmer to supply ‘prompts’ to clue the user on what is being asked. input() variableName = input(‘prompt’); Two examples given in the MATLAB help: num_apples = input('How many apples? '); name = input('Enter your name: ', 's'); 23 Use of input: Here we use the word “input” to mean a specific function that collections information from the user.

Syntax is data type dependent What data type is the user asked for? – num_apples = input('How many WHOLE apples? '); The user is asked to provide a number – here, an integer. – name = input('Enter your name: ', 's'); The user is asked to provide a sequence of characters – i.e. a string. 24 These are the only 2 forms of using the input() built-in function.

Form #1. input()- integer In the first form, only the prompt is inside the parentheses: num_apples = input('How many WHOLE apples? '); There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job. The one argument is the prompt string: 'How many WHOLE apples? ' 25 function call argument

Form #2. input(…, 's') - string In the second form of the input() function, there are TWO arguments: the prompt string, and another string: ‘s’ name = input('Enter your name: ', 's'); If the 2 nd argument is present, it must be the letter 's' and no other letter! 26 1 st argument 2nd argument For this function, the second argument tells the input() function to expect a string from the user.

Code for the triangle solver The program requires two values: a base and a height. To collect them from the user: Base = input('Enter base of triangle: '); Height = input('Enter height of triangle: '); Units = input('Enter unit system chosen: ', 's'); – Without the space it’s not very pretty, and the user may actually press the space bar, which is a problem for strings: 27 Notice the space… why?

Next chapter? 28 How do we display the results to the user…? ….in a clean & professional format?