CS 2073-001,   Exam 2, Spring 2005

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.
Points for each problem: 1-15, 2-15, 3-35, 4-35, 5-up to 10.


  1. Simple Array: Consider the partial program below. Add printf statements so that the program will produce exactly the output shown. (The numbers must be from the array, and not just in just inside the string of the printf, so that different numbers in the array will produce a different output. Do not try to use a loop here.)


  2. Simple Array: Fill in the rest of the C program below, so that it will use a loop to add the square of each number in the array and then print the sum. (The program should be complete with all necessary declarations. You must use a loop.)

  3. Reading numbers into an array: The following program from class reads integers as "exam scores" and puts them into an array. Answer the questions below, some of which involve adding to the code

    1. Suppose you want to enter three scores: 80, 100, and 90. What do you have to type after these three numbers so that the program will work?

       

    2. When the program is done reading, draw a diagram to show the values in the array s and give the value of the variable n .

       

       

    3. Write a loop at the end of the code above to print the exam scores, all on one line with a blank between each one. At the end print a newline. You must use a loop. You will also have to declare a loop variable.

    4. Write a second loop after the one above that will add up the scores that are in the array s, using an integer variable sum. Write code to calculate the average of the scores, using a double variable average. Finally print the value of the average score. You will have to declare sum and average. (It would be possible to add up the scores as they are read, but are supposed to work with the scores that are in the array.)

  4. 2-dimensional arrays: Consider the following program that declares a 2-dimensional array named x .

    1. How many rows does the array x have? What is the length of each row?

    2. What index number does C give to each row of the array x ? What index number does C give to each column of the array x ?

    3. Put in code above that will give the element in row number 1 and in column number 3 the value 57 .

    4. Give code above that will use a double for loop to print the values of the array x , each row on a separate line, with the elements of each row separated by one blank.

    5. Write a function rowsum with a two formal parameters: a row number i and the whole array x itself. This function will add up the numbers in row number i (ignoring the other rows), and will return the sum of these numbers as an int . (The code above shows a call to this function inside the main function that will find the sum of the numbers in row 2 and will print this sum, which is 148 .)


  5. Trivia question: Which European country has the highest population density? [Hint: think weird, think small.] Points for this problem: if top_pd is a double giving the value of the highest population density, and your_pd is a double giving the value of the population density of the country that you name, then you will get the following number of points: (int)(10.0*your_pd/top_pd) .