Hello sr2cute702,
I will start at the top.
Line 10 I changed "MAX" to "MAXSIZE" because it is a better description for the variable name. Not important to change, but something to think about for the future.
In the function proto types and function definitions along with the arrays in main I changed all the "int"s to "doubles".
On line 28 you pass (
list01,
list02, n, N) but on line 37 you receive as (int
list02[], int
list01[], int &n, int &N). Notice a difference?
Line 31 Although it works and was not a problem it is better to stay consistent, i.e., (list01, Sum01, list02, Sum02, n, N). Switching the order of the "01"s and "02"s can get confusing, i.e., line 28 and the function definition.
Line 37 the "get_data" function I changed the order of the variables "01" and "02" because the input was going into the wrong arrays. I also set up the function to input from a file, Just as easy to do it now as later. Also I would have initialized the arrays in main with the same numbers to save from typing each time I tested the program. Just as easy to have the program read from a file right from the start.
The next change was to
if (temp % 2 == 0)
. Changing "temp" to a double I had to change this line to
if (static_cast<int>(temp) % 2 == 0)
to work correctly. Which is OK because all you need to do is determine if what is on the lhs of the decimal point is negative or positive. In the end it still stores the floating point number read from the file. I also took out the square and cube root from the input to keep the original numbers in the arrays.
The "summation" functions work fine with the exception of "summation5". I had to change the for loop to work correctly
(int j = 0; j < 5; j++)
. Because you only need the first five.
In the "print_results" function this is where I squared and used the "cbrt" function in the output. Call it a personal quirk of mine, but I changed the headings a bit and used "std::setw()" to line up the numbers better. Rather than trying to describe everything here is what I did:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void print_results(double list02[], double sum02, double sum01, double list01[], int n, int N)
{
cout << "Input Array:\t2nd Array:\n Squared\tCube root\n" << endl;
for (int j = 0; j < N; j++)
{
cout << right << fixed << setprecision(5); // <--- Removed the setw().
if (j < n)
std::cout << setw(10) << list01[j] * list01[j]; // <--- Better to put the setw() here.
if (j < N && j< n)
std::cout << std::setw(14) << std::cbrt(list02[j]) << std::endl;
else
std::cout << setw(10) << " " << std::setw(14) << std::cbrt(list02[j]) << std::endl;
std::cout << std::endl;
}
//cout << fixed << setprecision(5); // <--- Already set.
cout << "Sum of all values in array 1: " << sum01 << '\n' << endl; // <--- Could also call the function here instead of passing the variable.
cout << "Sum of first 5 values in array 2: " << sum02 << '\n' << endl; // <--- Could also call the function here instead of passing the variable
}
|
One last note it is always a good idea to initialize you variables when defined. It saves headaches later.
Hope that helps,
Andy