COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.”

Slides:



Advertisements
Similar presentations
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Advertisements

Week 8 Arrays Part 2 String & Pointer
1 Various Methods of Populating Arrays Randomly generated integers.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 10 Introduction to Arrays
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 7: User-Defined Methods
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
COSC 1P03 Data Structures and Abstraction 1.1 Cosc 1P03 Week 1 Lecture slides “For myself, I am an optimist--it does not seem to be much use being anything.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
COSC 1P03 Data Structures and Abstraction 1.1 File Processing You've achieved success in your field when you don't know whether what you're doing is work.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
COSC 1P02 Introduction to Computer Science 8.1 Cosc 1P02 “For myself, I am an optimist--it does not seem to be much use being anything else.” Winston Churchill.
COSC 1P02 Intro. to Computer Science 8.1 Cosc 1P02 Only those who attempt the absurd can achieve the impossible.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Computer Programming for Engineers
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
COSC 1P02 Introduction to Computer Science 5.1 Cosc 1P02 Week 5 Lecture slides Psychiatrist to patient "You have nothing to worry about - anyone who can.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 21 Arrays – Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays.
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Arrays An Array is an ordered collection of variables
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Review for Final Exam.
Review for Final Exam.
Presentation transcript:

COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.” Albert Schweitzer

COSC 1P02 Introduction to Computer Science 1.2 Arrays  collections of values (objects)  elements  use  declare  create  process  memory model  length attribute  Returns the size of the array.  RainFall Example  Print out only those months with above average rainfall.  Palindrome Example  Strings as array of char  toCharArray & String(char[])

COSC 1P02 Introduction to Computer Science 1.3 RainFall private void display ( ) { String year; // year double[] rainfall; // rainfall for each month double totRain; // total rainfall for the year double aveRain; // average monthly rainfall year = in.readString(); rainfall = new double[12]; totRain = 0; for ( int i=0 ; i<rainfall.length ; i++ ) { rainfall[i] = in.readDouble(); totRain = totRain + rainfall[i]; }; aveRain = totRain / rainfall.length; writeHeader(year,aveRain); for ( int i=0 ; i<rainfall.length ; i++ ) { if ( rainfall[i] > aveRain ) { writeDetail(i,year,rainfall[i]); }; in.close(); out.close(); }; // display Declares the array variable rainfall Creates the array object of size 12 Length attribute returns the length of the rainfall array Store in rainfall at index location i. Scan through the rainfall array printing only those values greater then the average rainfall

COSC 1P02 Introduction to Computer Science 1.4 Processing “Variable-sized” Arrays  Example: standard deviation of student marks  data file  Array of Student objects  Array size not known  choose arbitrary size (constant)  keep count  Array organization  not all elements used  Std Dev formula:

COSC 1P02 Introduction to Computer Science 1.5 Data File COSC 1P02 Term Test Ima First Mary Marvelous Pretty Good Im Ok Joe Average60

COSC 1P02 Introduction to Computer Science 1.6 Output

COSC 1P02 Introduction to Computer Science 1.7 Memory Model

COSC 1P02 Introduction to Computer Science 1.8  Reading data  traversal  Student constructor  termination  no more data  no more space  error  Computing standard deviation  array as parameter  second traversal  Traversal pattern

COSC 1P02 Introduction to Computer Science 1.9 Traversal Pattern

COSC 1P02 Introduction to Computer Science 1.10 Std. Dev private void genReport ( ) { String course; // course name String work; // name of piece of work Student[] students; // the class of students Student aStudent; // one student int numStd; // number of students double sum; // sum of marks double ave; // average mark double std; // standard deviation course = stData.readString(); work = stData.readString(); setUpReport(course,work); students = new Student[MAX_STD]; numStd = 0; sum = 0; while ( true ) { aStudent = new Student(stData); if ( stData.isEOF() | numStd >= MAX_STD ) break; students[numStd] = aStudent; numStd = numStd + 1; sum = sum + aStudent.getMark(); writeDetail(aStudent); }; ave = sum / numStd; std = computeStd(students,numStd,ave); writeSummary(ave,std); report.close(); stData.close(); }; // genReport Declare a Students array Create the array, Size MAX_STD Counter used to keep track of the number of students read. Put student into array indexed by numStd. Increment numStd so next array location can be used. Exit condition is EOF of data file or the array is out of room

COSC 1P02 Introduction to Computer Science 1.11 Calculating Std. Dev private double computeStd ( Student[] students, int numStd, double ave ) { double sum; // sum of squares of deviations double aMark; // a student mark sum = 0; for ( int i=0 ; i<numStd ; i++ ) { aMark = students[i].getMark(); sum = sum + pow(aMark-ave,2); }; return sqrt(sum/numStd); }; // computeStd Formal parameter students will point to the Students array. Pass the number of Student objects in the array Loop is run from 0 to numStd, the number of Student objects in the array

COSC 1P02 Introduction to Computer Science 1.12 Arrays vs Sounds

COSC 1P02 Introduction to Computer Science 1.19 Reversing a char array O L I V E R R E V I L O R L I V E O R E I V L O

COSC 1P02 Introduction to Computer Science 1.20 Palindrome Revisited private void checkPalindromes ( ) { int button; Stringstr;// string to be checked as palindrome Stringreversed;// reversed version of str while ( true ) { button = form.accept(); if ( button==1 ) break; str = form.readString("in"); reversed = reverse(str); if ( str.equalsIgnoreCase(reversed) ) { form.writeString("out","This is a palindrome"); } else { form.writeString("out","This is not a palindrome"); }; };// checkPalindromes Read in the candidate from “InString” Call method reverse which reverses the input. Check to see if the original and the reversed are the same, if so then YES! Appropriate message is written to the Result field

COSC 1P02 Introduction to Computer Science 1.21 Palindrome Revisited. private String reverse ( String str ) { char []theString;// string as array of characters charc; inti; theString = str.toCharArray(); for ( i=0 ; i<theString.length/2 ; i++ ) { c = theString[i]; theString[i] = theString[theString.length-1-i]; theString[theString.length-1-i] = c; }; return new String(theString); };// reverse Define a character array Method converts the string to a right sized character array Loop runs 1/2 the length of the char array Swap the first and last char Last is defined by length -1. As i is increased the upper index is decremented proportionately. Constructor converts the character array back to a string. Which is returned by the method.

COSC 1P02 Introduction to Computer Science 1.22 Enrollments public class UStats { private final String []UNIVS = {" Adams"," Beacon"," Madison"," Lexington"}; private final String []DEPTS = {"Math.","Business","Comp. Sci.","Biology","French"}; private ASCIIDataFiledataFile;// file for input private ASCIIDisplayerout;// file for report public UStats ( ) { dataFile = new ASCIIDataFile(); msg = new ASCIIDisplayer(); out = msg; display(); dataFile.close(); out.close(); };// constructor private void display ( ) { int [][] enrol;// enrolment stats int [] uTotals;// University totals int [] dTotals;// Department totals int total;// grand total enrol = new int[DEPTS.length][UNIVS.length]; readStats(enrol); uTotals = sumRows(enrol); dTotals = sumCols(enrol); total = sumAll(enrol); writeStats(enrol,uTotals,dTotals,total); };// display Arrays of strings used for the row and column headings Array declarations for enrolment table, row & column stats Rows are represented by the departments, Columns by the universities. Length of each dimension is based on the heading lengths.

COSC 1P02 Introduction to Computer Science 1.23 What is a Palindrome?  A string which reads the same forward and reverse.  E.g.  This is not a palindrome Happy like a fox  These are all palindromes No devil lived on I did did I Plum was I ere I saw mulp Sleep on no peels Zeus sees Suez

COSC 1P02 Introduction to Computer Science 1.24 Array Declarations

COSC 1P02 Introduction to Computer Science 1.25 Good Luck on your Exam!!