Download presentation
Presentation is loading. Please wait.
1
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT
2
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
3
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.
4
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
5
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.
6
Polynomials Polynomials have the form Examples: –f(x) = x 2 + 3 (polynomial of degree 2) –f(x) = 2x 3 + 4x 2 (polynomial of degree 3)
7
Polynomials Polynomials are represented using arrays –x 2 + 3p = [ 1 0 3] –2x 3 + 4x 2 q = [ 2 4 0 0 ] –2x + 2 r = [ 2 2 ] Polyval command is used to evaluate polynomials –polyval(p, 1) = 1 + 3 = 4 –polyval(p, 2) = 4 + 3 = 7 –polyval(r, 1) = 2 + 2 = 4 –polyval(r, 2) = 4 + 2 = 6
8
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 2 + 10x – 8 –in MATLAB p = [ 4 10 -8 ] –roots(p) = -3.1375 and 0.6375 Polynomials can also be added and multiplied using the conv command.
9
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?
10
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
11
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
12
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?
13
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
14
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
15
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
16
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)
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
Sorting an array of numbers Input: [ 4 7 50 63 21 1 4 90 ] Output: [ 1 4 7 21 50 63 90 ] 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 …
26
Sorting an array of numbers Input: [ 4 7 50 63 21 1 90 ] Output: [ 1 4 7 21 50 63 90 ] Algorithm: –[ 4 7 50 63 21 1 90 ] Is 7 smaller than 4? No. Is 50 smaller than 4? No. … Is 1 smaller than 4. Yes. Switch. Continue. –[ 1 7 50 63 21 4 90 ] 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 –[ 1 4 50 63 21 7 90 ] 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…
27
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 [ 4 7 50 63 21 1 90 ] 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 6+5+4+3+2+1 = 21
28
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) + … 2 + 1 = ?? 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.