DOC PREVIEW
CSUN COMP 106 - Homework Solutions

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoEngineering Building Room 2303 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062May 9 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 3Homework Solutions – May 9, 2006Page 434, Checkpoint 7.17 – What is the output of the following program? (You may need to consult the ASCII table in Appendix A.).#include <iostream>using namespace std;// function prototypevoid fillArray( char[], int);void showArray( char[], int);int main(){ char prodCode[8] = { ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’ }; fillArray( prodCode, 8 ); showArray( prodCode, 8 ); return 0;}// Definition of function fillArray// (Hint: 65 is the ASCII code for ‘A’.)void fillArray( char arr[], int size){ char code = 65; for ( int k = 0; k < size; k++ ) arr[k] = code;}// Definition of function fillArrayvoid showArray( char codes[], int size){ for ( int k = 0; k < size; k++ ) cout << codes[k];}In the main function, all elements of the array, prodCode are initialized to the null character, ‘0’. Next the function fillArray is called. This function fills all elements of the array with the ASCII codefor the upper case letter, A. (When we set a char type variable to a numerical value it is assigned the character associated with the ASCII code for that number.) The showArray function then displays the output of the array, character-by-character, with no spacing. Since all the characters in the array are ‘A’, the output of the program is simply: AAAAAAAA.Note that the fillArray function is able to change the elements of the array prodCode in the calling program because whole arrays are always passed by reference.Page 434, Checkpoint 7.18 – The following program skeleton, when completed, will ask the user to enter ten integers, which are stored in an array. The function avgArray, which you must write, is to calculate and return the average of the numbers entered.#include <iostream>using namespace std;// Write your function prototype hereMay 9 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 4int main(){ int userNums[10]; cout << “Enter 10 numbers: “; for ( int count = 0; count < 10; count++ ) { cout << “#” << ( count + 1 ) << “: “; cin userNums[count]; } cout << “The average of those numbers is “; cout << avgArray( userNums, 10 ) << endl; return 0;}// // Write the function avgArray here//In this problem we have to write the function and the function prototype. We see that the main program calls the function with an array of type int and the size of the array as an integer constantas the two arguments. Thus the two function parameters must be an array of type int and a scalar variable of type int, representing the size of the array.There is no indication of what the type of the value returned by the function should have. However, we know that averages of integer variables can have a decimal component. Thus we will select to return the value as type double. (Alternatively we could use type float.) With these decisions, our function avgArray could be written as follows.double avgArray( int array[], int size ){ double sum = 0; for ( int count = 0; count < size; count++ ) sum += array[count]; return sum / size;}In the final division, the return value is calculated as a type double because sum is type double and the combination of the double type sum and the int type size promotes size to a type double. Thus, the result of the division, which is returned to the calling program, is type double.The function prototype is simply the function header with a semicolon added.double avgArray( int array[], int size );Alternatively we could write the function prototype without variable names, but we have to includethe brackets, [], to show that the first parameter is an array.double avgArray( int [], int


View Full Document

CSUN COMP 106 - Homework Solutions

Download Homework Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?