l5 - F3(7 System.out.printf"exiting main\n" Please...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS121 Autumn 2009 Lecture #5 exercises Exercise 1: What is the output of the following program: public class Ex1 { public static void printInfo(String name, String s, int v) { // the %s directive indicates that the corresponding // argument should be interpreted as a string System.out.printf("%s %s: %d\n", name, s, v); } public static int F1(int i, int j) { printInfo(" enter F1", "i", i); printInfo(" enter F1", "j", j); return i+j; } public static int F2(int i, int j) { printInfo(" enter F2", "i", i); printInfo(" enter F2", "j", j); i++; j++; int k = F1(i, j); printInfo(" in F2", "k", k); printInfo(" leave F2", "i", i); printInfo(" leave F2", "j", j); return k; } public static int F3(int i) { printInfo(" enter F3", "i", i); int l = F2(i+1, i+2); printInfo(" in F3", "i", i); printInfo(" in F3", "l", l); int m = F2(i+10, i+12); printInfo(" leaving F3", "i", i); printInfo(" leaving F3", "l", l); return l+m; } public static void main(String[] args) { System.out.printf("main calling to F3(7)\n");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: F3(7); System.out.printf("exiting main\n"); } } Please turn over Exercise 2: Write a function countOccurrences that takes an integer array and an integer value and returns a count of the number of times the value occurs in the array. Exercise 3: Write a function incrValues that takes an integer array and increments the elements in the array in place (that is, do not allocate a new array). Exercise 4 (challenging): The file /home/amr/cs121/L5/Histogram.java contains the code we discussed in class today. The current implementation of genHistogram generates a histogram for the range [0. .high]. Modify it to generate a histogram for the range [lo. .high] where lo is an additional param-eter to genHistogram . Element histogram[0] should hold the count of the number of elements in the array that equal lo (rather than as is now the case), histogram[1] should hold a count of the number of elements equal to lo+1 , etc....
View Full Document

This note was uploaded on 10/10/2009 for the course CMSC 12100 taught by Professor Annerogers during the Fall '09 term at UChicago.

Page1 / 2

l5 - F3(7 System.out.printf"exiting main\n" Please...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online