I am writing a C++ program and I am having problems with the incrementing. I wan
ID: 3554358 • Letter: I
Question
I am writing a C++ program and I am having problems with the incrementing. I want the array x[i] to start with the inputted vaule for f, and then double each time. i.e intial f value = 100, then it goes 100, 200, 400, 800, 1600. Here is what I have so far. Any help would be greatl appreciated.
#include <cstdlib>
#include <iostream> //pre-processor directs - iostream talks to screen
#include <iomanip> //pre-processor directs - string
#include <cmath> //allows math functions
using namespace std; //introduces namespace
const int size=100;
int main()
{
double x[size], y[size]; //Declares arrays
double W=100,f;
cout<<" Enter your frequency: ";
cin>>f;
for (int i=0;i<size;i++) //initiates, defines and sets counter
x[i]=(f*(i+i)); //initiates the first value of the array
cout<<"Frequency: "<<"Result: "<<endl; //prints column headers to screen
for (int i=0;i<size;i++) //initiates, defines and sets counter
y[i]=W+x[i]; //converts values in x array to to y array
for (int i=0;i<size;i++) //initiates, defines and sets counter
cout<<setprecision(6)<<setw(10)<<x[i]<<setw(25)<<y[i]<<endl; //prints values in arrays to screen
cout<<endl;
return 0;
}
Explanation / Answer
#include <cstdlib>
#include <iostream> //pre-processor directs - iostream talks to screen
#include <iomanip> //pre-processor directs - string
#include <cmath> //allows math functions
using namespace std; //introduces namespace
const int size=100;
int main()
{
double x[size], y[size]; //Declares arrays
double W=100,f;
cout<<" Enter your frequency: ";
cin>>f;
//x[0]=f;
for (int i=0;i<size;i++) //initiates, defines and sets counter
{
x[i]=f;
f=f*2;
} //initiates the first value of the array
cout<<"Frequency: "<<"Result: "<<endl; //prints column headers to screen
for (int i=0;i<size;i++) //initiates, defines and sets counter
y[i]=W+x[i]; //converts values in x array to to y array
for (int i=0;i<size;i++) //initiates, defines and sets counter
cout<<setprecision(6)<<setw(10)<<x[i]<<setw(25)<<y[i]<<endl; //prints values in arrays to screen
cout<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.