CS114-009 Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, e-mail group solution to in-class.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Computer Programming w/ Eng. Applications
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CS Class 16 Today Announcements Review for Exam
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Loops.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Chapter 6: User-Defined Functions I
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter 6: User-Defined Functions I
Programming is instructing a computer to perform a task for you with the help of a programming language.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
User-Defined Functions
Let’s all Repeat Together
Chapter 6: User-Defined Functions I
Objectives You should be able to describe: The while Statement
Introduction to Functions
Presentation transcript:

CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class exercise before Thurs. 9/18 at 9 am  Programming Project #2, due Tues. 9/23 by midnight  Today’s lecture: pages ( )

Class exercises (group project – give solutions in class) Write a C++ program that reads in 20 integers, counts the number of odd and even numbers, and prints out the number of odd and even numbers  Hint: what does num%2 do? Write a C++ program that reads in 10 integers and prints out the largest and smallest values seen Write a C++ program that reads in 20 characters (you will only see vowels in the input – a, e, i, o, and u) and prints out the vowel that occurred the most often Important: figure out your algorithm before you start to code these problems

Can have Nested Loops #include using namespace std; int main(){ int a=0,b=0; while (a< 2){ cout << “a= “ << a << endl; while (b < 3) { cout<<“b= “ << b << endl; b++; } b=0; a++; } return 0; } What is the output of this program?

Loops Loops involve  Initialization  Test for continuation  Update action The for statement  clean iteration loop  equivalent to while  semi-colons (shown in red) are important Basic syntax for ( initialization ; test to continue ; update action) { statement(s); }

Example: for and while loops int main( ) { int z; z = 0; while (z < 10) { cout<< z << endl; z++; } return 0; } int main( ) { int z; for (z=0; z<10; z++) { cout << z << endl; } return 0; } // purple = initialization // red = test to continue // green = updates

Comments on the for loop Can declare counter variables that are “local” to this loop int main( ) { for (int a=0; a<10; a++) cout << a << endl; for (int b=10; b>=0; b--) cout << b << endl; } Can do nothing or more than one statement in each part of the for loop int main( ) { int a, b; for (a=0, b=1; a<5; a++, b*=2) cout << a+b << endl; for (a=0; a<5; ) cout << a++ << endl; }

Class Exercises What is the output of the following? #include using namespace std; int main( ) { for (int a=0; ; a++) cout << "hello world" << endl; return 0; } What modification(s) should be made to display “hello world” exactly 12 times?

Class Exercises Write using while #include using namespace std; int main( ) { for (int a=0, b=5; a < b; a++, b--) cout << "YES" << endl; for (int z=10; z>5; z--) cout << "NO" << endl; return 0; } write using for #include using namespace std; int main( ) { int a=5, b=15; while (a < b) { cout << "UA" << endl; a += 2; b--; } return 0; }

Exercise Write a C++ program that reads in ten real numbers and prints out the two largest values that you read.

Programs will get bigger Examples  Operating Systems DOS, Windows 3.1, Windows 95, Windows 2000, Windows XP, Vista Currently about 40M lines  Word processing Office 2007 is version 12 of Office  myBama

Need to organize programs Break into pieces  Functions Why?  Can’t handle one single main function with over 1,000,000 lines of code  Allows us to reuse what others have done

Two basic types of functions Pre-defined functions (built-in functions)  Basic activities cin&cout  math related operations Square root Sine and cosine User-defined functions (programmer-defined)  must write these yourself

Today: pre-defined functions Code has already been written One small issue  Not all functions are automatically loaded  Need to inform compiler if using functions #include statements

#include using namespace std; int main( ) { double a, b, ans; cout << "Enter a & b: "; cin >> a >> b; ans = pow (a, b); cout << ans << endl; return 0; } Example: compute a b #include using namespace std; int main( ) { int a, b, ans = 1; cin >> a >> b; while (b > 0) { ans = ans * a; b = b – 1; } cout << ans << endl; return 0; }

#include using namespace std; int main( ) { double a, b, ans; cout << "Enter a & b: "; cin >> a >> b; ans = pow (a, b); cout << ans << endl; return 0; } Comments on compute a b Don’t forget include file Syntax for calling a function variable=function(params);  Parameters are values passed to the function Other variables Numbers  Parentheses are mandatory, even with no parameters  Don’t always need to assign the results

Pre-defined Math Functions abs  Requires #include  Return the absolute value double x, y, x = fabs( y ); Table on page 185 (187) has common mathematical functions pow  Requires #include  Return x y double x, y, z, z = pow( x, y ); sqrt  Requires #include  Returns square root double x, y, y = sqrt( x );

Class Exercises Write a C++ program that:  reads in four integers the first two numbers represent the x and y coordinates for point one the last two numbers represent the x and y coordinates for point two  calculates the distance between those two points This should be a double  Prints the distance Dist =  (x 1 -x 2 ) 2 + (y 1 -y 2 ) 2

End of Class 08 Read pages for next time.