In this exercise you will write function definitions. You will not write your ow
ID: 3625658 • Letter: I
Question
In this exercise you will write function definitions. You will not write your own main, only separate function definitions in the same .cpp source code file. To test your functions to see if they work, you will need to use the main( ) provided to call each of your functions. Type this main( ) under all the functions you have written. You are NOT to use any of the C++ library functions!
a) Write a function that takes in a double value and returns the absolute value of that number. Name the function absoluteValue.
b) Write a function that takes in three double values and returns the maximum number of the three. Name the function maxOfThree.
c) Write a function that takes in a single character and returns the uppercase version of that character if the parameter is a lowercase letter, otherwise the function returns the same character as the input parameter. Hint: use the ASCII code, you can use a char as an int ie: ‘A’ + 1 is ‘B’, ‘G’ – 2 is ‘E’.
Name the function giveMeUppercase.
d) Write a function that takes in a single character and returns true if the character parameter is a letter (upper or lower case) and returns a false otherwise. Name the function isALetter.
Here is a main( ) function you can test your functions with, type this code below your function code.
#include
using namespace std;
int main( )
{
//to test absoluteValue
double someNumber;
double answer;
//to test maxOfThree
double num1;
double num2;
double num3;
double theMax;
//to test giveMeUppercase
char someLetter;
char newVersionOfLetter;
//to test isALetter
char someChar;
bool result;
//code to test absoluteValue
someNumber = 4.5;
answer = absoluteValue(someNumber);
cout << “The absolute value of “ << someNumber << “ is “ << answer << endl;
someNumber = -3.4;
answer = aboluteValue(someNumber);
cout << “The absolute value of “ << someNumber << “ is “ << answer << endl;
//code to test maxOfThree
num1 = 6;
num2 = 7;
num3 = 4;
theMax = maxOfThree(num1, num2, num3);
cout << “The max of “ << num1 << “ “ << num2 << “ “ << num2 << “ and “ << num3 << “ is “ << theMax << endl;
//code to test giveMeUppercase
someLetter = ‘g’;
newVersonOfLetter = giveMeUppercase(someLetter);
cout << “The uppercase version of “ << someLetter << “ is “ << newVersionOfLetter << endl;
someLetter = ‘B’;
newVersonOfLetter = giveMeUppercase(someLetter);
cout << “The uppercase version of “ << someLetter << “ is “ << newVersionOfLetter << endl;
someLetter = ‘?’;
newVersonOfLetter = giveMeUppercase(someLetter);
cout << “The uppercase version of “ << someLetter << “ is “ << newVersionOfLetter << endl;
//code to test isALetter
someChar = ‘j’;
result = isALetter(someChar);
if(result)
{
cout << someChar << “ is a letter.” << endl;;
}
else
{
cout << someChar << “ is not a letter.” << endl;
}
someChar = ‘R’;
result = isALetter(someChar);
if(result)
{
cout << someChar << “ is a letter.” << endl;;
}
else
{
cout << someChar << “ is not a letter.” << endl;
}
someChar = ‘&’;
result = isALetter(someChar);
if(result)
{
cout << someChar << “ is a letter.” << endl;;
}
else
{
cout << someChar << “ is not a letter.” << endl;
}
cout << “ Testing Complete ”;
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
double absoluteValue(double s)
{
if (s< 0)
return -1*s;
return s;
}
double maxOfThree(double n1,double n2,double n3)
{
double max = n1>n2?n1:n2;
max = max>n3?max:n3;
return max;
}
char giveMeUppercase(char s)
{
char k = s;
if(s>97) k = s-32;
return k;
}
int isALetter(char s)
{
if((s>=65 && s<=91)||(s>=97 && s<=123))
return 1;
return 0;
}
int main( )
{
//to test absoluteValue
double someNumber;
double answer;
//to test maxOfThree
double num1;
double num2;
double num3;
double theMax;
//to test giveMeUppercase
char someLetter;
char newVersionOfLetter;
//to test isALetter
char someChar;
bool result;
//code to test absoluteValue
someNumber = 4.5;
answer = absoluteValue(someNumber);
cout << "The absolute value of" << someNumber << "is" << answer << endl;
someNumber = -3.4;
answer = absoluteValue(someNumber);
cout << "The absolute value of" << someNumber << "is" << answer << endl;
//code to test maxOfThree
num1 = 6;
num2 = 7;
num3 = 4;
theMax = maxOfThree(num1, num2, num3);
cout << "The max of " << num1 << " " << num2 << " " << num2 << " and " << num3 << " is " << theMax << endl;
//code to test giveMeUppercase
someLetter = 'g';
newVersionOfLetter = giveMeUppercase(someLetter);
cout << "The uppercase version of " << someLetter << " is " << newVersionOfLetter << endl;
someLetter = 'B';
newVersionOfLetter = giveMeUppercase(someLetter);
cout << "The uppercase version of " << someLetter << " is " << newVersionOfLetter << endl;
someLetter = '?';
newVersionOfLetter = giveMeUppercase(someLetter);
cout << "The uppercase version of " << someLetter << " is " << newVersionOfLetter << endl;
//code to test isALetter
someChar = 'j';
result = isALetter(someChar);
if(result)
{
cout << someChar << " is a letter." << endl;;
}
else
{
cout << someChar << " is not a letter." << endl;
}
someChar = 'R';
result = isALetter(someChar);
if(result)
{
cout << someChar << " is a letter." << endl;;
}
else
{
cout << someChar << " is not a letter." << endl;
}
someChar = '&';
result = isALetter(someChar);
if(result)
{
cout << someChar << " is a letter." << endl;;
}
else
{
cout << someChar << " is not a letter." << endl;
}
cout << " Testing Complete ";
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.