1 / 12

ITI 1120 Lab #5 Loops Introduction to arrays

ITI 1120 Lab #5 Loops Introduction to arrays. Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot. For today’s lab:. While statement For loop Do/While Loop Arrays Exercises. Sum of Squares. GIVENS: N (a number  1) RESULTS: Sum (sum of squares from 1 2 to N 2 )

lucas
Download Presentation

ITI 1120 Lab #5 Loops Introduction to arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITI 1120Lab #5Loops Introduction to arrays Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

  2. For today’s lab: • While statement • For loop • Do/While Loop • Arrays • Exercises

  3. Sum of Squares GIVENS: N (a number  1) RESULTS: Sum (sum of squares from 12 to N2) INTERMEDIATES: Value (current value to square) HEADER: Sum  SumOfSquares(N) BODY: Sum  0 Value  1 Value  N ? true false Sum  Sum + Value * Value Value = Value + 1

  4. Sum of Odd Integers GIVENS: N (a number  1) RESULTS: Sum (sum of odd integers up to N) INTERMEDIATES: Value (current odd integer) HEADER: Sum  SumOddIntegers(N) BODY: Sum  0 Value  1 Value  N ? true false Sum  Sum + Value Value = Value + 2

  5. Programming Model for Tracing SumOddIntegers(5) GIVENS: N (a number  1) RESULTS: Sum (sum of odd integers up to N) INTERMEDIATES: Value (current odd integer) HEADER: Sum  SumOddIntegers(N) BODY: N Sum Value 5 0 1 4 9 1 3 5 7

  6. Trace for Sum of Odd Integers

  7. Exercise 1 • The code below is supposed to print the integers from 10 to 1 backwards. • You need to find 2 logical errors in the code. • Take the time to draw an algorithm body to follow the logic and find the errors BEFORE any coding. • Correct the code and insert it into a main method (create the Lab4Ex1 class in the file Lab4Ex1.java) to check out your answer.  count = 10; while (count >= 0) { System.out.println(count); count = count + 1; }

  8. Exercise 2 • Develop an algorithm that asks the user to enter two integers. The numbers should be added and the sum displayed. Then the user is asked if she/he wishes to perform the operation again. If so, the operation is repeated, otherwise it should terminate. • Translate the algorithm to a Java method.

  9. Exercise 3 • Develop software to play a guessing game with the user. The program should randomly generate an integer between 1 and 10, and then asks the user to try to guess the number. If the user guesses incorrectly, the program indicates if the guess is high or low and ask them to try again until the guess is correct; when the guess is correct, the program should print a congratulatory message and the number of guesses required to find the number. Design the software as follows: • Develop your algorithms with Visio and Word. • Translate the algorithms to Java. Start with Template.java. • The main() algorithm/method generates a random number and calls the guessing() method. It prints the congratulation message to the user and the number of guesses used to get the number. • The guessing() method receives the random number plays the game with the user. Finally, when the user gets the number, number of guesses used to find the number is returned.

  10. Exercise 4-a • The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers. • Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. Computing the factorial is a lot like computing the sum of integers from 1 to N, but uses multiplication instead of addition. Don't forget about dealing with 0. • Develop your algorithms with Visio and Word. • Translate the algorithms to Java. Start with Lab4Ex4a.java and create a new class Lab4Ex4a in the file Lab4Ex4a.java • The main algorithm/method prompts the user to enter a positive integer, calls calcFact() to get its factorial, and prints the results the user. • The calcFact algorithm/method computes the factorial.

  11. Exercise 4-b • Now modify the main algorithm/method as follows: • Check to see if the user entered a negative number. • If a negative number is entered, print a message saying that a nonnegative number is required and ask the user to enter another number. • Keep prompting the user enters a nonnegative number, after which the factorial can be computed (by calling the problem solving algorithm/method) and printed. • Develop the new algorithm using Word and Viso. • Translate to Java. Start with Lab4Ex4a.java and create a new class Lab4Ex4b in the file Lab4Ex4b.java.

  12. Exercise 5 • Design a program that asks the user for two positive integers no greater than 15 (length and width of a rectangle). The program should then display a rectangle of ‘*’ on the screen with the corresponding dimensions. For example, if the user inputs width=3 and length=5, then the program should print: *** *** *** *** Design the software using the two methods model.

More Related