New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

CS 31 Smallberg Week 5 Notes (10/26 - 10/30)

by: Robin Zhang

CS 31 Smallberg Week 5 Notes (10/26 - 10/30) CS 31

Marketplace > University of California - Los Angeles > ComputerScienence > CS 31 > CS 31 Smallberg Week 5 Notes 10 26 10 30
Robin Zhang

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Includes functions part 2 as well as arrays. Details of the notes: -passing by value vs reference -how to declare/initialize arrays -things to look out for in arrays -an example of using an a...
Introduction to Computer Science I
Class Notes
25 ?




Popular in Introduction to Computer Science I

Popular in ComputerScienence

This 6 page Class Notes was uploaded by Robin Zhang on Saturday October 31, 2015. The Class Notes belongs to CS 31 at University of California - Los Angeles taught by Smallberg in Fall 2015. Since its upload, it has received 131 views. For similar materials see Introduction to Computer Science I in ComputerScienence at University of California - Los Angeles.


Reviews for CS 31 Smallberg Week 5 Notes (10/26 - 10/30)


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/31/15
CS 31 (Smallberg) ­ Week of October 26  Functions    Formal and Reference/Actual Parameters    Consider the following code:    #ncud <istea>  uin nmesac sd;    viddoomehig(nta,in b)    it ai()   ntx  4  nty  5  oSmehig (, );  ou < x<<  “<<y < nd;  }    viddoomehig in a it b {  ++   + 5  }    What does this code print out as a result of the cout statement?    4 5    Why doesn’t the code print out “5 10”?    This is because of how the parameters are called through the function. In this case, a and b are copies of  the ints x and y, respectively, that were called in the main() method. That is to say a and b are new ints  that hold the same values as x and y. Any edits made to a and b will NOT change x or y.    Think of this as making copies of a document. If you edit one of the copies without editing another, one  will be changed, and one will remain as it was.    The way to change the values of x and y is to call them in by value. This is called “calling in by value.”  The way to do this to add the ampersand character after the variable type:      #ncude<isteam  uin naesac st;    viddoSmehig(it&a,in& b;    it ain)   ntx =4;  nty =5;  oSmetin (, y;  ou <<x <  “ <  < edl;  }    viddoSmehig (nt a it& )   ++   + 5;  }    In this case, the program will print out “5 10”    In the first case where the program printed out “4 5”, the parameters a and b are called formal parameters.    In the second case where the program printed out “5 10”, the parameters a and b are called actual or  reference parameters.      Note:    In cases where you only need to change one value, it may be better to return that value instead of calling  by reference. Typically, calling by reference is only needed when you’re attempting to change two or  more variables using the same function.                          Arrays    An array is a more compact way of storing large amounts of data. For example, consider the code we  would write to find whether a specific date is valid:    bolisaliDae(nty,in m,in d {  f m  1||  >12||d  1  reur flse  f m =  ||m =  …  reur d<= 1;  f m =  ||m = …)  reur d<= 0;  f..  }    We would have to check against every single month. Although in this case there are only 12 cases,  imagine if we had 20 cases. Or 30. Or 40. It would get hard to write all the conditions to check them.    The easier way is to create an array to hold each value. An array is declared in the following way:    tpenae nube o eemnt]  {istofvaue};    OR    tpenae nube o eemnt];    Each array can hold only one specific type of variable. That type goes at the beginning.    The name of the array appears after the type.    The number of elements is after the name in square brackets and must be an integer. This value can be a  specific number, such as 5, or it can be a variable that already contains a value. If the variable does not  contain a value already, there will be an error.    Finally, you can choose whether to fill your array with values at the declaration or later. If you choose to  do it when the array is declared, you must use curly brackets with the values separated by commas inside.            So a way to write code for checking if the number of days in a month is valid is:    #ncud <osrem>  uin nmepae td    boldaeIVaid(it ea, it onh,in dy)    it ai(){  ..  }    boldaeIVaid(it ea, it onh,in dy){  ntdasIMoth[1]  {1,9,1,0,1,3,3,3,3,3,3,3};  ..  }    There are three important things to remember when considering using an array.  1. Making the array look compact and more readable is always better than not doing so. In the above  case, it’s hard to tell whether there are 12 items in that array. By dividing that line into 2 or 3  separate lines, it’s much easier to tell how many items are in the array:    ntdasIMoth[1]  {1,9,1,0,    3,3,3,3,    3,3,3,3};    2. The way to locate an item in an array is the same as that for a string: the array starts at item 0 and  goes up to item n­1, where n is the total number of elements in that array. So if I wanted to call  the value for February (the 29), I would have to call    ayInonhs1] instayInonh 2] (which would call the 31 for March)    3. There is .enth .iz() to find the size of the array. No function is  included in the C++ library. This is different from Java.    In the case of number 3, to make an array the correct size for the user, you can do two things:  1. You can ask the user for the number of elements beforehand, and use a for loop to catch all the  elements. This does have one problem: if the user miscounts the number of elements needed,  either the array will be too large or too small.  2. You can run an infinite loop that catches all the elements entered and break out of the loop when  a certain condition is met (e.g a negative value is entered). Just make sure that the certain  condition doesn’t hinder the operation of the loop.      ..  cns ntMA_NMBROFSCRE  100  it cre[MX_UME_O_SORS;  it Sors  0  it oal= ;  dubesuOfqure  0  cut< “ntr hesors neaiv wendoe:” < ed;  fr ;)   in s  ci > s  if(s< )  rek;  if(s> 0)   ou < “ soe us b n herag 0thouh 0. coe  gnre.”<<edl  onine;  }  toal+=s  screnSoes =s;  nSors+;  }  i (Sors = )  cot < Ter wren sors,son aerge”<<enl;  ese{  dobl ma =stti_as<dube>tta)/Scrs;  cot < Te ea o llth sorsis“ < en < nd;  fo (ntk= ;  <ncoes k+){  ouledif  coesk]­men;  umfSuaes+ dff* if;  }  cot < Te tadaddeiaio i  < srtsmOSqars   nSors)< edl  }    In the above case, if we were to find just the mean, we could wouldn’t need an array. We could just use a  running total as well as a running count of the number of inputs.     However, since we are also finding the standard deviation, we have to use an array to keep track of the  differences. This code uses the second method of cutting out of the loop by asking the user for a negative  number (since the test score will always be between 0 and 100).    Also, notictaiccaedobl>(aiale. We needed to include that line  because dividing an int by an int will not output a double value, even if there were decimals after the  division. Therefore, we need to change at least one of the variables being divided into a double to ensure  the result is a double. This is called casting. The variable that is being casted is between the parentheses. 


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.