Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Similar documents
Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 3: Input/Output

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CHAPTER 3 Expressions, Functions, Output

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Chapter 1 INTRODUCTION

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Exercise: Using Numbers

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Objects and streams and files CS427: Elements of Software Engineering

Window s Visual Studio Output

Chapter 3. Numeric Types, Expressions, and Output

Introduction to Programming EC-105. Lecture 2

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

I/O Streams and Standard I/O Devices (cont d.)

Chapter 3 - Notes Input/Output

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

REVIEW. The C++ Programming Language. CS 151 Review #2

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS313D: ADVANCED PROGRAMMING LANGUAGE

A First Program - Greeting.cpp

Engineering Problem Solving with C++, Etter/Ingber

BITG 1233: Introduction to C++

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

WARM UP LESSONS BARE BASICS

Computer Components. Software{ User Programs. Operating System. Hardware

Fundamentals of Programming CS-110. Lecture 2

Scientific Computing

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Chapter 1: Introduction to Computers and Programming

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Creating a C++ Program

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Lecture 2 Tao Wang 1

Fundamentals of Programming

Fundamental of Programming (C)

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

PROGRAMMING EXAMPLE: Checking Account Balance

Unit 3. Constants and Expressions

LECTURE 02 INTRODUCTION TO C++

Maciej Sobieraj. Lecture 1

Chapter 2: Overview of C++

Reserved Words and Identifiers

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CSCI 1061U Programming Workshop 2. C++ Basics

Computer Programming : C++

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Integer Data Types. Data Type. Data Types. int, short int, long int

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2: Introduction to C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

File I/O Christian Schumacher, Info1 D-MAVT 2013

Introduction. Arizona State University 1

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Introduction to Programming using C++

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter 1 Introduction to Computers and Programming

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Topic 2: C++ Programming fundamentals

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

IT 1033: Fundamentals of Programming Data types & variables

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 14 Sequential Access Files

C++ basics Getting started with, and Data Types.

Chapter 1 Introduction to Computers and C++ Programming

The C++ Language. Arizona State University 1

CS242 COMPUTER PROGRAMMING

C++ Input/Output: Streams

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic data types. Building blocks of computation

6.096 Introduction to C++ January (IAP) 2009

Chapter 2 Basic Elements of C++

Computers Programming Course 6. Iulian Năstac

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Introduction to C++ (Extensions to C)

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Motivation was to facilitate development of systems software, especially OS development.

Objectives. In this chapter, you will:

Week 3: File I/O and Formatting 3.7 Formatting Output

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

printf( Please enter another number: ); scanf( %d, &num2);

CS112 Lecture: Primitive Types, Operators, Strings

Programming. C++ Basics

2 nd Week Lecture Notes

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Summary of basic C++-commands

Zheng-Liang Lu Java Programming 45 / 79

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Work relative to other classes

Transcription:

Computer Systems Computer System Computer Structure C++ Environment Imperative vs. object-oriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text Editor Airline Reservation GUI Interface Operating System Web Browser Commend Interpreter(shell) Application Software System Software COSC120 COSC 220 COSC320 COSC350 COSC450 Constant variable, static variable Formatted output Machine Language Input from a file Output to a file Micro-Architecture Hardware Physical Devices 1 2 Computer Structure High-level vs. Low-level Languages CPU RAM ALU R 1 Registers Control Unit R 2 R 3 I High-Level Languages Low-Level Languages C++, Fortran, Java, LISP, Algol,.. Assembler Language for a specific machine PC Machine Languages Machine code for a specific machine Input/Output Hardware 3 4 C++ System Environment Imperative vs. Object-Oriented Programming with C++ Source code Object code C++ Programming Language Object Oriented Paradigm Text editor Preprocessor Compiler Linker C Programming Language Imperative Paradigm Modified Source code Executable Code C++ is a super set of C 5 6 1

Review for COSC120 Input / Output Where we start to write a program? Keyboard Inputs Output File 1. Plan and Design Goals of program. Inputs Outputs Mouse File Program Monitor 2. Modularization Divide and Conquer 3. Write program module by module 4. Compile the program 5. Test the program 7 8 Primitive Data Types( Integer) Primitive Data Types (Decimal to Binary) Integer Types short unsigned short 2 bytes 2 bytes - 2 16-1 ~ + 2 16-1 -1 0 ~ 2 16-1 Ex) convert decimal number 13 10 to binary 13 10 = 8 + 4 + 1 =2 3 + 2 2 + 2 0 = 1 2 3 + 1 2 2 + 0 2 1 + 1 2 0 int unsigned int long unsigned long - 2 32-1 ~ + 2 32-1 -1 0 ~ 2 32-1 - 2 32-1 ~ + 2 32-1 -1 0 ~ 2 32-1 = 1101 2 13 / 2 = 6, remainder 1 6 / 2 = 3, remainder 0 3 / 2 = 1, remainder 1 1 / 2 = 0, remainder 1 9 10 Primitive Data Types (Binary to Decimal) Ex) Convert a binary number 10001 2 to a decimal number 10001 2 = 1 2 4 + 0 2 3 + 0 2 2 + 0 2 1 + 1 2 0 = 16 + 0 + 0 + 0 + 1 = 17 10 Modern computer system use two s complementation for present a integer number To represent a positive number, use the convention adopted for an unsigned integer. To represent a negative number, 1. Convert quantity of negative decimal to binary. 2. Complement the positive number (change from 0 to 1 or from 1 to 0) One s complement number. 3. Add 1 to one s complemented number. 4. Store in a memory 11 12 2

How to store integer +25 in the 8 bit memory space 1. Convert 25 to binary number How to store negative integer 25 in the 8 bit memory space 1. Convert 25 to binary number 00011001 2. Save in a memory 00011001 2. one s complementation RAM RAM 11100110 11100111 3. two s complementation 4. Save in a memory 13 14 How to interpret a two s complement format integer value stored in the memory RAM Most significant bit is 0 + If the most significant bit is 0 (positive) Change binary number to decimal Put a plus sign in front of the number If the most significant bit is 1 (negative) Leave the rightmost bits up to the first 1 unchanged. Complement the rest of the bits Change the whole number from binary to decimal Put a negative sign in front of the number. 01001100 Convert to decimal 2 6 + 2 3 + 2 2 = 76 +76 15 16 Primitive Data Types (Character) RAM Most significant bit is 1 10110000 Get a quantity of negative number 01010000 Convert to decimal 2 6 + 2 4 = 64 + 16 =80 80 17 18 3

Primitive Data Types (Character) A : a character A ASCII code 56. Need 1 byte to store 01000001 A : a string A with length 1. Need 2 byte to store. 1 byte for ASCII code A (56) and 1 byte for the ASCII code for end of line character (\0). 01000001 00000000 (Primitive Data Type Character: Coercion or Type Casting) C and C++ is one of the few languages to allow coercion, that is forcing one variable of one type to be another type // This program demonstrates the coercion in C++ char letter; letter = 65; letter = 66; 19 20 (Primitive Data Type Character: Coercion or Type Casting) (Primitive Data Type Floating Point Number) // This program demonstrates the coercion in C++ Floating point data types are for real numbers. float 3.4 10-38 ~ 3.4 10 38 double 8 bytes 1.7 10-308 ~ 1.7 10 308 int letter; long double 8 bytes 1.7 10-308 ~ 1.7 10 308 C++ support scientific notation for presenting a real number by using E notation letter = 'A'; letter = 'B'; Decimal Notation Scientific Notation E Notation 123.45 1.245 10 2 1.2345E2 0.000012345 1.245 10-5 1.2345E-5 21 22 (Primitive Data Type Floating Point Number) (Primitive Data Type Boolean) When a floating-point value is assigned to an integer variable, the fraction part of value is discarded. Boolean variables only hold the values true or false. Usually false is assigned with 0 (zero) and true is assigned with positive integer. // This program uses floating point data types. // This program uses Boolean data types. int distance; bool TrueFalse; distance = 7.89; cout << "The distance is " << distance << endl; TrueFalse = true; cout << "True is print as " << TrueFalse << endl; TrueFalse = false; cout << "False is print as " << TrueFalse << endl; 23 24 4

(Mathematical Expression) You can construct complex mathematical expression by using arithmetic operators and grouping symbol parenthesis. When you construct a mathematical expression, you need consider associativity and precedence of each operators used. (Constant Variable) A constant variable is a variable whose content cannot be changed A constant variable must be initialized when it is declared. const double PI = 3.14159; // constant variable is declared and initiated double area, radius, circumference; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * radius * radius; circumference = 2 * PI * radius; cout << "The area of circle is " << area << endl; cout << "The circumference of the circle is " << circumference << endl; 25 26 (Formatted Output with <iomanip> Library) (File Processing: Reading Data From a File) setw(n) setprecision(n) fixed manipulator with setprecision(n) showpoint manipulator left and right manipulator Read data From a File 1. include the fstream header file; 2. declare a variable of type ifstream create object to handle a file 3. open the file by using the function open in ifstream object 4. check for an open file error 5. read data from the file 6. after each read, check for end-of-file using the eof() member function 7. close the file when access is no longer needed 27 28 (File Processing: Reading Data From a File) (File Processing: Writing Data To a File) // This program reads values from the file 'outfile.txt' // and echoes them to the display until a negative value // is read. #include <fstream> #include <cstdlib> // for exit function ifstream indata; // create ifstream object to read a data int num; indata.open("outfile.txt, ios::in); // opens the file if (!indata) // checking whether outfile.txt can open or not cerr << "Error: file could not be opened" << endl; exit(1); indata >> num; while (!indata.eof()) // keep reading until end-of-file cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found indata.close(); //close the file 'outfile.txt' cout << "End-of-file reached." << endl; Writing Data To a File 1. include the fstream header file; 2. declare a variable of type ofstream create ofstream object 3. open the file 4. check for an open file error 5. use the file 6. close the file when access is no longer needed 29 30 5

(File Processing: Writing Data To a File) // This program output values from an array // to a file named outfile.txt #include <fstream> #include <cstdlib> // for checking error for opening a file ofstream outdata; int i; int num[5] = 4, 3, 6, 7, 12; (Relational Operators) Relational operators are used to compare numerical values. A comparison result is true or false. Operator Name Relational Operators Syntax outdata.open("outfile.txt",ios::out); // create a file Less Than a < b if(!outdata ) Less Than or Equal To a <= b cerr << "Error: file could not be opened" << endl; exit(1); Greater Than a > b for (i=0; i<5; ++i) Greater Than or Equal To a >= b outdata << num[i] << endl; // write data to the file Not Equal To a!= b outdata.close(); // close file Equal To a = = b 31 32 6