Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
Note: each question is independent, i.e. in each time, you are adding only one function below "//** add here **".
Make a guess without running it, then actually test it with your computer.
|
|
|
|
sum
. The function
sums integers from start
and end
. For example,
sum(2,5)
will return 14, because we have 2+3+4+5 = 14.
Fill out the blank.
int sum(int start, int end)
{
if( start > end )
return 0;
else if( )
return end;
else
return start + sum(start+1, end);
}
pow(2, 5)
should return 32, and
pow(3, 4)
should return 81. No loops!
#include <iostream>
using namespace std;
double pow(double, int);
int main()
{
double x;
int n;
cout << "Enter x and n: ";
cin >> x >> n;
cout << "x^n is " << pow(x,n) << endl;
return 0;
}
// Define pow: it must be recursive!
Submit: ~/bin/submit -c=IC210 -p=hw26 hw.cpp
Turn in the codeprint of your source code.