You are on page 1of 11

OODP WEEK 13/14

QUESTION 1
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1;
str1.insert(str1.end(),'W');
str1.insert(str1.end(),'O');
str1.insert(str1.end(),'R');
str1.insert(str1.end(),'L');
str1.insert(str1.end(),'D');

for (string::const_iterator it = str1.begin(); it !=


str1.end(); ++it) {
cout << *it;
}
int len = str1.size();
cout<<"\nLength of string:"<<len;
cout << endl;
return (0);
}

QUESTION 2
#include <iostream>
#include <array>
#include <cstdlib>
using namespace std;
int main() {
array<int, 7> a; //declaring array
array<int, 7>::iterator it; //declaring an array as
iterator
int c, i; //declaring integer variables
a.fill(0);
int cnt = 0;
while (1) {
cout<<"1.Size of the array"<<endl;
cout<<"2.Insert Element into the Array"<<endl;
cout<<"3.Front Element of the Array"<<endl;
cout<<"4.Back Element of the Array"<<endl;
cout<<"5.Display elements of the Array"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Size of the Array: "; // print size of the
array.
cout<<a.size()<<endl;
break;
case 2:
cout<<"Enter value to be inserted: "; // enter
values in the array.
cin>>i;
a.at(cnt) = i;
cnt++;
break;
case 3:
cout<<"Front Element of the Array: "; //print
front element of the array.
cout<<a.front()<<endl;
break;
case 4:
cout<<"Back Element of the Arrary: "; //print
back element of the array.
cout<<a.back()<<endl;
break;
case 5:
for (it = a.begin(); it != a.end(); ++it ) //print all
elements of the array.
cout <<" "<< *it;
cout<<endl;
break;
case 6:
exit(1); //exit
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

QUESTION 3
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to print the elements of the list


void show(list<int> &l)
{
//Defining an iterator for the list
list<int>::iterator i;
for (i = l.begin(); i != l.end(); i++)
{
cout << "\t" << *i;
}

cout << endl;


}

int main()
{
cout << "\n\nWelcome :-)\n\n\n";

int i;

//List declaration (list of integers)


list<int> l1, l2;

//Filling the elements


cout << "Filling the First list from end\n\n";
for (i = 0; i < 5; i++)
{
l1.push_back(i * 5); //inserting elements from end
}

cout << "The elements of the First list are: ";


show(l1);

//Filling the elements


cout << "Filling the Second list from front\n\n";
for (i = 0; i < 5; i++)
{
l2.push_front(i * 10); //inserting elements from
front
}

cout << "The elements of the Second list are: ";


show(l2);
cout << "\n\nList 1 after getting reversed is: ";
l1.reverse();
show(l1);

cout << "\n\nList 2 after getting sorted is: ";


l2.sort();
show(l2);

cout << "\n\n\n";

return 0;
}
QUESTION 4
#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

// uniform initialization
unordered_set<int> numbers {1, 100, 10, 70, 100};

// loop across the unordered set


// display the value
cout << "numbers = ";
for(auto &num: numbers) {
cout << num << ", ";
}

return 0;
}

QUESTION 5
#include<iostream>
using namespace std;
int main() {
pair <char,int> value('a',7);
pair <string,double> fruit ("grapes",2.30);
pair <string,double> food ("pulao",200);
cout<<"The value of "<<value.first<<" is
"<<value.second <<endl;
cout<<"The price of "<<fruit.first<<" is Rs.
"<<fruit.second <<endl;
cout<<"The price of "<<food.first<<" is Rs.
"<<food.second <<endl;
return 0;
}

You might also like