C C C C++ 2 ( ) C C++ 4 C C

Similar documents
Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

C C C C++ 2 ( ) C C++ 4 C C

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CS349/SE382 A1 C Programming Tutorial

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Structures and Pointers

CHAPTER : 9 FLOW OF CONTROL

Introduction to Object-Oriented Programming with C++

EL2310 Scientific Programming

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

CS 11 C++ track: lecture 1

Linked List using a Sentinel

Object oriented programming

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

Enumerated Types. Mr. Dave Clausen La Cañada High School

CS 225. Data Structures

Use the dot operator to access a member of a specific object.

CSCE Practice Midterm. Data Types

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Unit 7. 'while' Loops

EL2310 Scientific Programming

Intermediate Programming, Spring 2017*

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS 225. Data Structures. Wade Fagen-Ulmschneider

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Classes and Pointers: Some Peculiarities

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

A class is a user-defined type. It is composed of built-in types, other user-defined types and

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Introduction Of Classes ( OOPS )

do { statements } while (condition);

Lecture 12. Data Types and Strings

Programming. C++ Basics

การทดลองท 8_2 Editor Buffer Array Implementation

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

CS2141 Software Development using C/C++ C++ Basics

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

1 Short Answer (5 Points Each)

Objectives. Describe ways to create constants const readonly enum

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Due Date: See Blackboard

C++ For Science and Engineering Lecture 15

Your First C++ Program. September 1, 2010

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Programming in C++: Assignment Week 8

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Cloning Enums. Cloning and Enums BIU OOP

1 #include <iostream> 2 #include <stdlib.h> //srand, rand 3 #include <time.h>//clock_t, clock, CLOCKS_PER_SEC 4 using namespace std; 5 6 //

Why VC++ instead of Dev C++?

Inheritance, and Polymorphism.

Implementing an ADT with a Class

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Midterm Exam 5 April 20, 2015

Classes and Objects in C++

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Object oriented programming

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Ch 6. Functions. Example: function calls function

Computer Programming

CSCE Practice Midterm. Data Types

CS3157: Advanced Programming. Outline

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Interview Questions of C++

CS24 Week 3 Lecture 1

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

CS 225. Data Structures. Wade Fagen-Ulmschneider

CA341 - Comparative Programming Languages

True or False (15 Points)

Due Date: See Blackboard

CSC1322 Object-Oriented Programming Concepts

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Programming in C++: Assignment Week 4

CS 103 Unit 11. Linked Lists. Mark Redekopp

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

1 #include <iostream> 2 #include <stdlib.h> //srand, rand 3 #include <time.h>//clock_t, clock, CLOCKS_PER_SEC 4 using namespace std; 5 6 //

CS 31 Discussion: Final Week. Taylor Caulfield

Module 7 b. -Namespaces -Exceptions handling

CSCE 110 PROGRAMMING FUNDAMENTALS

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Part I: Short Answer (6 questions, 18 points total)

Object-Oriented Programming for Scientific Computing

Due Date: See Blackboard

CSE 333 Lecture smart pointers

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Project Identity Shampoo-Conditioner Identification Device/ Medicine Reminder Week 2: 1/28/07-2/3/07 Karla Sittnick

Due Date: See Blackboard

Transcription:

# 7 11 13 C 4 8 11 20 C 9 11 27 C++ 1 10 12 4 C++ 2 11 12 11 C++ 3 12 12 18 C++ 4 C++ 5 13 1 8 ( ) 14 1 15 C++ 15 1 22 2

(D) ( ) C++ 3

7 new delete 4

5

1. 0 99 1000 0 99. 0 hist[ r ]++; (*(hist + r ))++; [] 6

2. text.txt text.txt 1 1 malloc 7 5

3. switch enum enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday ; enum 8

4. x 3-168x 2 +8621x -129978 ( x - a) ( x - b) ( x -c) a, b, c 0<a<b<c a, b, c 3 0 x a, b, c x=1, 2, 3 0 x a, b, c 9

10

Calculator1 ex6_calculator1.cc Calculator2 Calculator1 ex7_calculator2.cc 11

12

Calculator1 Calculator2 13

ex6_calculator1.cc #include <iostream> using namespace std; class Calculator1 { private: double val; // public: Calculator1() { // clear(); // void clear() { // val = 0; double add(double val2) { // val += val2; return val; double sub(double val2) { // val -= val2; return val; // double get_val() { return val; ; 14

ex6_calculator1.cc int main() { string ope; // double n; // Calculator1 calc; // cout << "Input an operation and a number separated by space (ex. "+ 10 " to add 10)." << endl << "Input "= " to show the current value." << endl << "Input "clear " to clear." << endl << "Press Ctrl+c to quit." << endl; cout << "Current value: " << calc.get_val() << endl; 15

ex6_calculator1.cc while(1) { // cin >> ope; // if (ope=="+") { // "+" cin >> n; // cout << "Result: " << calc.add(n) << endl; continue; // if (ope=="-") { // "-" cin >> n; // cout << "Result: " << calc.sub(n) << endl; continue; // 16

if (ope=="=") { // "=" cout << "Current value: " << calc.get_val() << endl; continue; // if (ope=="clear") { // "clear" calc.clear(); cout << "Result: " << calc.get_val() << endl; continue; // cout << "Invalid command! ignored." << endl; ex6_calculator1.cc return 0; 17

$./a.exe Input an operation and a number separated by space (ex. "+ 10" to add 10). Input "=" to show the current value. Input "clear" to clear. Press Ctrl+c to quit. Current value: 0 + 10 Result: 10 + 100 Result: 110 18

- -50 Result: 160-1000 Result: -840 clear Result: 0 + 10 Result: 10 = Current value: 10 19

ex7_calculator2.cc #include <iostream> using namespace std; class Calculator1 { protected: // protected double val; // public: Calculator1() { // clear(); // Calculation1 void clear() { // val = 0; double add(double val2) { // val += val2; return val; double sub(double val2) { // val -= val2; return val; // double get_val() { return val; ; 20

ex7_calculator2.cc class Calculator2 : public Calculator1 { public: // double mul(double val2) { val *= val2; return val; // double div(double val2) { val /= val2; return val; ; Calculation1 Calculation2 21

ex7_calculator2.cc int main() { string ope; // double n; // Calculator2 calc; // Calculator1 Calculator2 cout << "Input an operation and a number separated by space (ex. "+ 10 " to add 10)." << endl << "Input "= " to show the current value." << endl << "Input "clear " to clear." << endl << "Press Ctrl+c to quit." << endl; cout << "Current value: " << calc.get_val() << endl; 22

ex7_calculator2.cc while(1) { // cin >> ope; // if (ope=="+") { // "+" cin >> n; // cout << "Result: " << calc.add(n) << endl; continue; // if (ope=="-") { // "-" cin >> n; // cout << "Result: " << calc.sub(n) << endl; continue; // 23

if (ope=="*") { // "*" cin >> n; // cout << "Result: " << calc.mul(n) << endl; continue; // if (ope=="/") { // "/" cin >> n; // cout << "Result: " << calc.div(n) << endl; continue; // ex7_calculator2.cc Calculation2 24

if (ope=="=") { // "=" cout << "Current value: " << calc.get_val() << endl; continue; // if (ope=="clear") { // "clear" calc.clear(); cout << "Result: " << calc.get_val() << endl; continue; // cout << "Invalid command! ignored." << endl; ex7_calculator2.cc return 0; 25

class Calculator1 { private: double val; // ex6_calculator1.cc protected class Calculator1 { protected: // protected double val; // ex7_calculator2.cc 26

public private protected private 27

Calculation1 ex7_calculator2.cc class Calculator2 : public Calculator1 { Calculation2 public: public private: private 28

29

C sizeof malloc free #include <stdlib.h> /* malloc */ ex8_malloc.c int main() { int *n; n = (int *)malloc(sizeof(int)*10); /* int 10 */ free(n); /* */ return(0); 30

C++ new sizeof n=new int; delete [] int main() { int *n; n = new int[10]; // int 10 delete [] n; // return(0); [] ex9_new_array.cc 31

32

#include <string> // string #include <iostream> // using namespace std; // ex10_new_obj.cc class Account { public: string name; // int balance; // ; int main() { Account *suzukip; suzukip = new Account(); // Account suzukip->name = " "; // suzukip->balance = 123000; // cout << suzukip->name << " " << suzukip->balance << " " << endl; delete suzukip; // return 0;

new Account *suzukip; new suzukip = new Account();. -> suzukip->balance (*suzukip).balance suzukip.balance Account* * Account delete 34 []

35

36

ex11_constructor.cc #include <iostream> using namespace std; // class dyn_mem { public: int *array; // int size; // dyn_mem() { // size = 0; // 37 size 0

ex11_constructor.cc dyn_mem(int _size) { // cout << "Constructor is called." << endl; size = _size; cout << "Memory is allocated." << endl; array = new int[size]; // ~dyn_mem() { // cout << "Destructor is called." << endl; if (size!=0) { // cout << "Memory is released." << endl; delete [] array; size = 0; ; 38

ex11_constructor.cc int main() { { cout << "A" << endl; dyn_mem dm(10); // ( 10 ) cout << "B" << endl; cout << "C" << endl; return 0; 39

$./a.exe --A-- Constructor is called. Memory is allocated. --B-- Destructor is called. Memory is released. --C-- 40

41

1. 42

2. 3 main g++ -c ex5_main.cc g++ -c ex5_coordinate.cc g++ ex5_main.o ex5_coordinate.o 43

3. 2 44

4. k new (k+1)*(k+1) 2 (x,y) (x,y) delete 45

k k (0,0) (k-1,k-1) (k,0) (k,k-1) (0,k) (k-1,k) (0,0) (k-1,k-1) (k,k) (0,0) (k,k) k=4 (0,0) (0,1) (0,2) (0,3) (0,4) (1,0) (1,1) (1,2) (1,3) (1,4) (2,0) (2,1) (2,2) (2,3) (2,4) (3,0) (3,1) (3,2) (3,3) (3,4) (4,0) (4,1) (4,2) (4,3) (4,4) 46

(.cc.cpp ) kadai1204.cc.cpp (Visual Studio ) kadai1204 _v.cc.cpp.txt report1204_.txt 47

12 18 48