CSC 107 – Programming For Science. Announcements.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CSC Programming for Science Lecture 5: Actual Programming.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Introduction to C Programming
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.
Programming Introduction to C++.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CPS120: Introduction to Computer Science Lecture 8.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
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.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
CPS120: Introduction to Computer Science
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
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.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
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. The Week’s Goal.
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
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.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
CPS120: Introduction to Computer Science Variables and Constants.
CSC Programming for Science Lecture 37 : Course Review.
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.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
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.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables ICS2O.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Presentation transcript:

CSC 107 – Programming For Science

Announcements

History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation, language grown organically  Tradition of adding features beyond standard as desired

History of C++  Bjarne Stroustrup created to add “objects”  Also included many other improvements to language  Name is inside joke: "++" is increment operator in C  Updated for quick growth  2.0 release in 1989  1998 adopted as ISO standard  C x in development now

C Versus C++ C++ is designed to be as compatible with C as possible, thereby providing a smooth transition from C

C Versus C++ C++ C

C Versus C++ C

 Latest definition of C added most C++ features  Not classes & objects, these only found in C++  For this reason, also not a part of CSC 107  Differences now minimal and easily avoided  Once objects removed, C++ just “looser” C  Removes annoying restrictions that had been in C  Since makes life easier, often supported in C anyway

 Computers have no common-sense do what you tell them to do  They will only do what you tell them to do  NOT what you want them to do  NOT what you want them to do, which often differs

Case-Sensitivity  Example of computers being very literal  And language not helping by fixing what you say  main, Main, & MAiN treated as different words  Case of the letters matters, not just the words  Could be different, so C++ won’t change Main to main  Can help prevent easy mistakes from swapping names  With just a little practice, becomes second nature

“Whitespace”  One (very small) way C++ actually helps you  C++ treats whitespace equally – spaces, enters, & tabs  Whether 1 or – all will be ignored  Cannot use in symbol, whitespace splits words  Treats these as different “ : : ” and “ :: ”  Spaces between words needed, but not counted  Wecansplitwordsbutthecomputercannot

“Whitespace”  One (very small) way C++ actually helps you  C++ treats whitespace equally – spaces, enters, & tabs  Whether 1 or – all will be ignored  Cannot use in symbol, whitespace splits words  Treats these as different “ : : ” and “ :: ”  Spaces between words needed, but not counted  Wecansplitwordsbutthecomputercannot

Your First C++ Program #include using std::cout; int main() { /* Hi, Mom. This is a comment that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }

#include Statements #include using std::cout; /* Hi, Mom. This is a comment that goes over 2 line. */ int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  Nearly every C++ file begins with this directive  May add more #include to include other files  Contents of included file usable as if it were here  Easy way to copy ideas across multiple files  Programs can use two types of #include statements  Include system file using #include  #include “ filename ” includes a file you wrote

Watch Me Pull a Rabbit #include using std::cout; /* Hi, Mom. This is a comment that goes over 2 line. */ int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  For now, automatically start each file with this line  Details are unimportant – consider it magic

Watch Me Pull a Rabbit #include using std::cout; /* Hi, Mom. This is a comment that goes over 2 line. */ int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  For now, automatically start each file with this line  Details are unimportant – consider it magic

Your First C++ Program #include using std::cout; int main() { /* Hi, Mom. This is a comment that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }

Using Commands #include using std::cout; /* Hi, Mom. This is a comment that goes over 2 line. */ int main() { std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  More “magic”, but using has less important purpose  Tells compiler we are lazy & save some typing  Two types of using statements to choose from  Specify single shortcut with using std::cout  using std; gives you a list of shortcuts to use  Unlike #include statements, using never required  Do not worry about it – will not be using them

Your First C++ Program #include using std::cout; int main() { /* Hi, Mom. This is a comment that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }

main Function #include using std::cout; int main() { /* Hi, Mom. This is a comment that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  All C++ programs contain function called main  Tells computer where to start running program  Code inside the braces will be what is executed  For the moment, consider this more “magic”

main Function #include using std::cout; int main() { /* Hi, Mom. This is a comment that goes over 2 line. */ std::cout << “Hello world!”; return 0; // This comment goes to the line’s end }  All C++ programs contain function called main  Tells computer where to start running program  Code inside the braces will be what is executed  For the moment, consider this more “magic”

Comments  Vital  Vital for writing and maintaining any program  Not required to run program - only for human eyes  Computer simply ignores anything in a comment  Use to describe code in simple English  Sie konnen auch auf Deutsch screiben  o U c%d wrte n txt msg  Should be used liberally  I add comments where cannot see what code does  Impossible to have too many comments, if readable

Comments in C++  Double slash comments continue to line’s end a = a – 4; // Hi, Mom! // This entire line is a comment!  /* … */ comments can be on one or more lines a = a - /* Hi, Mom! */ 4; /* This comment takes an entire line. */ /* This is a really long comment that * goes on to multiple lines. The stars on * lines 2 and on are optional, but * makes things easier to read. */

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 common convention

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); cout << MY_NAME; cout << 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); cout << MY_NAME; cout << DUMB_EXAMPLE;

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

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

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

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

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

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

Variables  Variable names location to store data  Memory location's initial value is unknown  Assignments update memory location with new value  Memory location 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++ 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  Includes all type names on p. 83 of book  void, unsigned, class also reserved words

Variable Name Conventions  Usually names begin with lowercase letter  Helps clarify variables & symbolic constants  Provide good idea of what 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 parents and/or priest

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  American Standard Code for Information Interchange  256 (= 2 8 ) possible characters in extended definition  Since computers are stupid, need to set fixed size  Only use 0 s & 1 s within computer – all it knows  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  Single variable declared as: type name; double goodNameExample; short bad;  Can also declare multiple variables at once: int i, j; long double k, l, m, n, o, p; float thisIsAReallyLongName, thisIsAnotherLongName;

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  Constants very similar to variables  Must be declared with a data type and unique name  const data_type var_name declares variable  Value of constant fixed when declared, however  Variables & constants treated and used similarly

Your Turn  Get in groups & work on following activity

For Next Lecture  Read sections 5.1 – 5.4 & 5.6, 6.1 – 6.6 for Tues.  What is this cout thing, anyway?  Can we read in input from the keyboard?  What operations exist for us to use with variables?  What do we mean by order of operations?  Week #1 weekly assignment due Tuesday  Problems available on Angel – covered 1 st two already  If problem takes more than 10 minutes, TALK TO ME!