cis 101: computer programming and problem solving lecture 7 usman roshan department of computer...

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

Post on 19-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

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

CIS 101: Computer Programming and Problem Solving

Lecture 7Usman Roshan

Department of Computer Science

NJIT

Page 2: 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

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

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.

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

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

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

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.

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

Polynomials

• Polynomials have the form

• Examples:– f(x) = x2 + 3 (polynomial of degree 2)– f(x) = 2x3 + 4x2 (polynomial of degree 3)

f x a x a x a x ann

nn( ) . . .

1

11 0

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

Polynomials

• Polynomials are represented using arrays– x2 + 3 p = [ 1 0 3]– 2x3 + 4x2 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

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

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) = x2 – 1 is x=1

• We can find roots of a polynomial using the root(p) command.

• For example– polynomial p(x)=4x2 + 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.

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

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?

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

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

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

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

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

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

– <store name and score in file>– name = input(‘enter student name’);– score = input(‘enter student score’);

• end• Is this correct?

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

Infinite loops

• NO! --- loop condition is not right

• name = input(‘enter student name’);

• score = input(‘enter student score’);

• while score == -1– <store name and score in file>– name = input(‘enter student name’);– score = input(‘enter student score’);

• end

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

Infinite loops

• Correct solution is

• name = input(‘enter student name’);

• score = input(‘enter student score’);

• while score ~= -1– <store name and score in file>– name = input(‘enter student name’);– score = input(‘enter student score’);

• end

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

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

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

String equalityfunction 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

endendWhat is wrong with this solution? (there are several mistakes)

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

z=1; if lengths are equal it doesn’t mean the strings are equalelse

for i = 1:2:length(x) if x(i) == y(i)

z = 1; else

z = 0;end

endend

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

z=1; if lengths are equal it doesn’t mean the strings are equalelse

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

endend

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

z=1; if lengths are equal it doesn’t mean the strings are equalelse

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;

endend

end

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

String equalityfunction 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;

endend

elsez = 0; if lengths are unequal then return 0

endLet’s fix the solution

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

for i = 1:1:length(x) increment by 1if x(i) == y(i)

z = 1; if one of the characters are equal it doesn’t mean all are equal

else z = 0;

endend

elsez = 0; if lengths are unequal then return 0

endLet’s fix the solution

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

for i = 1:1:length(x) increment by 1if x(i) ~= y(i) if any character is unequal then return 0

z = 0; else

z = 0;end

endelse

z = 0; if lengths are unequal then return 0endLet’s fix the solution

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

String equalityfunction z = strcmp(x, y)if(length(x) == length(y))

for i = 1:1:length(x) increment by 1if x(i) ~= y(i) if any character is unequal then return 0

z = 0; else

z = 0; we need to fix thisend

endelse

z = 0; if lengths are unequal then return 0endLet’s fix the solution

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

String equalityfunction 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 1if x(i) ~= y(i) if any character is unequal then return 0

z = 0; end

endelse

z = 0; if lengths are unequal then return 0end