CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.

Slides:



Advertisements
Similar presentations
5.1 Real Vector Spaces.
Advertisements

Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
A simple example finding the maximum of a set S of n numbers.
Kernighan/Ritchie: Kelley/Pohl:
CSE115/ENGR160 Discrete Mathematics 02/28/12
Chapter 2 Matrices Finite Mathematics & Its Applications, 11/e by Goldstein/Schneider/Siegel Copyright © 2014 Pearson Education, Inc.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
True or false A variable of type char can hold the value 301. ( F )
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
Goldstein/Schnieder/Lay: Finite Math & Its Applications, 9e 1 of 86 Chapter 2 Matrices.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Data Structures Review Session 1
Complexity (Running Time)
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Matrix Mathematics in MATLAB and Excel
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Chapter 10 Review: Matrix Algebra
MATH 224 – Discrete Mathematics
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 3: The Fundamentals: Algorithms, the Integers, and Matrices
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 8 Arrays and Strings
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Data Structure Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
ALGORITHMS.
1 CS161 Introduction to Computer Science Topic #8.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
Topic 6 Recursion.
Computer Programming BCT 1113
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Chapter 5 Arrays Introducing Arrays
Data Structures Review Session
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Presentation transcript:

CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT

Review for mid-term Basics –writing simple expressions –creating arrays, accessing specific cells of an array, inverse of a matrix, solving a set of linear equations using arrays and matrices, matrix multiplication, and dot products –open a file, write to a file, close a file, fprintf command, input and display commands –plot command –function definition (correct syntax, arguments, and return values) –if statements, for loops, and while loops –polynomials

Review for mid-term Know the sorting algorithm we covered in class and also how to program it. Know how to search for a number in an array or matrix. Know the solutions to ALL the quizzes. That itself will get you a good number of points. Most problems will be similar to those on quizzes plus a couple of challenge ones.

Some sample problems Write a function that asks the user for student names and scores until a -1 is entered (an infinite while loop that waits until -1 is entered). Write a function that takes as input student names and grades, sorts them, and outputs the names of the top three students

Some sample problems Write a function that outputs 1 if two strings are equal and 0 otherwise. Strings are arrays, so just loop through each array testing for equality. Write a function that searches for a student name in an array (just linear time search). Problems 13, 14, 17, 18, 19, and 20 from Chapter 7.

Polynomials Polynomials have the form Examples: –f(x) = x (polynomial of degree 2) –f(x) = 2x 3 + 4x 2 (polynomial of degree 3)

Polynomials Polynomials are represented using arrays –x 2 + 3p = [ 1 0 3] –2x 3 + 4x 2 q = [ ] –2x + 2 r = [ 2 2 ] Polyval command is used to evaluate polynomials –polyval(p, 1) = = 4 –polyval(p, 2) = = 7 –polyval(r, 1) = = 4 –polyval(r, 2) = = 6

Finding roots of a polynomial Roots of a polynomial p(x) are values of x for which p(x)=0. For example, the root of the polynomial p(x) = x 2 – 1 is x=1 We can find roots of a polynomial using the root(p) command. For example –polynomial p(x)=4x x – 8 –in MATLAB p = [ ] –roots(p) = and Polynomials can also be added and multiplied using the conv command.

Searching an array Input: A (sorted array), x (integer) Output: 1 if x is in A and 0 otherwise Algorithm: –Traverse the array and compare x with each element of A MATLAB –function z = search(A, x) –for i = 1:1:length(A) if(x == A(i)) –z = 1; end –end Is this correct?

Searching an array NO! If x is not in A then what is return value of z? z is not defined initially –function z = search(A, x) –for i = 1:1:length(A) if(x == A(i)) –z = 1; end –end

Searching an array Correct MATLAB solution –function z = search(A) –z=0; –for i = 1:1:length(A) if(x == A(i)) –z = 1; end –end

Infinite loops Write a program that takes in student names and scores until -1 is entered name = input(‘enter student name’); score = input(‘enter student score’); while score == -1 – –name = input(‘enter student name’); –score = input(‘enter student score’); end Is this correct?

Infinite loops NO! --- loop condition is not right name = input(‘enter student name’); score = input(‘enter student score’); while score == -1 – –name = input(‘enter student name’); –score = input(‘enter student score’); end

Infinite loops Correct solution is name = input(‘enter student name’); score = input(‘enter student score’); while score ~= -1 – –name = input(‘enter student name’); –score = input(‘enter student score’); end

Testing for string equality Given two strings x and y, return 1 if equal and 0 otherwise For example if x = ‘abc’ and y = ‘xyz’ return 0. If x = ‘xyz’ and y = ‘xyz’ then return 1. Algorithm: –First check for equal lengths. If lengths are not equal then return 0 because they cannot be the same string. –If length is equal then we have to check each character by looping

String equality function z = strcmp(x, y) if(length(x) == length(y)) z=1; else for i = 1:2:length(x) if x(i) == y(i) z = 1; else z = 0; end What is wrong with this solution? (there are several mistakes)

String equality function z = strcmp(x, y) if(length(x) == length(y)) z=1;  if lengths are equal it doesn’t mean the strings are equal else for i = 1:2:length(x) if x(i) == y(i) z = 1; else z = 0; end

String equality function z = strcmp(x, y) if(length(x) == length(y)) z=1;  if lengths are equal it doesn’t mean the strings are equal else for i = 1:2:length(x)  increment should by 1 to look at every char. if x(i) == y(i) z = 1; else z = 0; end

String equality function z = strcmp(x, y) if(length(x) == length(y)) z=1;  if lengths are equal it doesn’t mean the strings are equal else for i = 1:2:length(x)  increment should by 1 to look at every char. if x(i) == y(i) z = 1;  if one of the characters are equal it doesn’t mean all are equal else z = 0; end

String equality function z = strcmp(x, y) if(length(x) == length(y)) for i = 1:2:length(x)  increment should by 1 to look at every char. if x(i) == y(i) z = 1;  if one of the characters are equal it doesn’t mean all are equal else z = 0; end else z = 0;  if lengths are unequal then return 0 end Let’s fix the solution

String equality function z = strcmp(x, y) if(length(x) == length(y)) for i = 1:1:length(x)  increment by 1 if x(i) == y(i) z = 1;  if one of the characters are equal it doesn’t mean all are equal else z = 0; end else z = 0;  if lengths are unequal then return 0 end Let’s fix the solution

String equality function z = strcmp(x, y) if(length(x) == length(y)) for i = 1:1:length(x)  increment by 1 if x(i) ~= y(i)  if any character is unequal then return 0 z = 0; else z = 0; end else z = 0;  if lengths are unequal then return 0 end Let’s fix the solution

String equality function z = strcmp(x, y) if(length(x) == length(y)) for i = 1:1:length(x)  increment by 1 if x(i) ~= y(i)  if any character is unequal then return 0 z = 0; else z = 0;  we need to fix this end else z = 0;  if lengths are unequal then return 0 end Let’s fix the solution

String equality function z = strcmp(x, y) if(length(x) == length(y)) z = 1;  initialize z to 1 by assuming the strings are equal to begin with for i = 1:1:length(x)  increment by 1 if x(i) ~= y(i)  if any character is unequal then return 0 z = 0; end else z = 0;  if lengths are unequal then return 0 end

Sorting an array of numbers Input: [ ] Output: [ ] Algorithm: –Move through array and compare each pair of numbers –Find the smallest and move to the extreme left. Then ignore the first element and find smallest in the rest of the array and move to the second. Then ignore second and find smallest in rest of array and …

Sorting an array of numbers Input: [ ] Output: [ ] Algorithm: –[ ] Is 7 smaller than 4? No. Is 50 smaller than 4? No. … Is 1 smaller than 4. Yes. Switch. Continue. –[ ] Now ignore the first number because we know it is the smallest. Start from second number 7. Is 50 smaller than 7? No… Is 4 smaller than 7? Yes. Switch and continue –[ ] Now ignore the second number because we know it is te second smallest. Start from the third number 50. Is 63 smaller than 50? No… Is 7 smaller than 50? Yes. Switch. Continue…

Sorting an array of numbers Is the previous algorithm correct? Yes, because we make sure that the smallest is in the correct order after each pass of the array How long does this take? For the input [ ] we traversed 6 numbers for the first position, 5 numbers for the second position, 4 numbers for the third,… So total number of comparisons made were = 21

Sorting an array of numbers If the array is of arbitrary size of say n numbers, then how many comparisons have to be made for sorting the array? Based upon previous analysis it would be (n-1) + (n-2) + (n-3) + … = ?? Let’s not worry about what the above quantity sums to (it’s actually n(n-1)/2) but it looks like it is at most n^2. So we say that the running time of this algorithm is n^2