i want know if it was understood right - this is the problem:
The Federal Reserve wants a program that can give the average interest rate on mortgages using data from four nationally ranked banks. Your tasks are to create a program that calculates the average of values stored in a one-dimensional double array named rates. The program should display the average rate on the screen.
Use the following data to:
Bank of America 6.2%, RBC Centura 7.1%, BB%26amp;T 5.9%, Federal Credit Union 6.0%
this is how i code it:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
//declare array
double total = 0;
double average = 0;
double rate[4] = {6.2, 7.1, 5.9, 6.0};
// calculate sum
for (int x = 0; x %26lt; 4; x = x + 1)
total = total + rate[x];
//end for
//calculate and display average
average = total / 4;
cout %26lt;%26lt; "Average: " %26lt;%26lt; average %26lt;%26lt;endl;
return 0;
}//end of the displayAverage program
is that what the problem is asking me to do? if so, is my code right?
Did i interprete this C++ problem right?
Looks correct to me. As long as your math is right-which it appears to be (seems simple enough). Only thing is this, which has to do with your syntax:
// calculate sum
for (int x = 0; x %26lt; 4; x = x + 1)
{
total = total + rate[x];
}
//end for
You just forgot the open/close brackets there however perhaps you were coding more of an abstract version of the program.
Reply:use x++ instead of x+1
I think C++ Compiler may consider x+1 in for loop as error.
http://www.karmadirectorysubmission.com/...
Reply:the brackets are not necessary for 1 line in a for loop. it's good practice but it is not an error.
x++ is shorter, but otherwise what he is doing "x=x+1" will work fine.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment