logo

UTK Notes


Clicker Questions - 15-Set

Question 1: What is the output of p1.cpp when it runs with input-1.txt as standard input?

input-1.txt

Samuel
Amelia
Lucas
Ian
Cole
Brooke
Bailey
Lucas
Charlie
Samuel

p1.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <set>
#include <iostream>
using namespace std;

int main()
{
    set <string> n;
    set <string>::const_iterator nit;
    string s;

    while (cin >> s) n.insert(s);
    for (nit = n.begin(); nit != n.end(); nit++) {
    cout << nit->at(0) ;
    }
    cout << endl;
    return 0;
}
Answer Remember that you don't insert duplicate elements into a set.
UNIX> g++ p1.cpp
UNIX> ./a.out < input-1.txt
ABBCCILS
UNIX> 

Question 2: What is the output of p2.cpp when it runs with input-2.txt as standard input?

input-2.txt

Gabriel
Wyatt
Eva
Lily
Sarah
Benjamin
Nicholas
Michael
Landon
Madelyn

p2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <set>
#include <iostream>
using namespace std;

int main()
{
    set <string> n;
    set <string>::const_iterator nit;
    string s;

    while (cin >> s) n.insert(s);
    for (nit = n.begin(); nit != n.end(); nit++) {
    cout << nit->at(0) ;
    nit++;
    }
    cout << endl;
    return 0;
}
Answer
UNIX> g++ p2.cpp
UNIX> ./a.out < input-2.txt
BGLMS
UNIX>

No Class Stats, PDF Download