CS212 Programming-II for Engineers

Slides:



Advertisements
Similar presentations
COSC 120 Computer Programming
Advertisements

1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Guide To UNIX Using Linux Third Edition
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CS212 Programming-II for Engineers Spring
Basic Elements of C++ Chapter 2.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 C++ Syntax and Semantics, and the Program Development Process.
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.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
C LANGUAGE Characteristics of C · Small size
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Bill Tucker Austin Community College COSC 1315
Chapter 12 Classes and Abstraction
C++ Lesson 1.
Chapter # 2 Part 2 Programs And data
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 1.2 Introduction to C++ Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
The Machine Model Memory
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
C++ Templates.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Computing Fundamentals
Basic Elements of C++.
CS3340 – OOP and C++ L. Grewe.
Objectives Identify the built-in data types in C++
Revision Lecture
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
week 1 - Introduction Goals
Basic Elements of C++ Chapter 2.
Chapter 1: Introduction to Computers and Programming
Enumeration used to make a program more readable
Introduction to Abstract Data Types
2.1 Parts of a C++ Program.
More About Data Types & Functions
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter # 2 Part 2 Programs And data
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Intro to Data Structures and ADTs
Recitation Course 0603 Speaker: Liu Yu-Jiun.
C++ Programming Basics
SPL – PS1 Introduction to C++.
Presentation transcript:

CS212 Programming-II for Engineers Spring 2018

Topics Basic Data Elements & Numeric arrays More ADTs & functions, Intro to Complexity (Big O) Linear list structures & recursion Queues (using arrays) Linked lists Stacks & Queues with pointers Prefix, postfix & infix notations General Lists & Strings Searching, binary trees & Big O again C++ classes, objects, methods & "friends" I/O, iostream, etc. Sorting Hashing Trees (general) Encoding & Compression (Huffman) Graphs

Course Material Makefiles: Syllabus and other course information available at: dforeman.cs.binghamton.edu/~foreman Makefiles: http://www.gnu.org/software/make/manual/make.html http://www.cprogramming.com/tutorial/makefiles.html

Review makefiles from CS211. See links on previous page. Some rules for CS212 This is primarily a course in Data Structures! principles are language independent We will be using C and a little C++ Platforms Lab: Linux , Windows Cygwin is NOT acceptable Programs submitted must compile & run using the GNU compiler (g++) and (the MS compiler (cl) or Visual Studio) Review makefiles from CS211. See links on previous page.

Some Course Goals Learn the use of common organizations of data in RAM Learn to evaluate efficiency memory space (RAM) needed for storage Runtime using Big O algorithms to manipulate the data Learn to define, implement and use Structured data types Basic Abstract Data Types (ADTs) Learn to use libraries of data structures STL

ANSI C++ Compilers GNU compiler Microsoft Visual Studio open source software from the Free Software Foundation www.gnu.org available on Bingsuns & labs in LNG103 built-in on most Linux systems Cygwin is a Linux-like environment (including the GNU compiler) for Windows (sources.redhat.com/cygwin/) – UNACCEPTABLE!! (compatibility, timing) Microsoft Visual Studio integrated editor, compiler, linker, project manager (IDE) installed on all Computer Center PCs and in Watson Microlab free to students in Watson courses through the Watson-on-the-Hub (see Prof. Foreman's website for link)

Development Process Java is an interpreted language C & C++ are compiled languages Source code Myprog.cpp Source code Myprog.java Interpreter Compiler byte-code file Myprog.class Object Code file Myprog.o Executable file Myprog.exe CPU MUST have interpreter to run Java pgm CPU

Data + Algorithms = Programs Every program processes data Data is stored in memory (RAM) at run-time Java, C, C++ are strongly typed languages all data items have a type associated with them Data to be processed must be declared as data objects (variables) using this syntax (for C, C+++ & Java): type name; type name = value; Type of a data object determines Allowable operations (+ - * / etc.) how data is represented in memory (integer, float, etc.)

.... Program structure – pt 1 -preprocessor directives (control compilation) (include header files} -global declarations (constants and types only!) -function prototypes (names & parameters only) myProg.c or myProg.cpp main f1 .... function definitions (implementations) fn

compile / link / execute Myprog.c Yourprog.c #include “myinfo.h” #include “myinfo.h” object's functions Myprog.o Yourprog.o Ourpgm.exe (a.out is a default name in Linux)

Compiler Commands compile only Link 2 units execute compile & link gcc -c Myprog.c produces Myprog.o gcc -c Yourprog.c produces Yourprog.o compile only gcc -o Ourpgm Myprog.o Yourprog.o produces Ourpgm Link 2 units ./Ourpgm executes the program execute compile & link gcc Myprog.c produces a.out Cl Myprog.c produces Myprog.exe

Types Pre-defined (built-in) types scalar (atomic) types – store a single value int char float, double bool structured types – store a collection of values Most languages have libraries of types STL for C and C++ Java library Programmer-defined types create new "types" appropriate for your problem built from pre-defined types

Types - 2 defined at compile time (how??) A char is USUALLY 8 bits Size off an int is 8, 16, 32 or 64 bits Size of float/double A char is USUALLY 8 bits Has a numeric value 0-255 Usually represents a printable symbol Some embedded systems may have 16 bit char's Arithmetic with a char is invalid (e.g.; char+char) A bool is a single T/F value – stored as a whole byte(?) - Compiler defined

typedef Allows using the name of an element as a datatype Does NOT reserve space for the element!!! (i.e.; the typedef statement uses no memory)

Types - 3 uint8_t is a typedef for unsigned char Why use this typedef? Indicates intent – you will do math with the element. Char is printable data in range 32-126 (decimal) 0-31 and 127-255 are for other uses but all 256 values are legal for math Examples: unsigned char i, x='a', y=0x01; i=x+y; // unclear uint8_t x='a', y=0x01; i=x+y // is clear. (both output 'b' as the value of i)

DataTypes Composite Types array struct union class void pointer Structured Types array struct union class Scalar Types Arithmetic void pointer priority_queue valarray vector deque list set map multiset multimap stack queue string bitset istream ostream iostream ifstream ofstream fstream C++ Standard Library classes Integral Floating point bool complex (reals) Characters float double long double Enumerations Integers int short int long int unsigned short unsigned long unsigned char unsigned char signed char

C vs. C++ C++ is a superset of C Any C program is a valid C++ program allows ALL of C syntax adds NEW syntax for objects I/O streams namespaces Any C program is a valid C++ program even if it does not use any of the ++ syntax Some compilers know by the file extension (MS) Others require specific compiler (gcc vs. g++)

namespaces (C & C++) "namespaces" allow you to assign portions of your program to different namespaces variable names are local so they may be reused in a program as long as the uses are in different namespaces using namespace std; the namespace “std” is a little special you don’t have to specify the “.h” for all standard #includes e.g.; #include <iostream> #include <iomanip>

Composite data types – C arrays One name for a collection of items Subscripted (as in standard math notation) X3 is represented as X[3] There are no subscript keys on a keyboard! Multiple values X[0], X[1], etc. All elements are same type and size Max # elements must be a constant (in C, C++, Java) (for non-dynamic arrays) int x[20]; float y[2012];

Arrays - 2 int X[5]; x[3]=17; X[0] X[1] X[2] X[3] X[4] 17 X[0]

Arrays - 3 C does not have an "array" data type C uses a simple variable with subscript notation: int x[5]; // BUT max subscript value in definition must be a constant int x[y]; // is ILLEGAL, even if y has a fixed value all arrays start at element 0 above is x[0] … x[4] C allows multi-dimensioned arrays (like Xijk) int y[3][4][2]; y[0][2][1]=17;

Arrays - 4 compiler might not detect it!!!!! compiler ALWAYS reserves memory for the ENTIRE array, whether it is used or not arrays may be ANY data type, including structs no protection on accessing beyond real size int x[5]; x[5]=3; // ERROR at RUNTIME compiler might not detect it!!!!! the array has 5 items, numbered 0-4 int j; scanf("enter j%i",j); x[j]=3; compiler CANNOT detect it!!!!!

Composite data types - more "struct" – a simple collection of items "union" – multiple types for the same RAM starting location union {char a,b,c,d; int X;} "a" is the leftmost byte of "X" "class" – data + functions that operate on it this is in C++, later in course All allow collections of dissimilar types

struct's A way to combine basic elements Complex numbers Create collections of elements Treat them separately AND/OR as a collection Use them to create more complex elements Complex numbers Real part Imaginary part

struct's – an example pt 1 struct Complx {float my_real_part; float my_imag_part; }; // note the semicolon!!!!! Creates a structured variable named Complx

struct's – an example pt 2 typedef struct Complx {float my_real_part; float my_imag_part; }; // note the semicolon!!!!! Creates a datatype named Complx Complx c; // "c" now refers to BOTH parts c.my_real_part=5.2; c.my_imag_part=3.6; // this is equivalent to 5.2+3.6j

Pointers a pointer is a memory address (points to some data) lets you refer to data without the data's name int x[5] ; // this takes up 20 bytes 5*(4 bytes/int) int *z; // z is a pointer to an int and is 4 bytes int (*y)[5]; // uses only 4 bytes y is a pointer to an array of 5 ints y=&x; // y now points at X (≠ X) z=&x[3]; // z now points to x[3] (has x[3]'s address) // z does not EQUAL the value in X[3] int *P[8]; // array of 8 "pointers to int's" (8*4bytes) NOT the integers themselves!!!!!!

Dynamic allocation Often need to make space at runtime Size/amount of data not pre-defined Data may need to be ordered by some rule (e.g.; "<") Complx *p; // defines ONLY the POINTER, p // only a “promise” that p will point to a Complex p=new Complx; //reserves RAM, p now points to it p->my_real_part=3.5; p->my_imag_part=5.6; Note use of pointer notation vs. member notation // equivalent to 3.5+5.6j

Abstract Data Types (ADTs) Collection of data items + operations for it Independent of programming language 2 parts: Definition – of data and operations allowed Implementation – how data is stored and algorithms to carry out the operations User – uses the ADT to solve a problem; doesn't need to know the implementation details Needs to know the syntax rules for the ADT

The Basic Concept An ADT encapsulates data and the operations on that data. The data can be accessed only via the operations & operators. Operations (methods) data

ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing how the functions and data are related An ADT implementation has several parts: interface function-names (methods) operators (possibly re-defined symbols) data Any/all of these may be public or private

ADTs – example revisited struct Complx { float my_real_part; float my_imag_part; }; // note the semicolon!!!!! All elements "visible”. Reserves storage NOT a new “type” Complx C; // reserves storage. Complx_init(&C, 2.5, 3); // prototype void Complx_init(Complx *C, float rr, float ii ) { C->my_real_part = rr; //note the pointers!! C->my_imag_part = ii; }

C++ console (terminal) I/O #include <iostream> basic operators << the "insertion" operator >> the "extraction" operator stream names for standard I/O cin is the input "file" cout is the output "file" usage cin>>x; // get a value cout<<x; // output a value

Operators New objects (structs, classes) may need more Ordinary variables have basic operators built-in + - * / % | & || && New objects (structs, classes) may need more How do you "add" 2 complex numbers? Given: complex#'s a and b; what does a+b mean??? compiler knows nothing about complex #'s Define a NEW action (function) to perform "+" add the real parts, add the imaginary parts store each result in the proper answer-part.

Example – Declare the functions typedef struct Complx { float my_real_part, my_imag_part; }; // below could be member function prototypes float Complx_get_real(return my_real_part); void Complx_init(set both parts); Complx Complx_add(add 2 complexes); // for this slide and following slides: // blue: declare the structure, green: use it, red: return-value type

Example- implementation // no automatic functions for initialization Complx x; // allocate memory for x as a struct Complx_init(Complx *x, float a, float b) { x->my_real_part=a; // note the -> notation // because x is a pointer to a Complx x->my_imag_part=b; } Complx Complx_add (Complx* a, Complx* b) {Complx tmp; // need a place for output // "tmp" is NOT a pointer-based variable, "a" and "b" are // so note differences in use of “.” and “->” tmp.my_real_part = a->my_real_part + b->my_real_part; tmp.my_imag_part = a->my_imag_part + b->my_imag_part; return tmp;

Example-pt 2 void Complx_init (Complx *, float, float); void main() { Complx A, B, C; Complx_init (&A,2,2); Complx_init (&B,1,1); C=Complx_add(&A, &B); printf("C=%d+%dj",C.myreal_part, C.my_imag_part); }

Program structure – 2 Usually, put the struct definition in a header file #include it later in BOTH : the implementation file where the methods are defined the user-program file where methods get used