CS Class 16 Today Announcements Review for Exam

Slides:



Advertisements
Similar presentations
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Advertisements

Computer Science 1620 Loops.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
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 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
Functions:Passing Parameters by Value Programming.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Review of C++ Basics.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
Functions Why we use functions C library functions Creating our own functions.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Count and add list of numbers From user input and from file.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CS Class 23 Today  MergeSort Code  Review for Exam 3 Announcements  Exam 3 – Nov. 25 th in class Closed book, closed-notes Bring ½ sheet of notebook.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
CS 1428 Exam II Review.
CS 1428 Exam II Review.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS150 Introduction to Computer Science 1
File Review Declare the File Stream Object Name
Lab 1 Introduction to C++.
Pass by Reference.
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Statements and flow control
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

CS114-009 Class 16 Today Announcements Review for Exam Programming Project #4 due by midnight Exam 2 is Tuesday, 10/28 during class Closed book, closed-notes Bring ½ sheet of notebook paper with hand-written notes on it No calculators needed 0 – 2

Exam 2 Topics: Writing and tracing functions Nested loops Void and non-void functions Prototype vs. definition Parameters: - pass by value, reference, arrays Functions can call other functions Nested loops Reading from /writing to files Testing for eof Formatting output

Programming Project #4 Do not assume there will only be 2 lines of data There could be many more lines of data

Class Exercise Write a version of FindMin that takes four parameters (four integers). Hint: The easiest way is to invoke some of the other versions that have been written above for a smaller number of arguments. Can you write it in one line of code? 15-26 int FindMin(int w, int x, int y, int z) { return FindMin( FindMin(w,x), FindMin(y,z) ); int FindMin (int w, int x, int y, int z) { int min1 = FindMin (w, x); int min2 = FindMin (y, z); return FindMin (min1, min2); }

#include <iostream> using namespace std; int FindMin(int, int); int FindMin(int, int, int); int main( ) { int a, b, c, d, x, y, z; cin >> a >> b >> c >> d; x = FindMin(a,b); y = FindMin(c,d); z = FindMin(x,y); cout << z << endl; // This is better! x=FindMin(a,b,c); z=FindMin(x, d); return 0; }

int FindMin(int x, int y) { if (x < y) return x; else return y; } int FindMin(int x, int y, int z) { return FindMin(x,z); return FindMin(y,z); // one line of code: // z=FindMin(FindMin(a,b), FindMin(c,d))

Exam Review

In-Class Exercise Write a C++ program that will read the ten numbers shown below from the file data.txt and write the even numbers to the file even.txt and the write the odd numbers to the file odd.txt. 5 8 10 13 15 20 23 25 29 30 You can create the input file data.txt by either 1) using Notepad 2) File / Add New Item / <select a text file> Make sure your input data file is in the same directory as your C++ project. 31 – 45 This is a “harder” class exercise in that they have to actually do something. You might want to put the previous slide back up for them to reference after you’ve discussed the assignment with them

#include<iostream> #include<fstream> using namespace std; // You can find the results from this program in the 2 files odd.txt and even.txt // these files will appear in your project folder - just look for them // Remember, you must create the data.txt file yourself int main () { int value; ifstream Infile; Infile.open("data.txt"); ofstream Oddfile; Oddfile.open("odd.txt"); ofstream Evenfile; Evenfile.open("even.txt"); for(int i=0; i<10; i++){ Infile >> value; if (value%2==0) Evenfile << value << " "; else Oddfile << value << " "; } return 0;

Class Exercises Write a program that generates the output below: 1 10 100 1000 10000 100000 Write a program that generates the output below: 0 1000 250 750 500 500 750 250 1000 0 45 – 50 Have them do one or the other

#include<iostream> using namespace std; int main () { int num=1; while (num <= 100000) { cout.setf(ios::right); cout.width(6); cout << num << endl; num=num*10; } return 0;

#include<iostream> using namespace std; int main () { for (int i=0; i<5; i++){ cout.setf(ios::left); cout.width(7); cout << (i*250); cout.setf(ios::right); cout << (1000-250*i) << endl; cout.unsetf(ios::right); // if both right and left set, // the right dominates, so must unsetf to set left again } return 0;

Class Exercise Write C++ programs that use nested for loops to display one of the following designs: XXXXXX XXXXXXXXXX XXXXX XXXXX XXXXXXXX X X XXXX XXXXXX X X XXX XXXX X X XX XX X X X XXXXX 18 – 35 Have them figure out two of these

#include <iostream> using namespace std; int main( ) { int i, j, k; for (i=6; i>0; i--){ for (j=(6-i); j>0; j--){ cout << " "; } for (j=0; j<i; j++){ cout << "X"; cout << endl << endl;

cout << endl << endl; for (i=0; i<5; i++){ for (j=0; j<i; j++){ cout<< " "; } for (int k=10-i*2; k>0; k--){ cout << "X";

for (int j=0; j<5; j++){ if (i==0 || i==5 || j==0 || j == 4) for (i=0; i<6; i++){ for (int j=0; j<5; j++){ if (i==0 || i==5 || j==0 || j == 4) cout << "X"; else cout <<" "; } cout << endl << endl;

// Another way to do this for (i=0; i<6; i++){ if (i%5 == 0) cout << "XXXXX" ; else { cout << "X"; for (j=0; j<3; j++) cout << " "; cout <<"X"; } cout << endl << endl; return 0;

Class Exercise Re-write your program from the start of class so that it uses three functions: One to read in the array values One to compute the average One to print all values greater than the average int main ( ) { int values[10]; read ( values ); double avg = average ( values ); cout << "average is : " << avg << endl; print ( values, avg ); return 0; } 41 - 43

#include<iostream> using namespace std; void read (int values []){ for (int a=0; a< 10; a++) cin >> values[a]; } double average(int values []){ double sum=0.0, avg; sum=sum+values[a]; avg=sum/10.0; return avg;

void print (int values[], double avg){ for (int a=0; a<10; a++) if (values[a] > avg) cout << values[a] << endl; } int main () { int values[10]; read(values); double avg=average(values); cout<< "average is: " << avg<< endl; print(values, avg); return 0;

End of Class 16 Read pages 545 – 553 (old text) for next time