Facebook
From Tani, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 197
  1. /*------------Lab1-------------
  2. 1)
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n;
  8.     cin >> n;
  9.     int a[n];
  10.     for(int i=0;i<n;i++)
  11.     {
  12.         cin >>a[i];
  13.         cout<<a[i]<<" ";
  14.     }
  15.  
  16.     return 0;
  17. }
  18.  
  19. 2)
  20.  
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int main() {
  25.     int n;
  26.     cin >> n;
  27.     int a[n];
  28.     for(int i=0;i<n;i++)
  29.     {
  30.         cin >>a[i];
  31.     }
  32.     for(int i=0;i<n;i++ )
  33.     {
  34.         if(a[0]<a[i])
  35.         a[0]=a[i];
  36.     }
  37.     cout<<a[0]<<endl;
  38.  
  39.     return 0;
  40. }
  41.  
  42. 3)
  43.  
  44. #include <bits/stdc++.h>
  45. using namespace std;
  46.  
  47. int main() {
  48.     double a, b, c, x1, x2, x;
  49.     cin >> a >> b >> c;
  50.  
  51.     double d = b * b - 4 * a * c;
  52.  
  53.     if (d > 0) {
  54.         x1 = (-b + sqrt(d)) / (2 * a);
  55.         cout << "x1 = " << x1 << endl;
  56.  
  57.         x2 = (-b - sqrt(d)) / (2 * a);
  58.         cout << "x2 = " << x2 << endl;
  59.     } else if (d == 0) {
  60.         x = -b / (2 * a);
  61.         cout << "Unique solution: " << x << endl;
  62.     } else {
  63.         cout << "No real solution" << endl;
  64.     }
  65.  
  66.     return 0;
  67. }
  68. ------------------lab 2----------------
  69. 4)
  70.  
  71. #include <iostream>
  72. using namespace std;
  73. int main()
  74. {
  75.     int arr[10],even[10],odd[10],evncnt=0,oddcnt=0,i;
  76.     for(i=0;i<10;i++)
  77.         cin>>arr[i];
  78.     for(i=0;i<10;i++)
  79.     {
  80.             if(arr[i]%2==0)
  81.                 even[evncnt++]=arr[i];
  82.             else
  83.                 odd[oddcnt++]=arr[i];
  84.     }
  85.     for(i=0;i<evncnt;i++)
  86.         cout<<"Even:"<<even[i]<<" ";
  87.     for(i=0;i<oddcnt;i++)
  88.         cout<<"Odd:"<<odd[i]<<" ";
  89. }
  90.  
  91. 5)
  92.  
  93. #include <iostream>
  94. using namespace std;
  95.  
  96. int main() {
  97.     int n;
  98.     cin >> n;
  99.     int a[n],pos,item;
  100.     for(int i=0;i<n;i++){
  101.         cin>>a[i];
  102.     }
  103.     cin >> pos>>item;
  104.     for(int i=n;i>=pos;i--){
  105.         a[i]=a[i-1];
  106.     }
  107.     a[pos]=item;
  108.     n++;
  109.     for(int i=0;i<n;i++){
  110.         cout<<a[i]<<" ";
  111.     }
  112.     return 0;
  113. }
  114.  
  115. 6)
  116.  
  117. #include <iostream>
  118. using namespace std;
  119.  
  120. int main() {
  121.     int n;
  122.     cin >> n;
  123.     int a[n], item, found = 0;
  124.     for(int i = 0; i < n; i++) {
  125.         cin >> a[i];
  126.     }
  127.     cin >> item;
  128.  
  129.     for(int i = 0; i < n; i++) {
  130.         if(a[i] == item) {
  131.             found = 1;
  132.             for(int j = i; j < n - 1; j++) {
  133.                 a[j] = a[j + 1];
  134.             }
  135.             n--;
  136.             i--;
  137.         }
  138.     }
  139.  
  140.     if(found) {
  141.         for(int i = 0; i < n; i++) {
  142.             cout << a[i] << " ";
  143.         }
  144.     } else {
  145.         cout << "not found";
  146.     }
  147.     return 0;
  148. }
  149. 7)
  150.  
  151. #include <iostream>
  152. using namespace std;
  153.  
  154. int main() {
  155.     int n;
  156.     cin >> n;
  157.     int a[n];
  158.     for(int i = 0; i < n; i++) {
  159.         cin >> a[i];
  160.     }
  161.     for(int i = 0; i < n - 1; i++) {
  162.         for(int j = 0; j < n - i - 1; j++) {
  163.             if(a[j] > a[j + 1]) {
  164.                 swap(a[j], a[j + 1]);
  165.             }
  166.         }
  167.     }
  168.     for(int i = 0; i < n; i++) {
  169.         cout << a[i] << " ";
  170.     }
  171.     return 0;
  172. }
  173.  
  174. --------------lab3--------------
  175.  
  176. 8)
  177.  
  178. #include <iostream>
  179. using namespace std;
  180.  
  181. int main() {
  182.     int n;
  183.     cin >> n;
  184.     int a[n], x, pos = -1;
  185.  
  186.     for(int i = 0; i < n; i++) {
  187.         cin >> a[i];
  188.     }
  189.     cin >> x;
  190.     for(int i = 0; i < n; i++) {
  191.         if(a[i] == x) {
  192.             pos = i;
  193.             break;
  194.         }
  195.     }
  196.     if(pos != -1)
  197.         cout << "Element found at index: " << pos << endl;
  198.     else
  199.         cout << "Element not found" << endl;
  200.  
  201.     return 0;
  202. }
  203.  
  204. 9)
  205. #include<bits/stdc++.h>
  206. using namespace std;
  207.  
  208. int main()
  209. {
  210.    int n;
  211.    cin >> n;
  212.    int a[n];
  213.    for(int i = 0; i < n; i++)
  214.    {
  215.        cin >> a[i];
  216.    }
  217.    int item;
  218.    cin >> item;
  219.  
  220.    int beg = 0, end = n - 1;
  221.    while(beg <= end)
  222.    {
  223.        int mid = beg + (end - beg) / 2;
  224.        if(a[mid] == item)
  225.        {
  226.            cout << mid << endl;
  227.            return 0;
  228.        }
  229.        if(a[mid] < item)
  230.            beg = mid + 1;
  231.        else
  232.            end = mid - 1;
  233.    }
  234.    cout << "Item not found" << endl;
  235.    return 0;
  236. }
  237.  
  238. 10)
  239.  
  240.  
  241. #include <bits/stdc++.h>
  242. using namespace std;
  243.  
  244. void SieveOfEratosthenes(int n)
  245. {
  246.  bool prime[n + 1];
  247.  memset(prime, true, sizeof(prime));
  248.  
  249.  for (int p = 2; p * p <= n; p++) {
  250.   if (prime[p] == true) {
  251.    for (int i = p * p; i <= n; i += p)
  252.     prime[i] = false;
  253.   }
  254.  }
  255.  
  256.  for (int p = 2; p <= n; p++)
  257.   if (prime[p])
  258.    cout << p << " ";
  259. }
  260.  
  261. int main()
  262. {
  263.  int n;
  264.     cin >> n;
  265.  cout << n << endl;
  266.  SieveOfEratosthenes(n);
  267.  return 0;
  268. }
  269.  
  270. ----------------la4--------------------
  271.  
  272. 12)
  273. #include <iostream>
  274. #include <string>
  275.  
  276. using namespace std;
  277.  
  278. int main() {
  279.     string t;
  280.      getline(cin, t);
  281.     string s ;
  282.     cin >>s;
  283.     int pos;
  284.     cin>>pos;
  285.  
  286.     if (pos < 0 || pos > t.length()) {
  287.         cout << "Invalid position!\n";
  288.     } else {
  289.         t.insert(pos, s);
  290.         cout << "Text after insertion: " << t << endl;
  291.     }
  292.  
  293.     return 0;
  294. }
  295.  
  296. 13)
  297.  
  298. #include<bits/stdc++.h>
  299. using namespace std;
  300.  
  301. int main() {
  302.     string t;
  303.     getline(cin, t);
  304.     size_t position;
  305.     cin >> position;
  306.      size_t l;
  307.     cin >> l;
  308.  
  309.     t.erase(position, l);
  310.  
  311.     cout << t << endl;
  312.  
  313.     return 0;
  314. }
  315.  
  316. 14)
  317.  
  318. #include<bits/stdc++.h>
  319. using namespace std;
  320.  
  321. int main()
  322. {
  323.     string s, p;
  324.     getline(cin, s);
  325.     getline(cin, p);
  326.  
  327.     size_t pos = s.find(p);
  328.  
  329.     if (pos != string::npos)
  330.         cout << "Pattern found at position: " << pos << endl;
  331.     else
  332.         cout << "Pattern not found." << endl;
  333.  
  334.     return 0;
  335. }
  336.  
  337. 15)
  338. #include<bits/stdc++.h>
  339. using namespace std;
  340.  
  341. int main() {
  342.     string str;
  343.     getline(cin, str);
  344.  
  345.     int freq[256] = {0};
  346.  
  347.     for(int i = 0; i < str.length(); i++) {
  348.         freq[str[i]]++;
  349.     }
  350.  
  351.     for(int i = 0; i < 256; i++) {
  352.         if(freq[i] != 0) {
  353.             cout << char(i) << " : " << freq[i] << endl;
  354.         }
  355.     }
  356.  
  357.     return 0;
  358. }
  359.  
  360. 16
  361. #include <iostream>
  362.  
  363. // Function to find the length of a string
  364. int strLength(const char* str) {
  365.     int len = 0;
  366.     while (str[len] != '\0') {
  367.         len++;
  368.     }
  369.     return len;
  370. }
  371.  
  372. // Function to copy string S2 to S1
  373. void strCopy(char* s1, const char* s2) {
  374.     int len = strLength(s2);
  375.     for (int i = 0; i <= len; i++) {
  376.         s1[i] = s2[i];
  377.     }
  378. }
  379.  
  380. // Function to concatenate string S2 to S1
  381. void strConcatenate(char* s1, const char* s2) {
  382.     int len1 = strLength(s1);
  383.     int len2 = strLength(s2);
  384.     for (int i = 0; i < len2; i++) {
  385.         s1[len1 + i] = s2[i];
  386.     }
  387.     s1[len1 + len2] = '\0';
  388. }
  389.  
  390. // Function to compare two strings S1 and S2
  391. int strCompare(const char* s1, const char* s2) {
  392.     int len1 = strLength(s1);
  393.     int len2 = strLength(s2);
  394.     if (len1 != len2) {
  395.         return 1;
  396.     }
  397.     for (int i = 0; i < len1; i++) {
  398.         if (s1[i] != s2[i]) {
  399.             return 1;
  400.         }
  401.     }
  402.     return 0;
  403. }
  404.  
  405. // Function to reverse a string S
  406. void strReverse(char* s) {
  407.     int len = strLength(s);
  408.     for (int i = 0; i < len / 2; i++) {
  409.         char temp = s[i];
  410.         s[i] = s[len - i - 1];
  411.         s[len - i - 1] = temp;
  412.     }
  413. }
  414.  
  415. int main() {
  416.     // Create two strings S1 and S2
  417.     char S1[100] = "Hello";
  418.     char S2[100] = "World";
  419.  
  420.     // Find the length of S1
  421.     std::cout << "Length of S1: " << strLength(S1) << std::endl;
  422.  
  423.     // Copy S2 to S1
  424.     strCopy(S1, S2);
  425.     std::cout << "Copied S2 to S1: " << S1 << std::endl;
  426.  
  427.     // Concatenate S2 to S1
  428.     strConcatenate(S1, S2);
  429.     std::cout << "Concatenated S2 to S1: " << S1 << std::endl;
  430.  
  431.     // Compare S1 and S2
  432.     if (strCompare(S1, S2)) {
  433.         std::cout << "S1 and S2 are not equal" << std::endl;
  434.     } else {
  435.         std::cout << "S1 and S2 are equal" << std::endl;
  436.     }
  437.  
  438.     // Reverse S1
  439.     strReverse(S1);
  440.     std::cout << "Reversed S1: " << S1 << std::endl;
  441.  
  442.     return 0;
  443. }