PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming w/ Eng. Applications
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
CPS120: Introduction to Computer Science Lecture 8.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
CSC 107 – Programming For Science. Announcements.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Instructor - C. BoyleFall Semester
CPS120: Introduction to Computer Science
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
PHY 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
CSC 107 – Programming For Science. Announcements.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
CSC Programming for Science Lecture 4: Beginning Programming.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
CSC Programming for Science Lecture 37 : Course Review.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
User Interaction and Variables
ECE Application Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Unit 3: Variables in Java
Module 2 Variables, Data Types and Arithmetic
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

PHY 107 – Programming For Science

Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those who want  Do get updated after class with any needed updates  Use them in labs, weekly assignments, and projects  There are also many other resources available  Links on D2L to a few useful websites  Library’s closed reserve has textbook & other books

Your First C Program

Pre-processor Directives  Code “pre-processed” before compilation  No need to request it --- automatically occurs  Easier-to-read code results from this process  Just like using comments -- notice a recurring theme?  Pre-processor directives start with #  One directive per line & nothing else on the line  Directives should not span multiple lines

Symbolic Constants  Directive can be used to name a constant  Any/all lines BELOW directive can use this constant  Pre-processor replaces name with value  Compiler sees value as if that were written there  When reading the code, programmer sees name  Makes code much easier to read, write, debug  Names traditionally in all CAPITAL letters  THIS IS NOT REQUIRED  THIS IS NOT REQUIRED, but “good style”

What You Write And Work With #define PI #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME double area = PI * (r * r); printf(MY_NAME); printf(DUMB_EXAMPLE);

What The Compiler Sees #define PI #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME double area = PI * (r * r); printf(MY_NAME); printf(DUMB_EXAMPLE);

What The Compiler Sees #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME double area = * (r * r); printf(MY_NAME); printf(DUMB_EXAMPLE);

What The Compiler Sees #define AVOGADRO 6.022E23 #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME double area = * (r * r); printf(MY_NAME); printf(DUMB_EXAMPLE);

What The Compiler Sees #define MY_NAME “Matthew Hertz” #define DUMB_EXAMPLE MY_NAME double area = * (r * r); printf(MY_NAME); printf(DUMB_EXAMPLE);

What The Compiler Sees #define DUMB_EXAMPLE “Matthew Hertz” double area = * (r * r); printf(“Matthew Hertz)”; printf(DUMB_EXAMPLE);

What The Compiler Sees double area = * (r * r); printf(“Matthew Hertz)”; printf(“Matthew Hertz)”;

What The Compiler Sees double area = * (r * r); printf(“Matthew Hertz)”; printf(“Matthew Hertz)”;

Symbolic Constants  Value copied where it is used

Symbolic Constants

Variables  Variable  Variable gives name to address where data stored  When variable created, its initial value is unknown  Assignments update memory location with new value  Locations in memory updated by assignment ONLY  When variable is used in program…  …uses current value at that memory location  Just about everything (interesting) uses variables

Variable Declarations  Variables must be declared before can be used  Way of getting computer to make space for variable  States how to interpret memory in future uses  Allows the compiler to check if uses are legal  Declarations must include two pieces:  Each variable must have legal, unique name  Type of data that the variable stores

Variable Names  Begin with letter or underscore ( _ )  Then use any letters, numbers, or underscore  C/C++ case-sensitive when naming variables  Will treat as different Mass, mass, & masS  Unique name * needed for each variable  Computer wouldn't know which of 1,000 bob s to use  Reserved words are… reserved and can't be used  Reserved words includes void, unsigned, class

Variable Name Conventions  Usually names begin with lowercase letter  Helps clarify variables & symbolic constants  Best if name specifies datum variable stores  Split multiple uses into multiple variables  Some things always make for bad names  tmp, b, l (lowercase letter L)  Anything would not say to grandparent, priest, boss…

Variable Name Conventions

Data Types  Each variable also has data type  How program treats variable’s value defined by this  Single true or false value held by bool  C/C++ defines 7 numeric data types  Integer types: short, int, long, long long  Decimal types: float, double, long double not really standardized  Ranges for each type is not really standardized  Non-negative versions using unsigned ______  char data type can hold a character

Representing Text  Most computers you find follow ASCII standard ASCII  American Standard Code for Information Interchange  256 (= 2 8 ) possible characters in extended definition  Since computers are stupid, need to set fixed size  Computers use 0 s & 1 s ONLY – its all they know  Number still stored, but character is displayed  For number 97, a is printed  Prints & for number 38  For number 55, 7 is printed

ASCII Table

There Is No Character  For computer, there are no characters  Add to actual number just like normal addition: ’M’+ 3 = = 80 (’P’) ’0’+ 5 = = 53 (’5’) 9 +’1’= = 58 (’:’) ’1’+’0’= = 97 (’a’)  Can also use to subtract, divide, any other operation

Writing Variable Declarations

 Could also specify initial value for variable  Variable, constant, literal, or expression can be used int i = 0.0; long j = -1; long double k = ; long l = j, many, minusJ = -j; char c = 'i'; char newLine = '\n'; char tab = '\t';

Writing Variable Declarations  Could also specify initial value for variable  Variable, constant, literal, or expression can be used int i = 0.0; long j = -1; long double k = ; long l = j, many, minusJ = -j; char c = 'i'; char newLine = '\n'; char tab = '\t';

Constants

Variables, Constants, & More General CasesExamples Variable Declaration dataType name; dataType name = value; dataType name, anotherName; dataType name = value, anotherName; int count; bool monkey = true; char help,letter; char a=‘a’,letter; Constant Declaration const dataType NAME= value; const double PI=3.1; Symbolic Constant #define NAME value #define AGE 34

Your Turn  Work on activity in groups until 11:45  Each group will submit 1 copy at end of class  Professor chooses the copy; so must work together

For Next Lecture  Read pages for Wed.  What operations exist for us to use with variables?  What can we use these variable to do anything?  Why do data types matter in computations?  What do we mean by order of operations?  Week #1 weekly assignment due Tuesday  Problems available on D2L & should be doable  If problem takes more than 10 minutes, TALK TO ME!