DT249-Information Systems Research Practice-2014-15 Programming Revision Lecture 2 Lecturer: Patrick Browne.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Introduction to arrays
1 Various Methods of Populating Arrays Randomly generated integers.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Arrays Chapter 6 Chapter 6.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Using break and continue
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Sorting Algorithms: Selection, Insertion and Bubble.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 7 & 8- Arrays and Strings
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.
Computer Programming Lab(5).
CS1101: Programming Methodology Aaron Tan.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Java Programming: From Problem Analysis to Program Design, 4e
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Programming: From the Ground Up
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Java Programming Language Lecture27- An Introduction.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
CSC111 Quick Revision.
Introduction to programming in java
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Chapter 8 – Arrays and Array Lists
Two-Dimension Arrays Computer Programming 2.
Chapter 6 More Conditionals and Loops
TK1114 Computer Programming
CSS161: Fundamentals of Computing
Presentation transcript:

DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne

One-Dimensional arrays An array is an ordered collection, or numbered list, of values. All of the values in an array must be of the same type. The type of the array is the type of the values it holds, followed by the characters []. An index is used to refer to individual values in the array. If we have N values, we think of them as being numbered from 0 to N-1. Each cell of the array called num contains a value.

1 dimensional arrays class ArrayApp1 { public static void main (String[] args) { // 8 elements indexed 0 to 7 int a[] = {17,3,60,128,11,11,2,8}; for (int i=0; i<8; i++) System.out.print(a[i] + " ");}} Make the above program omit the last element. Make the above program omit the first element. If we have N values, we think of them as being numbered from 0 to N-1.

Two dimensional array Each cell of the array contains a value

Two dimensional array import javax.swing.JOptionPane; public class ArrayApp2 { //... Define named constants to centralize definitions static final int ROWS = 2; static final int COLS = 4; public static void main(String[] args) { int[][] a2 = new int[ROWS][COLS]; String output = ""; // Accumulate text here //Print array in rectangular form using nested for loops. for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { output += " " + a2[row][col];} output += "\n";} JOptionPane.showMessageDialog(null, output);}} Assign the row value to each element Assign the column value to each element.

Functions class FactApp { static public void main (String [ ] args) { int a = Integer.parseInt(args[0]); int f = factorial (a); System.out.println("Factorial of " + a + " is " + f); } static public int factorial (int n ) { int c = n - 1; int r; if (c > 0) r = n * factorial(c); else r = 1; return r; }} The factorial of a number N, denoted by N !, is the product of all positive numbers less than or equal to N. Takes input from command line. Change to take input from prompt. Make a function to increment an integer Make a function to add two integers

Procedures (methods) import java.util.Scanner; public class MeanApp { public static int numSamples; // Number of samples to be computed. public static float total=0.0f, // Total of all sample values. mean =0.0f; // Mean of the sample values. // Asks user for number of samples and gathers input from the keyboard. public static void gatherInput() { // Create a new KeyboardInput object for input. Scanner keyIn = new Scanner (System.in); int count; // Counts through samples. System.out.print("How many samples? :"); numSamples = keyIn.nextInt(); for (count=1; count <= numSamples; count++) {System.out.print("Sample number " + count + " :"); total += keyIn.nextFloat(); } // Do the mean calculation. mean = total / numSamples;} // Displays the results of sample mean calculations. public static void displayResults() { System.out.println("The mean of the " + numSamples + " samples is " + mean); } public static void main(String[] args){ gatherInput(); displayResults();}}

2D arrays

CSV2DArrayApp This program reads a comma separated file into a two dimensional array. Copy the program and data files, called male.csv and female.csv to your folder. The dimensions of the array are int [][] numbers = new int[13][3]; Change these to [20][20] and then to [12][3], and then to [13][2]

Sorting public static void Sort() { int list[] = {10,7,19,5,16}; sortList(list); }

Sorting do { displayList(list); listChanged = false; for (int i=0; i<list.length-1; i++) { if (list[i] > list[i+1]) {temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; listChanged = true;}} } while (listChanged == true);

Sorting swapping elements temp = list[i]; list[i] = list[i+1]; list[i+1] = temp;

Sorting do { displayList(list); listChanged = false; for (int i=0; i<list.length-1; i++) { if (list[i] > list[i+1]) {temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; listChanged = true;}} } while (listChanged == true);

Bubble Sort Algorithm Elements of array list during the first iteration Elements of array list during the second iteration

Bubble Sort Algorithm Elements of array list during the third iteration Elements of array list during the fourth iteration

Location Quotients To load CSV files see program CSVLocQuoApp.java Location quotients compare a local area's business composition to that of a larger area. In this case the two CSV files (male.csv and female.csv) hold the number of people employed in various industries in the Dublin area. See notes section below.