Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
0 of .
Results for:
No results containing your search query
P. 1
h13 section handout 1

# h13 section handout 1

Ratings:

4.5

(2)
|Views: 11|Likes:
Published by minhhai2209

### More info:

Published by: minhhai2209 on Feb 18, 2009
Copyright:Attribution Non-commercial

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

02/01/2013

pdf

text

original

CS106B
Handout #13
Yves Lu
July 1, 2008
Section Handout #1

Sections will meet once a week to give you a more intimate environment to discuss course material, work through problems, and raise any questions you have. Each week we will hand out a set of section exercises, which are the problems proposed for section that week. While we will not be collecting or grading these problems, you will receive greater benefit from section

if you\u2019ve looked over the problems in advance and tried to sketch solutions. Your section leader

won't necessarily cover every exercise in depth, so be sure to speak up if there are particular problems you'd like to focus on. Solutions to all problems will be given in section so you can work through any remaining exercises on your own. Many of our section exercises have been taken from old exams, so they are also an excellent source of study questions when exam time rolls around.

Problem 1: Removing all occurrences of a character
Write a functionRe mo ve Al lO cc ur re nc es that takes a string as input and returns the string with
a given character removed.

"Stanford University" with the 'n' removed becomes "Staford Uiversity" "Llamas like to laugh" with the 'l' removed becomes "Lamas ike to augh" and so on . . .

Note that the function is case sensitive. This function could be written two ways. One way is to return a completely new string, and the other is to modify the original string. For practice write both of these functions. First write a function that returns a completely new string with the following prototype:

string RemoveAllOccurrences1(string text, char ch);
and then write a function that modifies the original string with the following prototype:
void RemoveAllOccurrences2(string & text, char ch);
Problem 2: Reversing Sentences
Write a functionReverseSentences that takes a string and reverse sentences in that string. You
can assume that the word boundary is indicated a whitespace. For example
ReverseSentences(\u201cYves and his daring crew\u201d)-> \u201ccrew daring his and Yves\u201d
Your function should have the following prototype :
string ReverseSentences(string input)
Hint: Think of how CS106 library can make your life easier.
2
Problem 3: Balls and urns
You are doing a problem set for a probability/statistics class but you cannot figure out the
answer. With the nifty skill you gained from CS106B, you want to write a program that gives
you a good approximation of the answer (which might give you some partial credit). Here\u2019s the
problem:

In an urn, there are three green balls (G), five red balls (R), and two blue balls (B). Moreover, each ball has a score associated with it. The value runs from 1 to n, where n is the total number of balls of that particular color. So this urn has G1, G2, G3, R1, R2, R3, R4, R5, B1, and B2 (where the letter denotes color and the number denotes score). If you draw a ball without replacement, what is the average number of balls that you need to draw in order to get all three colors and the total score is greater or equal to 12?

Write the function that simulates this experiment for 1,000 trials and finds the average.
double SolveBallProblem()
Problem 4: Files and Structs

Given a list of grades, let's say that we want to keep track of some statistics like the min, max and average scores. First, design a struct that will keep track of all of this information. Then, write a function that takes a filename, reads the scores from it (one per line where 0 <= score <= 100), and returns a struct of the type that you created containing these statistics. For efficiency\u2019s sake, the function should make only a single pass over the file.

Tip: int StringToInteger(String s) from \u201cstrutils.h\u201d might come in handy.
Problem 5: Function Trace
Trace through the execution of the following program and show the output it produces.
Also, draw the state of memory at the point indicated.

/*
* File: nonsense.cpp
* This program exists only to test your understanding of pointers.
*/
#include <iostream>
#include "genlib.h"
int Mystery(int *y, int x);
void SlapStick(int *x, int y, int *z);

int main()

{
int x, y, z;
x = 5;
y = 6;
z = Mystery(&x, y);
cout << "Main: x = " << x << ", y = " << y << ", z = " << z << endl;
SlapStick(&z, y, &y);
cout << "Main: x = " << x << ", y = " << y << ", z = " << z << endl;
return 0;

}
3
int Mystery(int *y, int x)

{
*y = x;
x += *y;
cout << "Mystery: x = " << x << ", *y = " << *y << endl;

\u2190Draw a diagram indicating the contents of memory at this point
return (x);
}void SlapStick(int *x, int y, int *z)

{
int *p1;
y = Mystery(z, *x);
p1 = z;
z = x;
x = p1;
*p1 = y + *z/2;
cout << "SlapStick: *x = " << *x << ", y = " << y;
cout << ", *z = " << *z << ", *p1 = " << *p1 << endl;

}Problem 6: Memory Diagram
Trace through the following bit of code and draw a diagram showing the contents of memory at
the indicated point. Be sure to differentiate between the stack and the heap.

struct heroT {
string name;
string weakness;
int powerLevel;

};

struct villianT {
string name;
string evilPlan;
int attackLevel;

};
void Battle(heroT aang, villianT & zuko)

{
int pos = 1;
int level = aang.powerLevel;
string name = zuko.name;

while(level > 20)

{
zuko.evilPlan[pos--] -= (level / 10);
level /= 2;

}zuko.attackLevel -= level;
pos = name.find(aang.weakness);

## Activity (1)

You've already reviewed this. Edit your review.
1 hundred reads
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->