Stek birinchi va oxirgi elementlari o‘rni almashtirilsin


Download 46.61 Kb.
Sana17.06.2023
Hajmi46.61 Kb.
#1529862
Bog'liq
Labaratory 3


MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT TEXNOLOGIYALARI
UNIVERSITETINING DASTURIY INJINIRING
FAKULTETI TALABASI OTABEK TO’LAGANOVNING
DASTURLASH II FANIDAN

LABARATORIYA ISHI



TOSHKENT 2023

  1. Stek birinchi va oxirgi elementlari o‘rni almashtirilsin.

#include <iostream>
#include <stack>
#include <deque>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
srand (time (0));
stack stk;
deque dek;
int n, first, x, last;
cout << "n = "; cin >> n;
last = rand () % 100;
stk.push(last);
for (int k = 2; k < n; k++) {
x = rand () % 100;
stk.push(x);
dek.push_front(x); }
first = rand () % 100;
stk.push(first);
dek.push_back(first);
dek.push_front(last);
cout << endl;
while (! stk.empty()) {
cout << stk.top() << "\t";
stk.pop(); }
cout << endl;
for (auto i: dek)
cout << i << "\t";
cout << endl;
return 0; }


2.Berilgan float turidagi to’plam qiymatlarining toqlari 2- va 3- to’plamda nechta marta qatnashganligini aniqlovchi va ularni ekranga chiqaruvchi dastur tuzing. Ikkala to’plamdan ham bir xil qiymatli elemetlar o’chirilib
Saralangan to’plam hosil qilinsin hamda uning qiymatlari ekranga chiqarilsin. Berilgan to’plam qiymatlarining toqlari yig’indisi hisoblansin.
#include <iostream>
#include <set>

int main() {
// Define the given sets
std::set set1 = {1, 2, 3, 4, 5};
std::set set2 = {2, 3, 4, 5, 6};
std::set set3 = {3, 4, 5, 6, 7};
// Define a set to store the odd values
std::set oddValues;

// Iterate over set2 and set3 to find odd values and remove duplicates
for (float val : set2) {
if (int(val) % 2 != 0) {
oddValues.insert(val);
set3.erase(val);
}
}
for (float val : set3) {
if (int(val) % 2 != 0) {
oddValues.insert(val);
}
}

// Output the odd values and their frequency
std::cout << "Odd values and their frequency: " << std::endl;
for (float val : oddValues) {
int count = 0;
if (set2.find(val) != set2.end()) {
count++;
}
if (set3.find(val) != set3.end()) {
count++;
}
std::cout << "Value " << val << " participated " << count << " times" << std::endl;
}

// Create a sorted set and output its values
std::set sortedSet;
sortedSet.insert(set2.begin(), set2.end());
sortedSet.insert(set3.begin(), set3.end());
std::cout << "Sorted set: ";
for (float val : sortedSet) {
std::cout << val << " ";
}
std::cout << std::endl;

// Calculate the sum of odd values
float sum = 0.0;
for (float val : oddValues) {
sum += val;
}
std::cout << "Sum of odd values: " << sum << std::endl;

return 0;
}


3.To’plam kalitlarining necha marotaba takrorlanganligini aniqlovchi va bu
qiymatni chop etuvchi dastur tuzing.
#include <iostream>
#include <map>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
srand (time(NULL));
multimap , int> mmap;
multimap <int, int> :: iterator it;
int n, key, value, count;
bool unique;
cout << "n = "; cin >> n;
array , 10> arr;
for
(int i = 0; i < n; i++) {
key = rand () % 10;
value = rand () % 100;
mmap.insert(pair <int, int> (key, value));
arr[i] = key;
}
for
(it = mmap.begin(); it != mmap.end(); it++)
cout << it -> first << "\t" << it -> second << endl;
cout << endl;
for (int i = 0; i < n; i++) {
count = 0;
unique = true;
for (int j = 0; j < i; j++)
if (arr[i] == arr[j]) {
unique = false;
break;
}
for (int j = 0; j < n; j++)
if (arr[i] == arr[j]) count++;
if (unique)
printf ("Key: %d -> %d times\n", arr[i], count);
}
return 0;
}
1 41
1 42
5 64
6 65
7 83
7 20
7 90
8 65
9 92

Key: 8 -> 1 times


Key: 7 -> 3 times
Key: 1 -> 2 times
Key: 0 -> 1 times
Key: 9 -> 1 times
Key: 6 -> 1 times
Key: 5 -> 1 times
PS D:\CPP>
Download 46.61 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling