CSCI
6302
Project
#6: Fun with functions!
Due
date: Start of class Thursday, 9/24/2009
Do
the following problems. Some questions
ask you to turn in code, others ask you to turn in a written answer. You may condense all answers requiring code into
a single program for turn in.
#1:
Run the following commands in a program and observe the result. Turn in a written description of what was
output, as well as an explanation of why the values output were produced.
int w = 2147483648;
cout << w-1 << endl;
cout << w << endl;
cout << w+1 << endl;
cout << w+2 << endl;
unsigned u = 4294967296;
cout << u-1 << endl;
cout << u << endl;
cout << u+1 << endl;
cout << u+2 <<
endl;
#2:
Write 2 functions "maximum" and "minimum", each which takes
3 input integers. Test your functions by
running the following lines of code to get 22 and 15 respectively:
cout << "The largets is:
" << maximum(5, 22, 18) << endl;
cout << "The smallest is: " << minimum(15,
18, 16) << endl;
#3: Write
a nice function called "random" that computes a random number between
two given input numbers. Test your
function with the following code to make sure a random number between 5 and 24
is output:
int a = 5;
int b = 24;
cout << "A random number between " << a
<< " and " << b
<< " is " <<
random(a,b) << endl;
#4:
Write a function called "capital" that returns a capitalized version
of an input string. Test your function
with the following code to verify that "ROBBIE" is displayed:
string name = "robbie";
cout << capital(name)
<< endl;
#5:
Write a function that takes an input string and changes the string to all
capital letters. Test your function with
the following code to verify that "BRENDA" is displayed:
string name2 = "brenda";
capitalize(name2);
cout << name2 <<
endl;
#6: Write a function 'listPrimes' to list all the primes up to a given
input integer. Test your functions with
the following code:
listPrimes(15);
which should display:
2
3
5
7
11
13
#7: Write
the mysterious function "mystery" into a program and test it with
some values. Try to figure out what
mystery does, and why it does it. Turn
in a paragraph discussing your theory on what mystery does, along with an
explanation of why it does what you say.
Also, answer the following question:
Is mystery a "cool" function or a "stupid" function.
int mystery( int a, int b )
{
if( b == 0 )
{
return 1;
}
else
{
int x = mystery( a, b/2
);
if( b % 2 == 0 )
{
return x*x;
}
else
{
return x*x*a;
}
}
}