2-Amaliy mashg‘ulotini bajarish bo‘yicha variantlar Topshiriqqa muvofiq funksiyaga doir bo‘lgan masalalarni yechish lozim. n ta elementdan tashkil topgan massiv berilgan.Bu massivning tub
elementlari soni aniqlansin. ()
{
if (n <= 1)
{
return false;
}
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int n;
cout << "Massiv uzunligini kiriting: ";
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
a[i] = rand() % 100 + 1;
}
cout << "Massiv elementi: ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
int count = 0;
for (int i = 0; i < n; i++)
{
if (isPrime(a[i]))
{
count++;
}
}
cout << "Massivda " << count << " ta tub son bor." << endl;
return 0;
}
2-jadval Do’konda sotuvga tushgan avtomobillar ro’yxati mavjud. Bu ro’yxatning
har bir yozuvi quyidagicha: avtomobil markasi, narxi, 100 km uchun
yoqilg’i harajati. Xaridor tomonidan kiritilgan markali avtomobillar
ro’yxatini chiqarish.
#include #include #include using namespace std;
struct Car
{
string make;
double price;
double fuelConsumption;
};
void printCars(vector&cars, string make)
{
cout << "Xaridor tomonidan kiritilgan " << make << " markali avtomobillar:" << endl;
for (int i = 0; i < cars.size(); i++)
{
if (cars[i].make == make)
{
cout << "Marka: " << cars[i].make << ", Narx: " << cars[i].price << ", 100 km uchun yoqilg'i harajati: " << cars[i].fuelConsumption << endl;
}
}
}
int main()
{
vector cars;
int n;
cout << "Avtomobillar sonini kiriting: ";
cin >> n;
for (int i = 0; i < n; i++)
{
Car car;
cout << "Avtomobil markasini kiriting: ";
cin >> car.make;
cout << "Avtomobil narxini kiriting: ";
cin >> car.price;
cout << "100 km uchun yoqilg'i harajatini kiriting: ";
cin >> car.fuelConsumption;
cars.push_back(car);
}
string make;
cout << "Markani kiriting: ";
cin >> make;
printCars(cars, make);
return 0;
}
Berilgan nomdagi matnli faylni yaratish va unga ixtiyoriy matn kiritish
dasturini tuzing.Undagi oxirgi so’z o’chirilsin .
#include using namespace std;
// Faylni yaratish funksiyasi void createFile(string fileName)
{
ofstream file(fileName);
if (!file)
{
cout << "Faylni yaratishda xatolik yuz berdi." << endl;
}
file.close();
}
// Faylga matn yozish funksiyasi void writeToFile(string fileName, string text)
{
ofstream file(fileName, ios::app);
if (!file)
{
cout << "Faylda yozishda xatolik yuz berdi." << endl;
}
file << text << endl;
file.close();
}
// Fayldan matn o'qish funksiyasi string readFromFile(string fileName)
{
ifstream file(fileName);
string text;
string line;
while (getline(file, line))
{
text += line;
}
file.close();
return text;
}