1
h07
CS16 F17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone")

h07: Arrays

ready? assigned due points
true Thu 11/09 02:00PM Thu 11/16 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

1.(15 pts) What is the output of the following code? If there is an error that will not allow an output, point it out.

int arr[5];
for (int i = 0; i <= 10; i++) 
{
	if (i < 3) 
        arr[i] = 'a';
	else 
        arr[i] = 'z';
	cout << arr[i] << endl;  
}

2.(15 pts) What is the output of the following code? If there is an error that will not allow an output, point it out.

int arr[7] = {5};
for (int i = 0; i < 7; i++)
	cout << arr[i] + i << endl; 

3.(15 pts) What is the output of the following code? If there is an error that will not allow an output, point it out.

int codes[] = {44, 66, 83, 973, -977};
for (int count : codes) 
{
	if ( (count/2) < 50 )
		cout << count << endl;
	else 
        cout << "invalid" << endl; 
}

4.(25 pts) Using the same formula from Lab 6, write a program that finds the standard deviation of the contents of this array of doubles:
nums[7] = {1.0, 3.2, 4.0, 4.0, 5.5, 9.0, 9.1}. In other words, instead of reading the numbers from a file, read the numbers from the array.

Give your answer in fixed point notation with 4 places after the decimal point showing. Print out your program and staple it to this homework.

5.(30 pts) Write a C++ program that creates a 2-dimensional array of 10x5 elements that are populated by random numbers between 0 and 49 (hint: use the rand() function and re-use code from the multi-dimensional array demonstration in class). The program should then ask the user for a number between 0 and 49 and searches the array for that number. If it finds it, it returns both indicies of the 2-D array, otherwise, it returns a message that says “I did not find this number.” Re-run your program multiple times to make sure that it works (it should show you different answers each time). Print out your program and staple it to this homework.