General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Introducing JavaScript
Dale/Weems/Headington
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
Guide To UNIX Using Linux Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
High-Level Programming Languages: C++
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.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
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.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 C++ Syntax and Semantics, and the Program Development Process.
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++
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
Information and Computer Sciences University of Hawaii, Manoa
Review 1.
Chapter Topics The Basics of a C++ Program Data Types
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Basic Elements of C++.
Variables A piece of memory set aside to store data
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Fundamental Programming
Presentation transcript:

General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009

Remaining Course Timeline Aug 3 - C/C++ Aug 5 - Matlab as a Tool Aug 10 - Final Review Aug 12 - Final, Project 2 due/demos

Lecture Overview Introducing C++ C++ vs. MATLAB Simple C++ programs Data Types Scope Loops Functions

A programming language Derived from a language called C Ubiquitous Object Oriented ◦ Easier to write large scale projects ◦ Reusability High Performance

Supports data security Helps code reuse Allows multiple functions/operators with same name

Large projects Systems applications Graphics Data Structures Want to speed up your scripts

When not to use C++ Small programs Prototyping (MATLAB, scripting languages) Web applications (javascript)

C++ versus MATLAB MATLAB uses an interpreter To run can just type and use matlab commands C++ uses a compiler Program converted to machine code with compiler

C++ versus MATLAB MATLAB uses dynamic typing Introduce new variables as needed Type of variable discovered by use C++ uses static typing Variables have to be introduced Type is given when introduced

#include using namespace std; int main() { // display output cout << “Hello World\n”; } First C++ Program (helloworld.cpp)

$ CC –o helloworld helloworld.cpp $./helloworld Hello World $

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; }

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A library providing input and output functionality

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } Can use names in the standard C++ library

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } All C++ programs have a main function

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } A comment

First C++ Program (helloworld.cpp) #include using namespace std; int main() { // display output cout << “Hello World\n”; } Send the string Hello World\n to console

C++ Variables Must introduce variables before using “Double quotes” for strings Input and output int studentAge; cout << “How old are you?”; cin >> studentAge;

Arrows “show the way” data if flowing cout << … going out (output) cin >> … going in (input)

cout << “Student Age” << 20 << endl; int i; float f; cin >> i; cin >> f;

C++ Data Types

Integer short, int, long Floating point float, double, long double Logical bool (values: true, false) Character char Text String (“Hello World”)

C++ Data Types short quantity = 63; int carValue = 0; // always initialize variables!! carValue = 57000; quantity = carValue; carValue = quantity; The variable quantity cannot store big numbers

C++ Data Types char Letter = ‘a’; string greeting = “This is a string”; greeting = Letter; Letter = greeting; The variable Letter cannot store strings; Think: A character is a string but a string is not a character.

C++ Scopes Curly braces { } introduce a new block Creates a Scope Functions create a scope Loops and if statements create a scope

C++ Scopes int main() { int x = 19; // x is known in all of main if (x == 19) { int y = 20; cout << “x + y is “ << x + y << endl; } // y is not visible here! }

For Loops in C++ Syntax for (initial expression; condition; concluding expression) { //Code } Slightly more complex that MATLAB’s Syntax for variable = start : increment : end %Code end

For Loops – A Comparison In MATLAB ◦ You create a vector of values by specifying  A start point  An end point  Maybe an increment In C++ ◦ You set the loop parameters by specifying  An initial condition  A condition  Concluding condition

For Loops – An Example Accessing ten elements in an Array In MATLAB for i = 1:10 x = array(i) end In C++ for (int i = 0; i < 10; i++) { int x = array[i]; }

For Loops – An Example Accessing every other element in an Array In MATLAB for i = 1:2:10 x = array(i) end In C++ for (int i = 0; i < 10; i+=2) { int x = array[i]; }

Break

C++ Function Definition Example 1: int main() { cout << “Hello World” << endl; } Example 2: int squareArg(int x) { return x * 2; }

C++ Function Definition Example 2: 1. int squareArg(int x) { 2. return x * 2; 3. } 1. Return type, function name, argument lists 2. Function body a) 0 or more return statements 3. Close curly brace

C++ Function Definition Function can take multiple arguments Example 3: int Power(int x, int n) { int powerof = x; for (int i =2; i<=n i++) { powerof = powerof * x; } return powerof; }

C++ Function Calling Main function calls the function squareArg. Example 2: int squareArg(int x) { return x * 2; } int main() { int x = squareArg(100); cout << “square of 100 is ” << x << endl; }

Putting all together Create a function to sum number from 1 to N What do we need?

Putting all together

Declaring of C++ arrays A typical declaration for an array is: type name [elements]; For example: int grades[60]; // int array of 60 elements

Initializing C++ arrays When we declare array we can assign initial values to each element Enclose the values in braces { } For example: int quantity [5] = {10, 235, 77, 40, 12071};

Initializing C++ arrays Can leave the square brackets empty [ ] Array size matches number of elements between {} For example: int quantity[] = {10, 235, 77, 40, 12071};

C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity

C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity A=quantity [0]; After this executes A has value 10.

C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity quantity [3] = 75; After this executes quantity[3] will have value 75.

C++ arrays have index 0 quantity[0] quantity[1] quantity[2] quantity[3] quantity[4] quantity quantity [3] = 75; Statement has executed.

Simple Array Example #include using namespace std; int main() { int quantity[] = {16, 2, 77, 40, 12071}; int result = 0; for (int i = 0; i<5; i++) { result = result + quantity[i]; } cout << result; }

Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … }

Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } element type

Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } Array variable name

Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } Pair of empty brackets

Arrays as parameters When declaring a function must specify: The element type An array variable name A pair of (empty) brackets void procedure (int anArray[]) { … } What is missing?

Example of Passing an Array #include using namespace std; int sumElements(int anArray[], int length) {... } int main() { int quantity[] = {16, 2, 77, 40, 12071}; int length = 5; int result = sumElements(quantity, length); cout << “Sum of Elements: ” << result << endl; } // rest of program on next slide

Project 2 tips Lots of comments! Clear reports on individual contribution Modularized code ◦ Use lots of functions  If it has > 6-7 lines of code it probably does more than one task ◦ Test each function that has outputs