MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNVIRSITETI
TELEKOMMUNIKATSIYALAR FAKULTETI
Dasturlash 2 fani
2 - topshiriq
Bajardi: SWD010-2 guruh talabasi
Narzullayev Sherali
Tekshirdi: Cho’lliyev Shoxrux Ibadullayevich
TOSHKENT 2023
1-Masala
SAVOL;
15. Taomlar haqida ma’lumotlar berilgan (class Meal). Suyuq va qo‘yiq taomlarni nomalri asosida qidirib topuvchi dastur tuzing
#include
#include
using namespace std;
class Meal {
public:
string name;
bool is_liquid;
Meal(string n, bool l) {
name = n;
is_liquid = l;
}
};
int main() {
vector meals;
meals.push_back(Meal("sho'rva ", true));
meals.push_back(Meal("osh", false));
meals.push_back(Meal("mastava ", true));
meals.push_back(Meal("grill", false));
meals.push_back(Meal("chuchvara", true));
meals.push_back(Meal("tovuqli sho'rva ", true));
string search_term;
cout << "Qanaqa ovqat kerak : ";
getline(cin, search_term);
vector matching_meals;
for (Meal m : meals) {
if (m.name.find(search_term) != string::npos) {
matching_meals.push_back(m);
}
}
if (matching_meals.size() == 0) {
cout << "Bizda bunday ovqat yo'q." << endl;
}
else {
cout << "ovqat bor:" << endl;
for (Meal m : matching_meals) {
cout << "- " << m.name << (m.is_liquid ? " (suyuq)" : " (quyuq)") << endl;
}
}
return 0;
}
2-SAVOL
14.String turidagi To‘plam berilgan. Undagi elementlarni char turidagi A massivga joylashtirib, polindrom so‘zlar sonini aniqlovchi dastur tuzing
KODLARI:
#include
#include
using namespace std;
bool isPolynomialWord(string word) {
int n = word.length();
if (n == 0 || n == 1) {
return false;
}
if (word[0] != 'A' || word[n-1] != 'A') {
return false;
}
for (int i = 1; i < n-1; i++) {
if (word[i] != 'A') {
return false;
}
}
return true;
}
int main() {
string collection[] = {"A", "AA", "ABAA", "AAA", "ABBA", "ABBBBA"};
int count = 0;
int n = sizeof(collection) / sizeof(collection[0]);
for (int i = 0; i < n; i++) {
if (isPolynomialWord(collection[i])) {
count++;
}
}
cout << "polindrom sonlar: " << count << endl;
return 0;
}
3-savol:
15 X to‘plam berilgan. Uning juft indeksli qiymatlarini Z to‘plamga ko‘chirib yozing. Z to‘plamdagi maksimal va minimal elementlarining o‘rnini o‘zaro almashtirib, chiqaruvchi dastur tuzing.
KODLAR:
#include
using namespace std;
int main() {
int X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int Z[5];
int j = 0;
for (int i = 0; i < 10; i += 2) {
Z[j] = X[i];
j++;
}
int max = Z[0];
int min = Z[0];
for (int i = 0; i < 5; i++) {
if (Z[i] > max) {
max = Z[i];
}
if (Z[i] < min) {
min = Z[i];
}
}
for (int i = 0; i < 5; i++) {
if (Z[i] == max) {
Z[i] = min;
} else if (Z[i] == min) {
Z[i] = max;
}
}
cout << "Z to'plam: ";
for (int i = 0; i < 5; i++) {
cout << Z[i] << " ";
}
cout << endl;
return 0;
}
Do'stlaringiz bilan baham: |