// PartialSum.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
usingnamespace std;
void sum1(double&, unsignedint);
double sum2(unsignedint);
unsignedint getValidPositiveInteger();
int main()
{
unsignedint i = getValidPositiveInteger();
unsignedint n = getValidPositiveInteger();
cout << "Enter the number of terms in the partial sum approximation of the series (n): \n";
cin >> i;
cout << "Using pass by value, the partial term using " << i << " terms is: \n" << sum2(i);
cout << "Using pass by reference, the partial term using " << i << " terms is: \n" << sum1(n, another parameter here);
system("PAUSE\n");
return 0;
}
void sum1(double &sum, unsignedint n)
{
sum = sum2(n);
}
double sum2(unsignedint n)
{
double sum = 0, result;
unsignedint a;
for (a = 1; a <= n; ++a)
{
result = pow((1.0 / a), 2);
sum += result;
}
return (sum);
}
// Get a valid integer from the user
unsignedint getValidPositiveInteger() {
int userInput;
cin >> userInput;
while (cin.fail() || userInput < 0) {
cin.clear();
cin.ignore();
cout << "Please enter a positive integer only\n";
cin >> userInput;
}
return userInput;
}
I'm trying to pass two parameters into the function sum1, I have n but the second one, sum doesn't work, I tried changing the unsigned int n = getPositiveInteger() to a double, but that doesn't work either, if anyone could help me out, that would be so appreciated, thanks.