
3. yn = (1/n)*yn−1+(1/n^2)*yn−2, y1 = 0.9, y2 = 1.1 Ha C++


Ответы на вопрос

Если я правильно понял условия, тогда вот
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "n:";
cin >> n;
double y = 0.7;
double yn = 0;
for(int i = 1; i<=n; i++){
y = y + 0.2;
yn = (1/i)*y-1+(1/i*i)*y-2;
cout << "y" << i << " = " << yn;
cout << endl;
}
return 0;
}



To implement the given recurrence relation yn = (1/n)*yn−1 + (1/n^2)*yn−2 with initial values y1 = 0.9 and y2 = 1.1 in C++, you can use a loop to calculate subsequent values of yn based on the previous two values. Here's a C++ code to achieve this:
cpp#include <iostream>
#include <vector>
using namespace std;
double calculateYn(int n, double y1, double y2) {
vector<double> y(n + 1); // Create a vector to store values of y from y1 to yn
y[1] = y1; // Set y1
y[2] = y2; // Set y2
// Calculate y3 to yn using the recurrence relation
for (int i = 3; i <= n; i++) {
y[i] = (1.0 / i) * y[i - 1] + (1.0 / (i * i)) * y[i - 2];
}
return y[n]; // Return yn
}
int main() {
int n;
double y1 = 0.9, y2 = 1.1;
cout << "Enter the value of n: ";
cin >> n;
if (n < 1) {
cout << "Invalid value of n. Please enter a positive integer." << endl;
} else {
double yn = calculateYn(n, y1, y2);
cout << "y" << n << " = " << yn << endl;
}
return 0;
}
In this code, the calculateYn
function takes the value of n
and the initial values y1
and y2
as input and returns the calculated value of yn. The main
function takes the value of n
as user input and calls the calculateYn
function to find the value of yn. Note that we use a vector to store the values of y from y1 to yn to easily access the previous values while calculating subsequent values.
Compile and run the program, enter the value of n, and it will output the corresponding value of yn.


Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili