Prompt: Write a C++ program that ask the user for a non-negative integer n and a
ID: 3651835 • Letter: P
Question
Prompt:Write a C++ program that ask the user for a non-negative integer n and a double number x, (make sure you ask for input in this order, ask n first and then ask for x to make testing easier), then calculate:
f(n, x) = 1+x+x^2+ x^3 + ... + x^n
where x^i is x to the power of i.
There are many different ways of calculating this value, please implement the following methods.
use pow function defined in <cmath> library to assist computing f(n, x)
write your code without using any library function to compute f(n,x), optimize your code if necessary so that your code contains only one level of loop (no nested loop)
use pow function defined in <cmath> library to compute:
g(n, x), where g(n, x) = n+1 when x is 1; and g(n, x) = (1-x^(n+1))/(1-x) when x is not 1.
Print out all three results of your computation from the 3 algorithms with 14 digits precision total. You should configure cout with just the following statement:
cout.precision(14);
before you use cout to print out double numbers.
this is the prompt and im lost in the water the only two libraries we can use are
#include<iostream>
#include <cmath>
please help thank you.
Explanation / Answer
#include #include f1(int n, double x) { double s=1; while(n) { s+=pow(x,n); n--; } return s; } f2(int n, double x) { double t= 1;s= 1; while(n--) { t*=x; s=s+t; } return s; } f3(int n, double x) { n++; if(x==1) return n; else return (1-pow(x,n))/(1-x); } void main() { int n; double x; coutx; cout.precision(14); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.