CSCI15a: Chapter 12

CSCI111: problems Chapter 12 (old text but doable)

  1. Chapter 10, exercise 1 had some systems playing games. What are some class features of these games that might be implemented with lists/arrays?

  2. Consider the following, where i and sum are int variables
        i = 1;
        sum = 0;
        while ( i != 10) {
          // sum = sum + 1 ;
          //  mistyped - should have been
           sum = sum + i ;
           i = i + 1;
        }
    

  3. Consider the following, where i and sum are int variables
        i = 1;
        sum = 0;
        while ( i != 10) {
           sum = sum + i ;
           i = i + 2;
        }
    
    • What is the final value of i? of sum?
    • what does the loop compute?

  4. Write a method that computes the sum of the integers from 1 to n, for any positive integer n

  5. Write a method that computes the sum of the odd numbers from 1 to n, for any positive integer n

  6. Given that i is an int variable, indicate the number of times the body of each of the following loops will be performed:
    • i = 1; while (i <= 10) {... i = i + 1;}

    • i = 0; while (i <= 10) {... i = i + 1;}

    • i = 0; while (i <= 10) {... i = i + 1;} (yes, the text has it twice too)

    • i = 0; while (i <= 10) {... i = i + 2;}

    • i = 10; while (i > 0) {... i = i - 1;}

    • i = 10; while (i > 1) {... i = i - 1;}

    • i = 10; while (i > 0) {... i = i - 2;}

    • i = 10; while (i != 0) {... i = i - 1;}

    • i = 9; while (i != 0) {... i = i - 2;}

    • i = 0; while (i != 10) {... i = i + 3;}

  7. See text - considers enrollees array and dropping and adding to the array.
solutions

CSCI111: problems Chapter 22 - arrays

  1. Lab related: Suppose you are teaching a class. In the class, each student needs to take two quizzes. How can one associate a student array with a second array that keeps track of how many quizzes the student has taken?
    Suppose the instructor wanted to randomly choose a student to take a quiz. How could this be done while still ensuring that the student only takes 2 quizzes? (
    answer)

  2. Write a method that takes an array of int as argument, and returns the number of negative values in the array

  3. Write a method that takes an array of int as argument, and reverses the values in the array. For instance, if a is the argument and has 5 elements, with a [0] through [4] containing the values 1, 3, 7, 9, 4, respectively, before the method is executed, then [0] through [4] should contain the values 4, 9, 7, 3, 1 after execution

  4. Write a method that takes an array of int as argument, and shifts the values in the array up by one, moving the last value into the [0]th position. For instance, if a is the argument and has ten elements, the value in a[0] is moved to a[1], the value in a[1] is moved to a[2], etc. The value in a[9] is moved to a[0],

  5. Write a method that takes two array of int arguments with the same length and computes the inner product assuming that the arrays represent vectors. The inner product of vectors v1 and v2 is given by the formula

    where m is the length of the vectors
solutions