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

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

View Full Document Right Arrow Icon
CS121 Autumn 2009 Lecture #5 exercises Reading: Sedgewick&Wayne 2.1 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");
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern