22C:21 Problem 2.3 Solution Outline.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Mock test review Revision of Activity Diagrams for Loops,
Fibonacci Numbers A simple example of program design.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Building Java Programs
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Object-Oriented Programming Simple Stack Implementation.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Chapter 8 – Arrays and Array Lists
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
USING ECLIPSE TO CREATE HELLO WORLD
CSCI 161 – Introduction to Programming I William Killian
8-Queens Puzzle.
Repetition-Counter control Loop
Algorithm Design and Analysis (ADA)
Data Structures Array - Code.
Decision statements. - They can use logic to arrive at desired results
Computing Adjusted Quiz Total Score
An Introduction to Java – Part I, language basics
Lecture 7: Linked List Basics reading: 16.2
Building Java Programs
Data Structures Array - Code.
Building Java Programs
CS 180 Assignment 6 Arrays.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
1D Arrays and Lots of Brackets
Methods and Data Passing
Arrays in Java.
Algorithms: Design and Analysis
Building Java Programs
Building Java Programs
Drawing complex figures
EECE.2160 ECE Application Programming
Array Review Selection Sort
Learning Plan 5 Arrays.
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
CIS 110: Introduction to Computer Programming
CIS 110: Introduction to Computer Programming
Principles of Object Oriented Programming
Presentation transcript:

22C:21 Problem 2.3 Solution Outline

Data Structure to store public class sparseMatrix { int row; int column; sparseMatrix (int r, int c) { this.row = r; this.column = c; }

The compress( ) Method public void compress() { // sparseMatrix[] compressed = new sparseMatrix[numEdges]; System.out.println("The compressed form:"); // Search and print for(int i = 0; i < Edges.length; i++) for(int j = 0; j < i; j++) if(Edges[i][j] == true) System.out.println("(“ + i + ",“ + j + ")"); // It makes sense to store and return the array, // But for testing purposes we will just print here. }

Main( ) in sparseGraph class public static void main(String[] args) { sparseGraph G = new sparseGraph(100); // Adding 100 vertices for(int i=0;i<100;i++) G.addVertex(Integer.toString(i)); // Adding 4 edges G.addEdge("12", "25"); G.addEdge("12", "19"); G.addEdge("29", "45"); G.addEdge("99", "56"); G.compress(); }

Symmetric search area Ignore No self-loop Search

Output The compressed form: (19,12) (25,12) (45,29) (99,56)

Quiz 2: Problem 2.2 (modified) Suppose that we use the myGraph class to build a graph. We start with an empty graph and first add vertices in the order: a, b, c, d, e, f, g. Then we add edges in the order {a,b},{a,c},{a,d},{b,f},{a,f},{d,e},{g,f}. After all of these additions show the contents of the four data members of the myGraph class: names, Edges, numVertices, and numEdges. Make sure you follow the code in the myGraph class carefully and show the contents of the variables exactly. Make sure you pay attention to how the arrays are resized. Draw the final graph.