The student is required to create the following functions. Please avoid using ma
ID: 3913780 • Letter: T
Question
The student is required to create the following functions.
Please avoid using magic numbers in these functions. Use constants when appropriate.
The student needs to create the following functions with the names indicated.
Functions:
Below are descriptions of the 12 functions you are to write:
Integer Functions:
find - Accepts an integer array, its length, and a integer value to find. It returns true if value exists in the array. Otherwise it returns false if not found.
IntArraySum - Accepts an integer array and length. Returns the sum of the integers stored in the array or zero if length is zero.
intArrayAverage - Accepts an integer array. This function returns the average of the elements of the array. Return 0 if the array is empty.
isSorted - Accepts an integer vector and returns a true of the vector is in sorted order. If the vector is empty return true.
String Functions:
countPunctCaseChars - Given a string, return the number of punction characters. Use ispunct from the cctype library.
countUpperCaseChars - Given a string, return the number of uppercase characters. Use isupper from the cctype library.
countLowerCaseChars - Given a string, return the number of lowercase characters. Use islower from the cctype library.
positionOfMaxAsciiValue - Given a string, return the position in the string of the character with the maximum ASCII value.
convertToUpper - Given an input string convert all alphabetic characters to uppercase. Return a new string converted to uppercase. If the input string is empty just return the input string.
convertToLower - Given an input string convert all alphabetic characters to lower case. Return a new string converted to lowercase. If the input string is empty just return the input string.
removeSpaces - Given an input string remove all spaces from this string. Return a new string with spaces removed. If the input string is empty return the original string.
characterCounts - Given a string, return a vector of ints of length 26 containing the number of times each letter was seen. Index 0 corresponds to ‘a’, 1 to ‘b’, etc… Treat upper-case letters as lower-case.
USE INSTRUCTIONS TO FILL OUT THE CODE BELOW
Explanation / Answer
Hello there, I am providing complete code for above 12 functions:
Source code==>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<cctype>
using namespace std;
bool find(int array[], int length, int value){
for(int i = 0;i<length;i++)
{
if(array[i]==value)
{
return true;
}
}
return false;
}
int intArraySum(int array[], int length){
int sum=0;
for(int i = 0;i<length;i++)
{
sum+=array[i];
}
return sum;
}
double intArrayAverage(int array[], int length){
int sum=intArraySum(array,length);
if(sum!=0)
{
return sum/length;
}
return 0;
}
bool isSorted(vector <int>vec) {
auto i=vec.begin();
for(auto j = next(vec.begin());j!=vec.end();++j)
{
if(*i > *j)
{
return false;
}
i=j;
}
return true;
}
// Strings
unsigned int countPunctChars(const string &countme){
int count=0;
for(auto i = countme.begin();i!=countme.end();++i)
{
if(ispunct(*i))
{
count++;
}
}
return count;
}
unsigned int countUpperCaseChars(const string &countme){
int count=0;
for(auto i = countme.begin();i!=countme.end();++i)
{
if(isupper(*i))
{
count++;
}
}
return count;
}
unsigned int countLowerCaseChars(const string &countme){
int count=0;
for(auto i = countme.begin();i!=countme.end();++i)
{
if(islower(*i))
{
count++;
}
}
return count;
}
int positionOfMaxAsciiValue(const string &countme){
int maxAsci=0;
int index;
for(auto i = countme.begin();i!=countme.end();++i)
{
if(int(*i) > maxAsci)
{
maxAsci=int(*i);
index = i-countme.begin();
}
}
return index+1; // Returns Position.
}
string convertToLower(const string &to_be_converted){
string newString;
int i = 0;
while(to_be_converted[i])
{
newString[i]=tolower(to_be_converted[i]);
i++;
}
return newString;
}
string convertToUpper(const string &to_be_converted){
string newString;
int i = 0;
while(to_be_converted[i])
{
newString[i]=tolower(to_be_converted[i]);
i++;
}
return newString;
}
string removeSpaces(const string &remove_from_me){
string res;
for(auto i=remove_from_me.begin(); i!=remove_from_me.end();++i)
{
if(!isspace(*i)) {
res.push_back(*i);
}
}
return res;
}
vector <int> characterCounts(const string &countme){
vector <int> count;
char c;
for(auto i = countme.begin();i!=countme.end();++i)
{
c=tolower(*i);
count[int(c)-96]+=1;
}
return count;
}
template<typename T>
string print(vector<T> vec);
template<typename T>
string print(T array[], int length);
int main(){
srand(time(0));
cout << "find:" << endl;
int x1[10] = {1,2,3,5,5,6,7,8,9,0};
cout << "false/0 = " << find(x1, 10, 4) << endl;
cout << "false/0 = " << find(x1, 10, 99) << endl;
cout << "true/1 = " << find(x1, 10, 5) << endl;
cout << "true/1 = " << find(x1, 10, 0) << endl;
cout << "true/1 = " << find(x1, 10, 1) << endl;
cout << "true/1 = " << find(x1, 10, 6) << endl;
cout << endl;
cout << endl;
cout << " String Functions: " << endl;
string upper_test = "aaABCdeFG";
cout << "countUpperCase" << endl;
cout << "5 = " << countUpperCaseChars(upper_test) << endl;
cout << "5 = " << countUpperCaseChars("AAAAA") << endl;
cout << "0 = " << countUpperCaseChars("aaaaa") << endl;
string no_upper_case = "aaaa";
cout << "0 = " << countUpperCaseChars(no_upper_case) << endl;
cout << "RemoveSpaces:" << endl;
string test1 = "aa AB Cd e FG";
string test2 = "aaAB Cd e FG ";
string test3 = " rrAB Cd e FG";
string test4 = " rrAB Cd e FG ";
cout << "aaABCdeFG = " << removeSpaces(test1) << endl;
cout << "aaABCdeFG = " << removeSpaces(test2) << endl;
cout << "rrABCdeFG = " << removeSpaces(test3) << endl;
cout << "rrABCdeFG = " << removeSpaces(test4) << endl;
return 0;
}
// A sneaky way to allow 1 function to print any typed array, as long as
// the passed array element can be sent to <<.
// The stringstream allows us to 'print' information to a fake output
// stream, and then get the result as a string. It's a simple way of
// getting a non-string/character into a string.
// Contense of this function will not be tested in this course!
template<typename T>
string print(vector<T> vect) {
stringstream out;
out << '[';
for(int i = 0; i < vect.size(); i++){
out << vect[i];
if(i != vect.size()-1)out << ',';
}
out << ']';
return out.str();
}
template<typename T>
string print(T array[], int length){
stringstream out;
out << '[';
for(int i = 0; i < length; i++){
out << array[i];
if(i != length-1)out << ',';
}
out << ']';
return out.str();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Feel free to ask queries and modification for above solution, I am always here to help you!!!
Thank you!!! :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.