Programming in OOP/C++

Size: px
Start display at page:

Download "Programming in OOP/C++"

Transcription

1 Introduction Lecture 3-2 Programming in OOP/C++ Arrays Part (2) By Assistant Professor Dr. Ali Kattan 1

2 Arrays Examples Solutions for previous assignments Write a program to enter and store your name and 4 marks. Then print your name with the average. You can only enter a single string and no spaces allowed When we use strings without initialization, we must declare a size. The size we choose should be suitable for our application (more, not less) 2

3 Arrays Examples Solutions for previous assignments Write a program to enter City name, Area Name and print the sentence: I live in area xxxxxx of yyyyyy city. When we use strings without initialization, we must declare a size. The size we choose should be suitable for our application (more, not less) 3

4 Arrays Examples Solutions for previous assignments Write a program to declare 2 double arrays of size N and store the multiplication result of element in a third array then print it. 4

5 Arrays of Strings Suppose that we want to make a list of items and their prices: Bread $1.5 Eggs $3.8 Meat $15.2 Milk $2.5 We want to represent the item name with its price inside a program? 5

6 Arrays of Strings In this case we will need two arrays, one to store the item s name and the other to store the price. The program below is an example to enter such lists. Pay attention to char item [size][12]; 6

7 Arrays of Strings char item [4][12]; There 4 strings each is a maximum of 12 characters Bread Eggs Meat Milk 7

8 Sorting Sorting means arranging the elements of an array in either descending order (from high to low), or ascending order (from low to high) Unsorted Sorted (ascending) Sorted (descending) 8

9 Sorting There are several sorting algorithms such as: Selection Sort. Bubble Sort. Counting Sort Insertion Sort. Quick Sort. Merge Sort. Heap Sort. We will study the first two only. Selection sort is explained in this lecture and bubble sort & counting sort is left as a homework. 9

10 Sorting: Selection Sort Swapping 10

11 Sorting: Selection Sort Understanding Swapping Before we explain Selection Sort, we explain what is swapping. Swapping is exchanging the values between 2 variables. For example if A=30 and B=50, swapping means that we want to exchange the values of A and B such that A takes the value of B and B takes the value of A. A = 30, B = 50; A = B; B = A; WRONG!! To do this we will need a 3rd variable, let s call it temp. 1. A is 30 and B is Store current A value in Temp. 3. A takes the current value of B. 4. B takes A s original value from temp temp = A; A = B; B = temp; 11

12 Sorting: Selection Sort Understanding The Algorithm Selection sort is a combination of searching and sorting. It uses arrays and nested loops to do the job. Outer loop starts from the first element of the array to the one before the last. Inner loop starts from current outer loop value+1 to last element of the array. See Video 12

13 Sorting: Selection Sort Understanding The Algorithm Steps for sorting array of 6 numbers. Outer loop is i, inner loop is j j i 13

14 Assignment 1. Write a program to enter 5 students names such that you use one array to enter first name and another array to enter last name. Print all the names after entry. 2. Write a program to enter 6 shopping list items that contains item name (string), item quantity (integer) and item price (float). Then find the total of the list (each item quantity x price then sum all). 3. Write a program to enter 10 integers then sort them in a descending way (from high to low). 14

15 Remember: If you don t practice C++ programming on the computer, YOU WILL NOT LEARN ANYTHING. To become a professional programmer you must try all the examples by yourself on a computer. 15

16 How can I get the lecture notes? Check the website One hardcopy will be provided after the lecture for photocopying. By ali.kattan@nobleinst.krd (if there is a problem only) 16

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Programming in C++ PART 2

Programming in C++ PART 2 Lecture 07-2 Programming in C++ PART 2 By Assistant Professor Dr. Ali Kattan 1 The while Loop and do..while loop In the previous lecture we studied the for Loop in C++. In this lecture we will cover iteration

More information

Sorting. Introduction. Classification

Sorting. Introduction. Classification Sorting Introduction In many applications it is necessary to order give objects as per an attribute. For example, arranging a list of student information in increasing order of their roll numbers or arranging

More information

Sorting Pearson Education, Inc. All rights reserved.

Sorting Pearson Education, Inc. All rights reserved. 1 19 Sorting 2 19.1 Introduction (Cont.) Sorting data Place data in order Typically ascending or descending Based on one or more sort keys Algorithms Insertion sort Selection sort Merge sort More efficient,

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Looking for something COMP 10 EXPLORING COMPUTER SCIENCE Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Searching algorithms Linear search Complexity Sorting algorithms

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

Understanding Sorting

Understanding Sorting Understanding Sorting Based on CBSE Curriculum Class -11 By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region INTRODUTION Generally, sorting means to categorize the objects as per their groups. A Computer

More information

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan Arrays in C DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) Array C language provides a

More information

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms Sorting Savitch Chapter. Why sort Easier to search (binary search) Sorting used as a step in many algorithms Sorting algorithms There are many algorithms for sorting: Selection sort Insertion sort Bubble

More information

Principles of Programming. Chapter 6: Arrays

Principles of Programming. Chapter 6: Arrays Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching

More information

Subject: Computer Science

Subject: Computer Science Subject: Computer Science Topic: Data Types, Variables & Operators 1 Write a program to print HELLO WORLD on screen. 2 Write a program to display output using a single cout statement. 3 Write a program

More information

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return 0. How would we write the BinaryHeap siftdown function recursively? [0] 6 [1] [] 15 10 Name: template class BinaryHeap { private: int maxsize; int numitems; T * heap;... [3] [4] [5] [6] 114 0

More information

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Analysis of Algorithms

Analysis of Algorithms ITP21 - Foundations of IT 1 Analysis of Algorithms Analysis of algorithms Analysis of algorithms is concerned with quantifying the efficiency of algorithms. The analysis may consider a variety of situations:

More information

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements. ITEC 136 Business Programming Concepts Week 12, Part 01 Overview 1 Week 12 Overview Week 11 review Associative Arrays Common Array Operations Inserting shifting elements right Removing shifting elements

More information

Objectives of This Chapter

Objectives of This Chapter Chapter 6 C Arrays Objectives of This Chapter Array data structures to represent the set of values. Defining and initializing arrays. Defining symbolic constant in a program. Using arrays to store, list,

More information

LECTURE 17. Array Searching and Sorting

LECTURE 17. Array Searching and Sorting LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort

More information

Sorting. Bringing Order to the World

Sorting. Bringing Order to the World Lecture 10 Sorting Bringing Order to the World Lecture Outline Iterative sorting algorithms (comparison based) Selection Sort Bubble Sort Insertion Sort Recursive sorting algorithms (comparison based)

More information

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort Sorting Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. A list of cities could be sorted by

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 11 / 2015 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Sorting Searching Michael Eckmann - Skidmore College - CS 106 - Summer 2015

More information

Using Lists (Arrays) Notes

Using Lists (Arrays) Notes Using Lists (Arrays) Notes - 27.11.16 Array/List = a data structure that allows for multiplied items or elements to be stored using just one variable name. Each element in a list is accessed using an index

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. Week 4. Assylbek Jumagaliyev Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays

More information

AN ENHANCED SELECTION SORT ALGORITHM

AN ENHANCED SELECTION SORT ALGORITHM AN ENHANCED SELECTION SORT ALGORITHM Ramesh M. Patelia 1, Shilpan D. Vyas 2, Parina S. Vyas 3, Nayan S. Patel 4 1 Department of M.Sc. (IT) & HOD of BCA, AMCOST, Anand, Gujarat, (India) 2 Department of

More information

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017 COMP 250 Lecture 7 Sorting a List: bubble sort selection sort insertion sort Sept. 22, 20 1 Sorting BEFORE AFTER 2 2 2 Example: sorting exams by last name Sorting Algorithms Bubble sort Selection sort

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Task Description. Selection Sort. Should we worry about speed? Sorting Should we worry about speed? Task Description We have an array of n values in any order We need to have the array sorted in ascending or descending order of values 2 Selection Sort Select the smallest

More information

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7 Sorting Algorithms One of the fundamental problems of computer science is ordering a list of items. There s a plethora of solutions to this problem, known as sorting algorithms. Some sorting algorithms

More information

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",

More information

TOPICS TO COVER:-- Array declaration and use.

TOPICS TO COVER:-- Array declaration and use. ARRAYS in JAVA TOPICS TO COVER:-- Array declaration and use. One-Dimensional Arrays. Passing arrays and array elements as parameters Arrays of objects Searching an array Sorting elements in an array ARRAYS

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10 // CS Introduction to Computation " UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department Professor Andrea Arpaci-Dusseau Fall Lecture : How can computation sort data in order for you? What is sorting?

More information

We will stamp HW Block day:

We will stamp HW Block day: Sorting Videos! We will stamp HW Block day: #10 Recursion worksheet #3 #11 12 Recursion-1 Coding Bats #12 Code Step By Step (see canvas) Today we Dance! No Homework tonight :) Guest Speaker Masters in

More information

Data Structures - Simple Sorting

Data Structures - Simple Sorting Data Structures - Simple Sorting Dr. TGI Fernando 1 2 December 21, 2011 1 Email: gishantha@dscs.sjp.ac.lk 2 URL: http://tgifernando.wordpress.com/ Dr. TGI Fernando () Data Structures - Simple Sorting December

More information

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes ) CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16 In-Class Midterm ( 11:35 AM 12:50 PM : 75 Minutes ) This exam will account for either 15% or 30% of your overall grade depending on your

More information

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

VARIABLE, OPERATOR AND EXPRESSION [SET 1] VARIABLE, OPERATOR AND EXPRESSION Question 1 Write a program to print HELLO WORLD on screen. Write a program to display the following output using a single cout statement. Subject Marks Mathematics 90

More information

CS 202, Fall 2017 Homework #4 Balanced Search Trees and Hashing Due Date: December 18, 2017

CS 202, Fall 2017 Homework #4 Balanced Search Trees and Hashing Due Date: December 18, 2017 CS 202, Fall 2017 Homework #4 Balanced Search Trees and Hashing Due Date: December 18, 2017 Important Notes Please do not start the assignment before reading these notes. Before 23:55, December 18, upload

More information

THE UNIVERSITY OF WESTERN AUSTRALIA

THE UNIVERSITY OF WESTERN AUSTRALIA THE UNIVERSITY OF WESTERN AUSTRALIA MID SEMESTER EXAMINATION April 2018 DEPARTMENT OF COMPUTER SCIENCE & SOFTWARE ENGINEERING DATA STRUCTURES AND ALGORITHMS CITS2200 This Paper Contains: 6 Pages 10 Questions

More information

All answers will be posted on web site, and most will be reviewed in class.

All answers will be posted on web site, and most will be reviewed in class. Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required.

More information

AMPL Integer Linear Programming. ORLAB Operations Research Laboratory. Borzou Rostami. Politecnico di Milano, Italy.

AMPL Integer Linear Programming. ORLAB Operations Research Laboratory. Borzou Rostami. Politecnico di Milano, Italy. AMPL Integer Linear Programming ORLAB Operations Research Laboratory Borzou Rostami Politecnico di Milano, Italy January 18, 2012 Integer Programming: Many linear programming problems require certain variables

More information

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10 EECE.2160: ECE Application Programming Spring 2018 Exam 2 March 30, 2018 Name: Lecture time (circle 1): 8-8:50 (Sec. 201) 12-12:50 (Sec. 202) For this exam, you may use only one 8.5 x 11 double-sided page

More information

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements Chapter Sorting and Searching Algorithms Fall 2017 CISC2200 Yanjun Li 1 Sorting Given a set (container) of n elements Eg array, set of words, etc Suppose there is an order relation that can be set across

More information

Data Structures and Algorithms Sorting

Data Structures and Algorithms Sorting Data Structures and Algorithms Sorting Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/23 12-0: Sorting Sorting is

More information

Procedural Programming

Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.

More information

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi SCJ2013 Data Structure & Algorithms Bubble Sort Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi 1 Bubble Sort Sorting activities for Bubble: Go through multiple passes over the array. In every pass: Compare

More information

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only CS2223: Algorithms D- Term, 2015 Homework I Teams: To be done individually Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only 1 General Instructions Python Code vs. Pseudocode: Each

More information

EXTERNAL SORTING. CS 564- Spring ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

EXTERNAL SORTING. CS 564- Spring ACKs: Dan Suciu, Jignesh Patel, AnHai Doan EXTERNAL SORTING CS 564- Spring 2018 ACKs: Dan Suciu, Jignesh Patel, AnHai Doan WHAT IS THIS LECTURE ABOUT? I/O aware algorithms for sorting External merge a primitive for sorting External merge-sort basic

More information

Priority Queues and Binary Heaps

Priority Queues and Binary Heaps Yufei Tao ITEE University of Queensland In this lecture, we will learn our first tree data structure called the binary heap which serves as an implementation of the priority queue. Priority Queue A priority

More information

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009

Fast Bit Sort. A New In Place Sorting Technique. Nando Favaro February 2009 Fast Bit Sort A New In Place Sorting Technique Nando Favaro February 2009 1. INTRODUCTION 1.1. A New Sorting Algorithm In Computer Science, the role of sorting data into an order list is a fundamental

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms Instructor: SharmaThankachan Lecture 10: Greedy Algorithm Slides modified from Dr. Hon, with permission 1 About this lecture Introduce Greedy Algorithm Look at some problems

More information

Repetition Structures II

Repetition Structures II Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi Types of Control Structures Sequential All programs that we have written so far The statements inside a pair

More information

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

CSCE 2014 Final Exam Spring Version A

CSCE 2014 Final Exam Spring Version A CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers

More information

Recursion: The Beginning

Recursion: The Beginning Yufei Tao ITEE University of Queensland This lecture is the inception of a powerful technique called recursion. If used judiciously, this technique can simplify the design of an algorithm significantly,

More information

Chapter 6 Heapsort 1

Chapter 6 Heapsort 1 Chapter 6 Heapsort 1 Introduce Heap About this lecture Shape Property and Heap Property Heap Operations Heapsort: Use Heap to Sort Fixing heap property for all nodes Use Array to represent Heap Introduce

More information

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website: Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website: https://users.wpi.edu/~sjarvis/ece2049_smj/ece2049_labs.html You do not need to keep

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 9 Date:

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

More information

EENG 212 Lab 2. Recursive Functions

EENG 212 Lab 2. Recursive Functions EENG 212 Lab 2 Outline - Recursive Functions - Arrays Recursive Functions As it was said before modules in C are called functions. One of the types of functions is a recursive function. A recursive function

More information

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin CS 61B Summer 2005 (Porter) Midterm 2 July 21, 2005 - SOLUTIONS Do not open until told to begin This exam is CLOSED BOOK, but you may use 1 letter-sized page of notes that you have created. Problem 0:

More information

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials. Exam 1 practice problems Remember to also pactice: Homework, quizzes, class examples, slides, reading materials. P1 (MC) For all the questions below (except for the True or False questions), the answer

More information

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening. Last Time Announcements The File class. Back to methods Passing parameters by value and by reference. Review class attributes. An exercise to review File I/O, look at passing by reference and the use of

More information

Chapter 16: Greedy Algorithm

Chapter 16: Greedy Algorithm Chapter 16: Greedy Algorithm 1 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm 2 Coin Changing Suppose that in a certain country, the coin dominations consist

More information

Recursion: The Beginning

Recursion: The Beginning Department of Computer Science and Engineering Chinese University of Hong Kong This lecture will introduce a useful technique called recursion. If used judiciously, this technique often leads to elegant

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Final Exam. COMP Summer I June 26, points

Final Exam. COMP Summer I June 26, points Final Exam COMP 14-090 Summer I 2000 June 26, 2000 200 points 1. Closed book and closed notes. No outside material allowed. 2. Write all answers on the test itself. Do not write any answers in a blue book

More information

PRACTICAL LIST FOR ODD SEMESTERS Session (PGDCA/MCA/MSC (CS))

PRACTICAL LIST FOR ODD SEMESTERS Session (PGDCA/MCA/MSC (CS)) GURU JAMBHESHWAR UNIVERSITY OF SCIENCE & TECHNOLOGY, HISAR DIRECTORATE OF DISTANCE EDUCATION PRACTICAL LIST FOR ODD SEMESTERS Session 2015-16 (PGDCA/MCA/MSC (CS)) SEMSTER 1 st Programme: PGDCA/MCA/MSC

More information

SORTING AND SEARCHING

SORTING AND SEARCHING SORTING AND SEARCHING Today Last time we considered a simple approach to sorting a list of objects. This lecture will look at another approach to sorting. We will also consider how one searches through

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 21, 2016 Lecture 8: Sorting Assignment 3: Due Sunday Sept 25 @ 10pm Remember: javac Xlint:all & checkstyle *.java Solutions should be independently written!

More information

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

CALIFORNIA STATE UNIVERSITY, SACRAMENTO College of Business Administration. MIS Information Systems II. Midterm Exam

CALIFORNIA STATE UNIVERSITY, SACRAMENTO College of Business Administration. MIS Information Systems II. Midterm Exam CALIFORNIA STATE UNIVERSITY, SACRAMENTO College of Business Administration MIS 211 - Information Systems II Midterm Exam Points: 100 Date Due: Wednesday, March 20 Answer the following three problems. The

More information

Data Structures and Algorithms in Java. Second Year Software Engineering

Data Structures and Algorithms in Java. Second Year Software Engineering Data Structures and Algorithms in Java Second Year Software Engineering Introduction Computer: is a programmable machine that can store, retrieve and process data. Data: The representation of information

More information

Sorting TEN. Alphabetical sort

Sorting TEN. Alphabetical sort TEN Sorting In this chapter we will look at various ways in which sorting might be used in programs. To with, let's see how a series of words could be arranged into alphabetical order by the computer...

More information

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different? QUIZ 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different? 1 QUIZ 3] What are the 4 fundamental types of algorithms used to manipulate arrays? 4] What control structure

More information

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median

More information

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018 Integrated Introduction to Computer Science Fisler, Nelson Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018 Contents 1 In-Place Sorting 1 2 Swapping Elements in an Array 1 3 Bubble

More information

AMPL Integer Linear Programming And Sensitivity Analysis for LP. ORLAB Operations Research Laboratory. Borzou Rostami. Politecnico di Milano, Italy

AMPL Integer Linear Programming And Sensitivity Analysis for LP. ORLAB Operations Research Laboratory. Borzou Rostami. Politecnico di Milano, Italy AMPL Integer Linear Programming And Sensitivity Analysis for LP ORLAB Operations Research Laboratory Borzou Rostami Politecnico di Milano, Italy December 6, 2012 Integer Programming: Many linear programming

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24 Priority Queue: Motivating Example 3 jobs have been submitted

More information

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer

Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer Armstrong Atlantic State University Engineering Studies MATLAB Marina Sorting Primer Prerequisites The Sorting Primer assumes knowledge of the MATLAB IDE, MATLAB help, arithmetic operations, built in functions,

More information

Chapter 8 Search and Sort

Chapter 8 Search and Sort Chapter 8 Search and Sort Goals This chapter begins by showing two algorithms used with arrays: selection sort and binary search. After studying this chapter, you will be able to understand how binary

More information

GCSE Computer Science Component 02

GCSE Computer Science Component 02 GCSE Computer Science Component 02 Revision Computational Thinking ABSTRACTION Making a problem simpler by removing unnecessary details. This is important when coding as it: Makes it easier to code! Means

More information

Placing an Order. Error! No text of specified style in document. page 1

Placing an Order. Error! No text of specified style in document. page 1 Placing an Order Error! No text of specified style in document. page 1 Overview of the Order Sheet This section will help you navigate around the order sheet and change settings to customise your view

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Answer Sheet Short Answers 1. In an array declaration, this indicates the number of elements that the array will have. b a. subscript b. size declarator

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Chapter 3: Searching and Sorting, Algorithmic Efficiency

Chapter 3: Searching and Sorting, Algorithmic Efficiency 1 Chapter 3: Searching and Sorting, Algorithmic Efficiency Search Algorithms Problem: Would like an efficient algorithm to find names, numbers etc. in lists/records (could involve DATA TYPES). Basic Search:

More information

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1) Topics Data Structures and Information Systems Part 1: Data Structures Michele Zito Lecture 3: Arrays (1) Data structure definition: arrays. Java arrays creation access Primitive types and reference types

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

Arrays and Applications

Arrays and Applications Arrays and Applications 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2014 Instructor: Dr. Asish Mukhopadhyay What s an array Let a 0, a 1,, a n-1 be a sequence

More information

ECE 242 Data Structures and Algorithms. Simple Sorting I. Lecture 4. Prof.

ECE 242 Data Structures and Algorithms.  Simple Sorting I. Lecture 4. Prof. ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Simple Sorting I Lecture 4 Prof. Eric Polizzi Logistics public void needhelpwithproject(string instructor){ switch

More information

Multiple-Subscripted Arrays

Multiple-Subscripted Arrays Arrays in C can have multiple subscripts. A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of information arranged in rows and

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 17 December Takako Nemoto (JAIST) 17 December 1 / 17 A tip for the last homework 1 Do Exercise 9-5 ( 9-5) in p.249 (in the latest edtion). The type of the second

More information

Ch 5-2. Arrays Part 2

Ch 5-2. Arrays Part 2 2014-1 Ch 5-2. Arrays Part 2 March 29, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

Sorting. Chapter 12. Objectives. Upon completion you will be able to: Chapter 12 Sorting Objectives Upon completion you will be able to: Understand the basic concepts of internal sorts Discuss the relative efficiency of different sorts Recognize and discuss selection, insertion

More information

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6 6.0 Introduction Sorting algorithms used in computer science are often classified by: Computational complexity (worst, average and best behavior) of element

More information

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong Department of Computer Science and Engineering Chinese University of Hong Kong In this lecture, we will design the merge sort which sorts n elements in O(n log n) time. The algorithm illustrates a technique

More information