1-amaliy mashg’ulot topshiriqlari


-amaliy mashg’ulot topshiriqlari


Download 1.56 Mb.
bet2/2
Sana18.06.2023
Hajmi1.56 Mb.
#1583729
1   2
Bog'liq
6 ta Vazifaaaaa

2-amaliy mashg’ulot topshiriqlari
1-topshiriq
Quyidagi masalalar uchun algoritm va dastur kodini yozing. Har bir talaba o’zining jurnaldagi raqami bo’yicha bittadan masalani yechadi. Masalalar kam bo’lganda sanoq tartib boshidan boshlanadi. Masalan, 20 masala bo’lsa, 21-o’rindagi talaba 1-masalani, 22-o’rindagi talaba 2-masalani yechadi.
Bir o`lchamli sonli massiv max elеmеnti bilan k chi elеmеnti o’rnini almashtiring. max elementdan bir necha bo’lishi mumkin.
Kiruvchi ma’lumotlar: Birinchi satrda n (1<=n <=100). Ikkinchi satrda n ta
butun son (-100<=a[i]<=100). Uchinchi satrda k (1<=k<=100)
Dastur kodi: #include
#include

int main() {


int n;
std::cout << "Massivning o'lchamini kiriting (n): ";
std::cin >> n;

std::vector array(n);


std::cout << "Massiv elementlarini kiriting:" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
}

int k;
std::cout << "k ni kiriting: ";


std::cin >> k;

int maxElement = array[0];


for (int i = 1; i < n; i++) {
if (array[i] > maxElement) {
maxElement = array[i];
}
}

int temp = array[k - 1];


array[k - 1] = maxElement;

std::cout << "Yangi massiv:" << std::endl;


for (int i = 0; i < n; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;

return 0;


}


2-topshiriq
Quyidagi masalalar uchun algoritm va dastur kodini yozing. Har bir talaba o’zining jurnaldagi raqami bo’yicha bittadan masalani yechadi. Masalalar kam bo’lganda sanoq tartib boshidan boshlanadi. Masalan, 20 masala bo’lsa, 21-o’rindagi talaba 1-masalani, 22-o’rindagi talaba 2-masalani yechadi.

Quyidagi funksiyani to’rtburchaklar, Trapetsiya va Simpson formulalari yordamida taqribiy hisoblash dasturini tuzing. Har bir talaba guruh jurnalidagi raqami bo’yicha bitta masala uchun dastur tuzadi.



  1. #include

  2. #include


  3. double f(double x) {

  4. return sin(x) / pow(cos(x), 3);

  5. }


  6. double approximateIntegralRectangles(double a, double b, int n) {

  7. double h = (b - a) / n;

  8. double integral = 0.0;


  9. for (int i = 0; i < n; i++) {

  10. double x = a + i * h;

  11. integral += f(x);

  12. }


  13. integral *= h;

  14. return integral;

  15. }


  16. double approximateIntegralTrapezoid(double a, double b, int n) {

  17. double h = (b - a) / n;

  18. double integral = 0.0;


  19. for (int i = 0; i <= n; i++) {

  20. double x = a + i * h;

  21. if (i == 0 || i == n) {

  22. integral += f(x) / 2;

  23. } else {

  24. integral += f(x);

  25. }

  26. }


  27. integral *= h;

  28. return integral;

  29. }


  30. double approximateIntegralSimpson(double a, double b, int n) {

  31. if (n % 2 != 0) {

  32. std::cout << "Error: n should be an even number for Simpson's rule." << std::endl;

  33. return 0.0;

  34. }


  35. double h = (b - a) / n;

  36. double integral = 0.0;


  37. for (int i = 0; i <= n; i++) {

  38. double x = a + i * h;

  39. if (i == 0 || i == n) {

  40. integral += f(x);

  41. } else if (i % 2 == 0) {

  42. integral += 2 * f(x);

  43. } else {

  44. integral += 4 * f(x);

  45. }

  46. }


  47. integral *= h / 3;

  48. return integral;

  49. }


  50. int main() {

  51. double a = 0.0;

  52. double b = 1.0;

  53. int n;


  54. std::cout << "Enter the number of intervals (n): ";

  55. std::cin >> n;


  56. double integralRectangles = approximateIntegralRectangles(a, b, n);

  57. double integralTrapezoid = approximateIntegralTrapezoid(a, b, n);

  58. double integralSimpson = approximateIntegralSimpson(a, b, n);


  59. std::cout << " To'rtburchaklar yordamida taxminiy integral: " << integralRectangles << std::endl;

  60. std::cout << " Trapetsiya qoidasi yordamida taxminiy integral: " << integralTrapezoid << std::endl;

  61. std::cout << " Simpson qoidasi yordamida taxminiy integral: " << integralSimpson << std::endl;


  62. return 0;

  63. }



3-amaliy mashg’ulot topshiriqlari
1-topshiriq
Quyidagi masalalar uchun algoritm va dastur kodini yozing. Har bir talaba o’zining jurnaldagi raqami bo’yicha bittadan masalani yechadi. Masalalar kam bo’lganda sanoq tartib boshidan boshlanadi. Masalan, 20 masala bo’lsa, 21-o’rindagi talaba 1-masalani, 22-o’rindagi talaba 2-masalani yechadi.
Quyidagi transsendent tenglamani oraliqni teng ikkiga bo’lish va oddiy iteratsiya usuli yordamida yechimini aniqlovchi dastur tuzing:

2x-lgx=3
Dastur kodi:


#include
#include

double f(double x) {


return 2 * x - log10(x) - 3;
}

double g(double x) {


return (2 * x - 3) / log(10);
}

double solveEquation(double a, double b, double epsilon) {


double x0 = (a + b) / 2;
double x1 = g(x0);
double error = std::abs(x1 - x0);

while (error >= epsilon) {


x0 = x1;
x1 = g(x0);
error = std::abs(x1 - x0);
}

return x1;


}

int main() {


double a, b, epsilon;

std::cout << "Intervalni kiriting [a, b]: ";


std::cin >> a >> b;

std::cout << "Kerakli aniqlikni kiriting (epsilon): ";


std::cin >> epsilon;

double solution = solveEquation(a, b, epsilon);

std::cout << "Taxminiy yechim: " << solution << std::endl;

return 0;


}

Bu kodda, f funksiyasi berilgan transsendent tenglamaning solishtiruvchi tomoni ifodalaydi (2x - log10(x) - 3). g funksiyasi esa oraliqni teng ikkiga bo'lish formulasi bilan f(x) / log(10) ifodalangan. solveEquation funksiyasi oraliqni teng ikkiga bo'lish va oddiy iteratsiya usuli yordamida yechimni topish uchun ishlatiladi.


main funksiyasida foydalanuvchidan tenglamaning qiymatlarini va epsilon (xato limiti) ni olish uchun so'raladi. solveEquation funksiyasi oraliqni teng ikkiga bo'lish va oddiy iteratsiya usulidan foydalanarak yechimni hisoblayadi.
Dastur natijasida aniqlovchi qiymat (Approximate solution) ekranga chiqariladi.
2-topshiriq
Quyidagi masalalar uchun algoritm va dastur kodini yozing. Har bir talaba o’zining jurnaldagi raqami bo’yicha bittadan masalani yechadi. Masalalar kam bo’lganda sanoq tartib boshidan boshlanadi. Masalan, 20 masala bo’lsa, 21-o’rindagi talaba 1-masalani, 22-o’rindagi talaba 2-masalani yechadi.
Quyidagi transsendent tenglamani Nyuton va Vatarlar usuli yordamida yechimini aniqlovchi dastur tuzing:
x(x+1)2=2
Dastur kodi: #include
#include

double f(double x) {


return x * pow(x + 1, 2) - 2;
}

double df(double x) {


return 3 * pow(x, 2) + 4 * x + 1;
}

double solveEquationNewtonRaphson(double x0, double epsilon, int maxIterations) {


double x = x0;
int iterations = 0;
double error = std::numeric_limits::max();

while (error >= epsilon && iterations < maxIterations) {


double dx = f(x) / df(x);
x = x - dx;
error = std::abs(dx);
iterations++;
}

return x;


}

double solveEquationSecant(double x0, double x1, double epsilon, int maxIterations) {


double x2;
int iterations = 0;
double error = std::numeric_limits::max();

while (error >= epsilon && iterations < maxIterations) {


x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
error = std::abs(x2 - x1);
x0 = x1;
x1 = x2;
iterations++;
}

return x2;


}

int main() {


double x0, x1, epsilon;
int maxIterations;

std::cout << "Dastlabki taxminni x0 kiriting: ";


std::cin >> x0;

std::cout << "Kerakli aniqlikni kiriting (epsilon): ";


std::cin >> epsilon;

std::cout << "Maksimal takrorlash sonini kiriting: ";


std::cin >> maxIterations;

double solutionNewtonRaphson = solveEquationNewtonRaphson(x0, epsilon, maxIterations);


double solutionSecant = solveEquationSecant(x0, x0 + 0.01, epsilon, maxIterations);

std::cout << "Nyuton-Rafson usuli yordamida taxminiy yechim: " << solutionNewtonRaphson << std::endl;


std::cout << "Sekant usuli yordamida taxminiy yechim: " << solutionSecant << std::endl;

return 0;


}

Download 1.56 Mb.

Do'stlaringiz bilan baham:
1   2




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