Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
COBOL 5.47 KB | None | 0 0
  1. #include<vector>
  2. #include<iomanip>
  3. #include"utility.h"
  4.  
  5.  
  6. //1. Create a function displaying matrixes stored as vector of int vectors
  7.     //eg.
  8.         //   |  0   1   2|
  9.         //   |  3   4   5|
  10.         //   | -6  -7   8|
  11.     //display all the elements as there was presentet above (TIP: assume std::setw(3))
  12.     //you can assume square matrix
  13. void disp_matrix(std::vector < std::vector<int>>& vec)
  14. {
  15.     ///solution 1
  16.  
  17.     for (unsigned short int i = 0; i < vec.size(); i++)
  18.     {
  19.         std::cout << " | ";
  20.         for (unsigned short int j = 0; j < vec[i].size(); j++)
  21.         {
  22.             std::cout << std::setw(3) << vec[i][j] << " ";
  23.         }
  24.         std::cout << " | \n";
  25.     }
  26.     ///
  27. }
  28.  
  29. //Problem 2.  Create a function RESIZING the the vector of vectors square matrix n x m to square matrix (n-1)x(m-x)
  30. //Problem 3.  Convert it as a list of size n x m (it is not necessary to create list of list just create one longer) return that list
  31. std::list<int> shrink_square_matrix(std::vector <std::vector<int>>&vec)
  32. {
  33.     ///solution 2
  34.     std::list<int> v_to_list;
  35.     int l = vec.size() - 1;
  36.     vec.resize(l);
  37.     for (unsigned int i = 0; i < l; i++)
  38.     {
  39.         int k= vec[i].size() - 1;
  40.         vec[i].resize(k);
  41.         for (unsigned int j = 0; j < k; j++)
  42.         {
  43.             v_to_list.push_back(vec[i][j]);
  44.         }
  45.     }
  46.     return v_to_list;
  47.     ///
  48. }
  49. //Problem 4. The argument of function is list of many lenghts in cm. Replace them with their corresponding lenght in inchs (inch=2.54cm)
  50. //return sum of lenghts in inches
  51. //do not use iterator just push back to the empty list
  52. double sum_of_inches(std::list<double>&lenght_list)
  53. {
  54.     std::list<double> inches;
  55.     ///solution 4
  56.     double sum = 0;
  57.     int s = lenght_list.size();
  58.     for (int i = 0; i < s; i++)
  59.     {
  60.         double conv = lenght_list.front()*2.54;
  61.         sum += conv;
  62.         inches.push_back(conv);
  63.         lenght_list.pop_front();
  64.     }
  65.     ///
  66.     lenght_list = inches;
  67.     return sum;
  68. }
  69.  
  70. //problem 5. in dque_do_smthg
  71. //a)    create 2 QUEUES in one put 5 in second 2 full names of your choice. After all of that them use funtion disp_queue from utility.cpp on both of queues
  72. //b)    merge them into dequem ,the order does not matter, assign it to argument. Make sure that there is no previus data in deque
  73.  
  74. void dque_do_smthg(std::deque<std::string>&dqueue)
  75. {
  76.     ///solution 3
  77.     std::queue<std::string> q1;
  78.     std::queue<std::string> q2;
  79.     q1.push("cypis");
  80.     q1.push("popek");
  81.     q1.push("linkiewicz");
  82.     q1.push("quebo");
  83.     q1.push("kebabfajde");
  84.  
  85.     q2.push("lech walensa");
  86.     q2.push("kwasniewski");
  87.  
  88.     disp_queue(q1);
  89.     disp_queue(q2);
  90.     dqueue.clear();
  91.     while ((!q1.empty()) || (!q2.empty()))
  92.     {
  93.         if (!q1.empty())
  94.         {
  95.             dqueue.push_back(q1.front());
  96.             q1.pop();
  97.         }
  98.         if (!q2.empty())
  99.         {
  100.             dqueue.push_front(q2.front());
  101.             q2.pop();
  102.         }
  103.     }
  104. }
  105. //Problem 6 remove the second object on stack from top (remeber that top variable should not disappear)
  106. void stack_remove_sec_from_top(std::stack<float> &st)
  107. {
  108.     ///sol 6
  109.     float temp = st.top();
  110.     if (st.empty()) return;
  111.     st.pop();
  112.     if (st.empty()) return;
  113.     st.pop();
  114.     st.push(temp);
  115.  
  116. }
  117. //Problem 7 Display all elements form 0 to N contained in set.
  118. void show_set_elem(std::set<unsigned int> ui_set, int N)
  119. {
  120.     ///
  121.     for (unsigned int i = 0; i < N; i++)
  122.     {
  123.         if (ui_set.count(i))
  124.         {
  125.             std::cout << "set It have: " << i << std::endl;
  126.         }
  127.     }
  128. }
  129. //Problem 8 Display all coords basing on x-axis formating ( , )
  130. void show_map_elem(std::map<int,int> coord_map,int x_min,int x_max)// get val pair.first or pair.second
  131. {
  132.     ///
  133.     for (int i = x_min; i < x_max; i++)
  134.     {
  135.         if (coord_map.count(i))
  136.         {
  137.             std::cout << "(" << i << "," << coord_map[i] << "),   ";
  138.         }
  139.     }
  140. }
  141.  
  142. int main()
  143. {
  144.    
  145.     std::cout << "Problem 1:" << std::endl;
  146.     std::vector<std::vector<int>> vec = { {0,1,2},  {3,4,5},  {-6,-7,-8} };
  147.     disp_matrix(vec);
  148.  
  149.     std::cout <<std::endl<< "Problem 2 :" << std::endl;
  150.     std::cout << std::endl << "Resizing..." << std::endl << std::endl;
  151.     std::list<int> lst=shrink_square_matrix(vec);
  152.     disp_matrix(vec);
  153.     std::cout << std::endl << std::endl << "Problem 3" << std::endl<<std::endl << "The matrix converted to list: ";
  154.     display_list(lst);
  155.    
  156.     std::cout << std::endl << std::endl;
  157.     std::cout << "Problem 4:" << std::endl<<std::endl;
  158.     std::list<double> lngth;
  159.     lngth.push_back(2.489626556016);
  160.     lngth.push_back(100);
  161.     lngth.push_back(4.14937759336099);
  162.     lngth.push_back(200);
  163.     std::cout << "A list before any operations: " << std::endl;
  164.     display_list(lngth);
  165.     std::cout << std::endl<<std::endl<< "The sum is a:" << sum_of_inches(lngth) << std::endl << "A list after many operations:" << std::endl;
  166.     display_list(lngth);
  167.     std::cout << std::endl << std::endl << "Problem 5" << std::endl << std::endl;
  168.     std::deque<std::string> a;
  169.     a.push_back("this should not be");
  170.     a.push_back("displayed");
  171.     dque_do_smthg(a);
  172.     std::cout << std::endl;
  173.     disp_dequeue(a);
  174.     std::cout << std::endl << "Problem 6" << std::endl;
  175.     std::stack<float> fstack;
  176.     fstack.push(0.402005);
  177.     fstack.push(10.2);
  178.     fstack.push(111.32);//remove this
  179.     fstack.push(1.37);
  180.    
  181.     display_stack(fstack);
  182.     stack_remove_sec_from_top(fstack);
  183.     display_stack(fstack);
  184.    
  185.     std::cout << std::endl << std::endl << "Problem 7" << std::endl<<std::endl;
  186.     std::set<unsigned int> ui;
  187.     ui.insert(13);
  188.     ui.insert(32);
  189.     ui.insert(2933);
  190.     ui.insert(3);
  191.     ui.insert(1999);
  192.     ui.insert(34);
  193.     ui.insert(99);
  194.     show_set_elem(ui, 3000);
  195.  
  196.     std::cout << std::endl<< std::endl << "Problem 8" << std::endl<<std::endl;
  197.     std::map<int, int> crds;
  198.     crds[-10] = 31;
  199.     crds[0] = 13;
  200.     crds[5] = 99;
  201.     crds[-3] = -5;
  202.     crds[1] = 2;
  203.     show_map_elem(crds, -20, 20);
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement