Week4. Huayi Li

Similar documents
Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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.

Why Is Repetition Needed?

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

EP578 Computing for Physicists

Local and Global Variables

1st Midterm Exam: Solution COEN 243: Programming Methodology I

do { statements } while (condition);

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

In this chapter you will learn:

Chapter 17 - Notes Recursion

University of Dublin

Programming Language. Functions. Eng. Anis Nazer First Semester

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Introduction to Programming

REPETITION CONTROL STRUCTURE LOGO

Chapter 10 - Notes Applications of Arrays

1 Unit 8 'for' Loops

Functions and Recursion

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Notes on the 2008 Exam

Introduction to Algorithms and Programming (COMP151)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Something we should know

Unit 6. Thinking with Functions

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

b. Suppose you enter input from the console, when you run the program. What is the output?

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Data Structures II Lesson 1 (Circular Queue)

COMP 208 Computers in Engineering

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

A SHORT COURSE ON C++

Scientific Computing

C++ Support Classes (Data and Variables)

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Chapter 4 - Notes Control Structures I (Selection)

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

CS Semester I. Quiz 1 (version A)

Separate Compilation of Multi-File Programs

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Computer Programming

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

Lab Instructor : Jean Lai

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

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

Name SECTION: 12:45 2:20. True or False (12 Points)

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Linked List using a Sentinel

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

Increment and the While. Class 15

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Computer Engineering 1 (1E3)

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Computer Programming

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Introduction to C/C++

Control Structures of C++ Programming (2)

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Chapter 7 - Notes User-Defined Functions II

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

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

Example 3. #include <iostream> using namespace std; int main()

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Tree Travsersals and BST Iterators

CS101 PLEDGED SPRING 2001

Solving an Oblique Triangle

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

Computer Programming

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

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

Object Oriented Design

Number: Name-Surname :... Group:...

CS Data Structure Spring Answer Key- Assignment #3

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

The following expression causes a divide by zero error:

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

CSC 330. An Exception is. Handling Exceptions. Error Handling. Assertions. C++ Exception. Example

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

C++ Programming for Non-C Programmers. Supplement

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

pointers + memory double x; string a; int x; main overhead int y; main overhead

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Templates and Vectors

Apllications. March 03, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications.

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 27 CRASH COURSE IN C++ MICHAEL GROSSBERG

Transcription:

Week4 Huayi Li 2011-9-15

1. Summary of Quiz1

2. An example of if-else statement Recall that, given a Quadratic ax 2 + bx + c = 0 if a = 0, then problem reduces to linear equation. x = -c /b if b 0 x is unsolvable if b=0 and c 0 x is arbitrary if b=0 and c=0 If a 0 let = b*b 4*a*c if <0 there are two complex roots ( -b ± sqrt(- ) i) /(2a) if =0 there is one real root -b/(2a) if >0 there is two real roots ( -b ± sqrt( ) ) /(2a) C++ codes can be downloaded from my webpage http://www.cs.uic.edu/~hli/#tabs-9

If(A) some codes; If(A) { some codes; 3. Some notes on If-else If some codes = 1 (# of statements), then the above two are equivalent, otherwise not. Conclusion: { } can be omitted only when there is only one statement under if or under else

3. Some notes on If-else cont d #include<iostream> using namespace std; intmain(){ boola = false; cout << " \n\nnow a is false see the difference below " <<endl; if(a) cout<<" 1st code statement under if a"<<endl; cout<<" 2nd code statement 'under' if a"<<endl; // this statement is // supposed not to be executed // however no { } are used, the statement is actually outside if(a) system("pause"); }

3. Some notes on If-else cont d else always matches the last if if( A) { if (B){ if(c){ else{ // which if this else matches? --- if(c) else{ // which if this else matches? --- if(a)

4. SimpleFunction // simple function, no parameters (input), no return value (output) // try to debug it #include<iostream> using namespace std; void f(){ cout<<"f() is called\n"; void g(){ cout<<"g() is called\n"; void h(){ f(); g(); intmain(){ f(); h(); system("pause");

5. Local variables and global variables #include<iostream> using namespace std; intglobala= 1; void func(){ cout<<"locala="<<locala<<endl; //error main.localais invisible to func() cout<<"globala="<<globala<<endl; intmain(){ intlocala= 2; cout<<"locala="<<locala<<endl; cout<<"globala="<<globala<<endl; func(); system("pause");

6. Incremental Programming on HW2

6.1 Version 2.1 #include<iostream> using namespace std; intmain(){ intopt; cout <<"====================Menu======================="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; system("pause");

6.2 version2.2 // version 2.2 // add while loop to allow user use it for whatever times he wants #include<iostream> using namespace std; intmain(){ while(true){ // new codes for while int opt; cout <<"====================Menu======================="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "5. Exit " << endl; // new codes for menu 5 cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; case 5: system("pause"); return 0 ; break; // new codes for case 5 system("pause"); return 0;

// version 2.3 // 2.1 -> 2.2 add while loop to allow user use it for whatever times he wants // 2.2 -> 2.3 add system clear to let user have a better view #include<iostream> using namespace std; intmain(){ while(true){ intopt; cout <<"====================Menu======== ==============="<<endl; cout << "1. SSS Problem " << endl; cout << "2. ASA Problem For Single Triangle " << endl; cout << "3. ASA Problem For Multiple Triangles" << endl; cout << "4. SAS Problem " << endl; cout << "5. Exit " << endl; 6.3 version2.3 cout << "choose the option to proceed "<<endl; cin >> opt; switch(opt){ case 1: cout<<"solving SSS"<<endl; break; case 2: cout<<"solving ASA For Single Triangle"<<endl; break; case 3: cout<<"solving ASA For Multiple Triangles"<<endl; break; case 4: cout<<"solving SAS"<<endl; break; case 5: system("pause"); return 0 ; break; system("pause"); // new codes system("cls"); // new codes system("pause"); return 0;

// version 2.4 // 2.1 -> 2.2 add while loop to allow user use it for whatever times he wants // 2.2 -> 2.3 add system clear to let user have a better view // 2.3 -> 2.4 use function to ease our maintenance and readability #include<iostream> using namespace std; void printmenu(){ // new codes defining a printmenufunction cout <<"====================Menu======================="<<endl; cout<< "1. SSS Problem " << endl; cout<< "2. ASA Problem For Single Triangle " << endl; cout<< "3. ASA Problem For Multiple Triangles" << endl; cout<< "4. SAS Problem " << endl; cout << "5. Exit " << endl; cout<< "choose the option to proceed "<<endl; } void SSS(){ // new codes defining a Function solving SSS problem cout<<"solving SSS"<<endl; } void ASASingle(){ // new codes defining a Function solving ASASingle problem cout<<"solving ASA For Single Triangle"<<endl; } void ASAMultiple(){ // new codes defining a Function solving ASAMultipleproblem cout<<"solving ASA For Multiple Triangles"<<endl; } void SAS(){ cout<<"solving SAS"<<endl; } 6.4 version 2.4 intmain(){ while(true){ intopt; printmenu(); // replaced with newcodes cin >> opt; switch(opt){ case 1: SSS(); break; case 2: ASASingle(); break; case 3: ASAMultiple(); break case 4: SAS(); break; case 5: system("pause"); return 0 ; // switch system("pause"); system("cls"); //while system("pause"); return 0; }// main

6.5 version 2.5.. double side1,side2,side3 ; // global variabelssuch that all functions have access to them double angle1, angle2,angle3;. void SSS(){ // real codes here void ASASingle(){ // real codes here void ASAMultiple(){ // real codes here void SAS(){ // real codes here intmain(){. }