0% found this document useful (0 votes)
13 views5 pages

NC assignment-BSR

The document contains code for two programming problems. The first problem involves calculating values for variables x, y, and z given values for M, q, and r. The second problem uses backward interpolation to estimate population values based on sample data points.

Uploaded by

Rafay Naveed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

NC assignment-BSR

The document contains code for two programming problems. The first problem involves calculating values for variables x, y, and z given values for M, q, and r. The second problem uses backward interpolation to estimate population values based on sample data points.

Uploaded by

Rafay Naveed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

nc assignment 1 part 2

Abdur Rafay
[Company name] [Company address]
Q2
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double M, q, r;

cout << "Enter M: ";


cin >> M;
cout << "Enter q: ";
cin >> q;
cout << "Enter r: ";
cin >> r;

double x = sqrt(r * r * (1 - 2 * M / r + 3 * M * q * q / (r * r * r)));


cout << "x = " << x << endl;

double z = 1.0;

double y = sqrt((pow(1 - 2 * M / r + 3 * M * q * q / (r * r * r), -1)) / ((x + z * r) * (x + z * r)));


cout << "y = " << y << endl;

z = (-2 * M / (r * r) + 6 * M * q * q / (r * r * r * r)) / (2 * y * y * (x + z * r));


cout << "z = " << z << endl;

return 0;
}

Q3
Part 1
Part 2
#include<iostream>
using namespace std;

double u_calculator(int n, double x[], double y[], int i, double x_value)


{
double u_result = (x_value - x[i]) / (x[i] - x[i - n]);
return u_result;
}
double backward_interpolation(int n, double x[], double y[], int i, double x_value)
{
double u = u_calculator(n, x, y, i, x_value);
double result = y[i];
double temp = u;
for (int k = 1; k < n; k++)
{
temp = temp * (u - k);
result = result + (temp / k) * y[i - k];
}
return result;
}
int main()
{
int n = 6;
double x[] = { 1941, 1951, 1961, 1971, 1981, 1991 };
double y[] = { 12, 15, 20, 27, 39, 52 };
double x_value;
cout << "Enter the year for which you want to find the population: ";
cin >> x_value;
int i;
for (i = 0; i < n; i++)
{
if (x_value <= x[i])
{
break;
}
}
double population = backward_interpolation(n, x, y, i, x_value);
cout << "The population in the year " << x_value << " is approximately " << population << " thousand." << endl;
return 0;
}

You might also like