WK 2 Problems #2

You might also like

You are on page 1of 2

Week 2 Session 2 Problems

For the following problems, predict the output and verify with your local machine.

1.
int problem1() {
string s = "\n\n\n"; string s = "nnn"

cout << s.length();


return 0;
} output = 3

2.
int problem2() {
string s1 = "I\'m learning strings";
erase - starts with 0 (Up until index 3 but not including index 3
cout << s1.erase(0, 3) << "\n"; s = " learning strings
cout << s1[2]; output = e
return 0;
}

3.
int problem3() {
string s;
getline(cin, s); // input is Hello World![Enter]
s = "Hello World!"
cout << s.substr(4);
return 0; output = "o World!"
}

4.
int problem4() {
int x, y;

cin >> x >> y; // input is 10[Enter]12


cout << x + y;
return 0; output = 22 (because +x and y are int
}
5.
int problem5() {
string s;

cin >> s; // input is PIC 10A[Enter]


cout << s + " Dis2B";
return 0;
} PIC 10A Dis2B

Write programs that do the following.

6. Output
Hello, my name is [name],
and I'm a [student year].
I would rate PIC10A so far a [score] out of 10.
to the console, where [name], [student year], and [score] are user inputted, separated by
spaces. The variable used to hold score should be an integer. For example, given user
input Alex grad 10[Enter], the desired output is
Hello, my name is Alex,
and I'm a grad.
I would rate PIC10A so far a 10 out of 10.
You may assume that name and student year are just one word each.

7. Take in 5 integers from user input, and output the integer closest to the average of the
given integers. You may assume that the inputted integers are between INT_MIN / 5 and
INT_MAX / 5.

8. Define a string s = "the quick brown fox jumped". Without using any other hard-coded
string literals (i.e. only use string manipulations), define a string holding "the quick fox
picked up". Output the string to double-check.

You might also like